API Gateway Flashcards
What are some examples/use cases of API Gateway Integrations (at a high level)?
Lambda
- invoke lambda function
- easy way to expost REST API backed by Lambda
HTTP
- expose HTTP endpoints int eh backend
- example: internal HTTP API on premises, Application Load Balancer
- can add rate limiting, caching, user authentications, API keys, etc.
AWS service
- expose any AWS API though the gateway (e.g., posting a message to SQS)
- can add authentication, deploy publicly, rate control, etc.
What are the three endpoint types?
Edge optimized (default)
- For global clients
- Requests are routed through the CloudFront Edge locations (better latency)
- API Gateway still lives in only one region
Regional
- Clients within the same region
- Could manually combine with CloudFront (more control over caching strategies and distribution)
Private
- Can only be accessed from your VPC using an interface VPC endpoint (ENI)
- Use a resource policy to define access
How can you provide user authentication for API Gateway?
- IAM roles (for internal applications)
- Cognito (identity for external users - e.g., mobile users)
- Custom Authorizer (custom logic in a Lambda function)
How can you use Custom Domain Name HTTPS security with API Gateway?
Integration with ACM - AWS certificate manager
- If using Edge-Optimized, then certificate must be in us-east-1
- If using Regional, certificate must be in same region as the API Gateway stage region
- Must set up CNAME or A-alias record in Route 53
How can you deal with API breaking changes from, say, a new version of a Lambda function?
Upversion your API Gateway stage to a be able to handle this new change - this will generate a new API Gateway URL.
What are stage variables, and what is a use case for them?
API Gateway environment variables
- Configure HTTP endpoints your stages talk to (dev, test, prod)
- Pass config params to Lambda through mapping templates
How are stage variables passed to a lambda function?
Through the ‘context’ object in Lambda
- Format: ${stageVariables.variableName}
How can you use stage variables and lambda aliases to manage lambda deployments?
Separate API Gateway into dev, test and prod stages
- Use stage variables to pass in the correct lambda alias to each stage (i.e., prod alias to prod stage)
- Can use the alias in prod to direct a small portion of traffic to the test lambda function (e.g., to test a V2 function)
What is a method to safely test new stage deployments in prod?
Enable canary deployment
- Choose the % of traffic the canary channel receives
- Gives separate metrics and logs for the canary stage
- Can override stage variables for canary
Equivalent of a blue/green deployment for Lambda and API Gateway
What is Integration Type MOCK?
API Gateway returns a response without sending the request to the backend
What is Integration Type HTTP / AWS?
Integration with Lambda & AWS Services
- Must configure both integration request and response
- Setup data mapping using mapping templates for the request and response
What is Integration Type AWS_PROXY?
Lambda proxy
- Request from client is the input to the lambda
- Function is responsible for the logic of request/response
- No mapping template, headers, query string params: these are passed as arguments
What is Integration Type HTTP_PROXY?
- No mapping template
- HTTP request passed to backend, and response from the backend is forwarded by API Gateway
- Possibility to add HTTP Headers (such as an API key, so that it does not need to be passed in via the client)
What is a mapping template, and where can it be used?
Used to modify request / responses
- rename / modify query string params
- modify body content
- add headers
- filter output results
- Content-Type must be set to application/json or application/xml
What is a use-case of mapping templates?
- Transform a REST API request (JSON) to a SOAP API request (XML) for use with a SOAP service
- Rename query string parameter variable names
What is Open API Spec, and how does it relate to API Gateway?
Common way of defining REST APIs, using API definition as code
- Can import existing OpenAPI 3.0 spec into API Gateway
- Can export current API as OpenAPI spec
How can you reduce unnecessary calls to the backend with API Gateway?
Configure API Gateway to perform basic validation of an API request before proceeding with the integration request
- If validation fails, returns a 400 error to the caller
- Checks that required request params are in the URI, query string and headers of an incoming request are included an non-blank
- Checks if the payload adheres to a configured JSON Schema request model
How can you reduce the number of calls to the backend for different methods/stages?
Enable caching
- Caches are defined per stage (and very expensive, so makes sense in production but not other envs)
- Can override caching config on a per method basis
How can you allow specific clients to invalidate a cache?
Include in the request the header: Cache-Control: max-age=0
- Requires proper IAM authorization
- If you don’t impose an InvalidateCache policy (i.e., request authorization), any client can invalidate the API cache
How can you make your API available to customers?
Usage plans
- Who can access one or more deployed stages and methods
- Configure throttling limits and quota limits per client
- Use API keys to identify API clients and meter access
API keys
- Alphanumeric string values to distribute to customers
- Throttling and quota limits are applied to API keys
- Associate API stages and API keys with a usage plan
How can you check if your API Gateway cache is working properly?
Enable logging to CloudWatch, and review CacheHitCount and CacheHitMiss metrics
How can you check the total number of API Gateway requests?
‘Count’ metric in CloudWatch
How can you check how long it is taking for the backend to reply to an API Gateway request?
Check the IntegrationLatency metric - measure time between the request sent to the backend and the response received.
How can you check the total time taken for API Gateway to respond to a request from a client?
Latency metric - total API Gateway overhead (including IntegrationLatency)