API GATEWAY Flashcards
Inegrations
Lambda Function: REST API
HTTP: Expose HTTP endpoint
API on premise
ALB on Cloud enviroment
Add rate limit, cache, user auth, api keys
AWS service
Expose any aws api through gateway
auth, deploy public, rate control
API gateway endpoint types
Edge Optimized
Cloudfront edge location routing of request
API gateway in one region services multiple others
Regional
Clients in same region
can be integrated with cloudfront for more control
Api clients in one region
Private
Only in VPC, use resource policy to access
API Gateway Deployment
Changes not implemented until Deployment, this makes it LIVE
Deployed to Stages: Dev , Prod , TEST
Each stage gets own configuration, can be rolled back as each step of stage is kept.
API Breaking Change
Blue/Green deployment of versions, allows stages to be assigned V1 and V2
when new stage deployed, new API url generated, users are directed slowly over to new stage without breaking old stage user sessions. full migration over time.
Stage Variables
Stage Variables are similar to ENVIROMENT VARIABLES
Used for items that change often, changing configuration variables.
Usage:
Configure HTTP endpoints dev/test/prod
Pass config parameters to AWS Lambda through mapping templates
Passed to the context object in AWS lambda
Stage Variable-> Lambda Alias
Dev Stage -> DEV alias ->Latest lambda function
Test Stage -> Prod Alias -> Lambda V1 (95%) Lambda V2 (5%)
Prod Stage -> Test Alias -> Lambda V2 (100)%
This allows prod to send 5% of traffic over to new stage.
API GATEWAY CANARY Deployment
Allows testing of new API gateway
Client traffic moves 95% to the prod stage, then 5% will be directed to canary, this allows testing before offloading 100%
Prod Stage -> V1
Prod Stage Canary -> V2
Allows Blue-Green Deployment
- Allows Separate metric and logs
- override stage variables
API gateway integration: MOCK
MOCK: for Development , allows to make gateway to be made without actually sending back responses to backend. enables you to develop your API independently from your backend.
API INTEGRATION:
HTTP/AWS Lambda / other services
HTTP/AWS (Lambda / other services)
Custom HTTP
Custom AWS service integration
Configure integration request and integration response
setup data mapping suing mapping templates for request and response. API gateway has power to change request and response.
API integration
You choose an API integration type according to the types of integration endpoint you work with and how you want data to pass to and from the integration endpoint.
API INTEGRATION:
AWS Proxy
Incoming request is the input to Lambda
Function is responsible for the logic of request/response
NO MAPPING TEMPLATE, cant change HEADERS , or QUERY STRING PARAM, These are passed to lambda function as arguments
Lambda takes invocation, and processes and creates a function response. Work is on backend, API gateway just proxy request through
API INTEGRATION:
HTTP Proxy
NO MAPPING TEMPLATE
HTTP request is passed to the backend (ex ALB)
HTTP response from Backend is forwarded by API gateway
Mapping Templates (AWS OR HTTP) NO PROXY
USE: Modify Request and responses
Rename / Modify Query String Parameters
Modify Body Content
Add Headers
Uses Velocity Template Language VTL
Filter output results, and removes unnecessary data.
Mapping template example
JSON to XML with soap
Client -JSON-> API -XML->SOAP API
SOAP API -XML-> API -JSON->Client
API gateway:
extract data that comes in by JSON
build soap message based on request data (mapping template)
Call SOAP service with built XML
receive XML
Tranform XML response to JSON back to user.
Swagger
API definition as code
Import existing Swagger/ OpenAPI 3.0
Method Method Request Integration Request Method response \+AWS extensions for API
Can export current API as Swagger/OPENAPI
Swagger written in YAML or JSON
Swagger can be used to generate SDK for apps
CACHING API response
Caching reduce calls to backend
Default TTL is 300, 5 min
MAX is 3600, min is 60
Defined per stage or override per Method
Cache encryption can occur
Size 0.5GB to 237GB
Expensive, only use in production
Cache invalidation
Can be flushed entirely
or clients can invalidate
Cache-Control:Max-age=0
Need permissions from IAM
if not impose Inavlidatecache policy or not choose “require auth” option in console then any client can invalidate cache!!!
METHOD
The primary or most-commonly-used HTTP verbs (or methods, as they are properly called) are POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively.
In API Gateway, an API method embodies a method request and a method response. You set up an API method to define what a client should or must do to submit a request to access the service at the backend and to define the responses that the client receives in return. … An API method request is an HTTP request.
STAGE
A stage is a named reference to a deployment, which is a snapshot of the API. You use a Stage to manage and optimize a particular deployment. For example, you can configure stage settings to enable caching, customize request throttling, configure logging, define stage variables, or attach a canary release for testing.
API Usage Plans
Usage plan
Who can access Stage and Method
Speed of Access
What keys are linked to ID clients and meter access
Can Configure throttle limits and quota limits that are enforced on individual client.
API Keys
Alphanumeric String, distribute to clients
Can be used with usage plans to control access
Throttle limit is applied to API keys
Quota limits is the overall number of max requests
Correct Order for API key
Configure and use a Plan
- Create API, Configure methods in API to require API Key, Deploy API into stages
- Generate or import API key to distrbiute application to dev’s or customers who will use key
- Create usage plan with desired throttle and quota limits
- Associate API stages and API keys with usage plan!
**Callers of API must supply assigned API key in
X-api-Key Header in requests to API
monitor: Cloud watch Logs
Cloudwatch Logging at Stage level , Via Log level
Can override settings on a per API basis
Error: Anything that results in error
Info: anything outgoing or incoming from API
Debug
Log has: information about request / response body
Monitor: X-ray Tracing
Enable Tracing to get extra information about request in API gateway
X-ray Gateway + AWS lambda give full picture