16 - Serverless Overviews

Amazon API Gateway

awsapi-gatewayserverlessrest-apisaa-c03

Overview

Clients need a way to invoke a Lambda function that uses DynamoDB for an API. Options:

  • The client invokes the Lambda function directly, which requires IAM permissions for the client.
  • An Application Load Balancer in front of Lambda exposes it as an HTTP endpoint.
  • API Gateway: a serverless offering to create REST APIs that are public and accessible to clients. The client talks to API Gateway, which proxies the request to Lambda.

API Gateway gives more than an HTTP endpoint: authentication, usage plans, development stages and more. API Gateway plus 02-AWS-Lambda is a full serverless application with no infrastructure to manage.


Features

  • Support for the WebSocket protocol (real-time streaming).
  • API versioning (v1, v2, v3 without breaking clients).
  • Multiple environments (dev, test, prod).
  • Security: many ways for authentication and authorization.
  • API keys and request throttling for clients making too many requests.
  • Swagger / Open API 3.0 import and export of API definitions.
  • Transform and validate requests and responses.
  • Generate SDK and API specifications.
  • Cache API responses.

Integrations

Integration Details
Lambda function Most common and easy way to expose a REST API backed by Lambda (full serverless app)
HTTP Expose any HTTP backend: an on-premises HTTP API or an Application Load Balancer. Gains rate limiting, caching, user authentication, API keys
AWS service Expose any AWS API through API Gateway, e.g. start a Step Functions workflow or post a message to SQS. Reasons: add authentication, deploy publicly, rate control

Example: API Gateway with Kinesis Data Streams

Clients send HTTP requests to API Gateway, which is configured to send messages into Kinesis Data Streams securely, without giving clients AWS credentials and without managing servers. From there, records can go to Kinesis Data Firehose and into an S3 bucket in JSON format.


API Gateway integrations

Clientspublic REST API API Gatewayauth, throttling, caching Lambda functionmost common HTTPon-premises API or ALB AWS serviceStep Functions, SQS, Kinesis The console also lists Mock and VPC link integration types.
Clients call one public REST API. Behind it sit Lambda, HTTP backends or other AWS services.

Endpoint Types

Type Details
Edge-Optimized (default) For global clients. Requests are routed through CloudFront Edge locations to improve latency. The API Gateway still lives in only one region
Regional For clients within the same region. No CloudFront edge locations. You can create your own distribution (transcript says "platform distribution", likely CloudFront) for the same result as edge-optimized, with more control over location strategies and settings
Private Only accessible from within your VPC using interface VPC endpoints (ENIs). Access defined with a resource policy

Security

Need Solution
Internal applications (e.g. on EC2) IAM roles
External users (mobile/web apps) Amazon Cognito (see 13-Amazon-Cognito)
Own logic Custom authorizer (a Lambda function)
HTTPS on a custom domain AWS Certificate Manager (ACM)
  • ACM certificate for an Edge-Optimized endpoint must be in us-east-1.
  • For a Regional endpoint the certificate must be in the same region as the API Gateway stage.
  • Set up a CNAME or A-alias record in Route 53 pointing to the API Gateway.
Exam Pattern

Expose AWS services (Kinesis, SQS, Step Functions) publicly and securely without sharing credentials -> API Gateway in front.


Hands-On Walkthrough

Create a REST API

  1. Open the API Gateway console. API types: HTTP API, WebSocket API, REST API (public or private). Only REST API is used here (try the new console).
  2. Choose REST API -> Build. Options: create new, import from an Open API definition file, clone an existing API, or the example API.
  3. New API named MyFirstAPI. Endpoint type options: Regional, Edge-optimized, Private. Choose Regional. Click Create API.

Create a Lambda function

  1. In Lambda create api-gateway-route-gets with a Python runtime (Python 3.11, any Python 3 works).
  2. Paste the code from lambda-code.py: it returns statusCode 200, headers with Content-Type: application/json, and a body of json.dumps "Hello from Lambda".
  3. Click Deploy. Test with an event DemoTest: result is "hello from Lambda" with status 200.

Create the GET method

  1. In the API click Create method, type GET.
  2. Integration types (five): Lambda function, HTTP, Mock, AWS service (any service in any region), VPC link. Choose Lambda function.
  3. Paste the function ARN and enable Lambda proxy integration (full request passed to Lambda and the response sent back).
  4. Timeout: Lambda can run 5 or 15 minutes, but API Gateway has a limited timeout. Default is 29 seconds. It can be customized to less than 29 seconds. For Regional and private REST APIs it can also be raised above 29 seconds through a Service Quotas request; edge-optimized APIs cannot.
AWS update (2024-06)

Regional and private REST APIs can raise the integration timeout above 29 s via a Service Quotas request; edge-optimized APIs cannot. The course said 29 s is a hard maximum, and the exam may still use the course figure. AWS source

  1. Click Create method. API Gateway is automatically granted the right to invoke the function. Verify in Lambda -> Configuration -> Permissions -> resource-based policy statement: the API Gateway is allowed to invoke the function if the source API is the one for route GET.
  2. Method execution shows: method request, Lambda integration request (proxy), integration response (API Gateway parses what Lambda sends) and method response (application/json).
  3. Use the Test tab (optional query strings and headers). Result: status 200, body "hello from Lambda", header content type JSON. The execution log is shown, useful for debugging.

Debug the event

  • Add print(event) in the function, deploy, test again, then check CloudWatch Logs (Monitoring -> latest log stream).
  • The event includes the resource (/), path (/), method (GET), headers, query string parameters and more. Lambda can use this to forge a response.

Add a /houses resource

  1. Create resource houses, then a GET method with Lambda proxy integration.
  2. Create a second function (Python) with the body changed to "hello from my pretty house", deploy, paste its ARN.
  3. Test: 200 "hello from my pretty house". Now / and /houses invoke two different Lambda functions.

Deploy to a stage

  1. Click Deploy API, choose New stage named dev.
  2. Copy the invoke URL into a browser. /dev returns "hello from Lambda", /dev/houses returns "hello from my pretty house".
  3. A wrong path such as /wrong returns the error "message missing authentication token".

Questions & Answers

Q1: Why use API Gateway instead of an ALB in front of Lambda?

Answer:

It adds authentication, usage plans, stages, API keys, throttling, caching, request/response validation and transformation, versioning, WebSocket and more.

Q2: What are the API Gateway endpoint types?

Answer:

Edge-Optimized (default, global clients via CloudFront edge), Regional (same-region clients) and Private (VPC only, interface VPC endpoints, resource policy).

Q3: What is the default API Gateway timeout?

Answer:

29 seconds by default, even if the Lambda function can run longer. Regional and private REST APIs can request a higher limit through Service Quotas.

AWS update (2024-06)

The 29 s default can be raised above 29 s for Regional and private REST APIs via Service Quotas; the course gave it as a fixed limit. AWS source

Q4: How do you authenticate API Gateway users?

Answer:

IAM roles for internal apps, Cognito for external users, custom authorizer (Lambda) for custom logic.

Q5: Where must the ACM certificate be for a custom domain?

Answer:

us-east-1 for Edge-Optimized. Same region as the stage for Regional. Add a Route 53 CNAME or A-alias record.

Q6: How do you let clients send data to Kinesis Data Streams without AWS credentials?

Answer:

Put API Gateway in front with an AWS service integration to Kinesis Data Streams.

Check yourself

Quiz

Flashcards

Click the card (or focus it), then use ← → to move and Space to flip.