Amazon API Gateway
- A fully managed service used to
create, publish, maintain, monitor, and secure APIsat any scale. - Act as the "front door" for applications to access
data, business logic, or functionalityfrom your backend services. - Create RESTful APIs and WebSocket APIs that enable
real-time two-way communicationapplications. - Supports
containerizedandserverlessworkloads, as well asweb applications.
1. Types of APIs supported by API Gateway
Amazon API Gateway supports the following three types of APIs supported by Amazon API Gateway:
- HTTP APIs(Stateless):
- Optimized for serverless workloads: HTTP APIs are designed to proxy requests to AWS Lambda functions or other HTTP backends, making them ideal for building serverless applications.
- No API management features: Unlike REST APIs, HTTP APIs are simpler and do not offer features like API key management, usage plans, or analytics.
- REST APIs(Stateless):
- Proxy functionality with API management: REST APIs in API Gateway offer the ability to proxy requests to backend services (like Lambda or HTTP endpoints), while also providing a comprehensive suite of API management features.
- API management features include:
- Usage plans
- API keys
- Publishing
- Monetization
- These features allow you to manage and scale your APIs effectively.
- WebSocket APIs(Stateful):
- Persistent connection for real-time communication: WebSocket APIs maintain an open connection between the client and the server, allowing for continuous, two-way communication.
- Real-time message delivery: Ideal for real-time applications such as chat, gaming, and live data feeds. When a message is received from a client, WebSocket APIs can trigger backend integrations, such as AWS Lambda functions, Amazon Kinesis, or HTTP endpoints.
2. Throttling incoming requests
Amazon API Gateway uses throttling to control the rate of incoming requests and prevent API from being overwhelmed. This is done using the token bucket algorithm.
- Token Bucket Algorithm:
- The token bucket algorithm is used to manage the flow of requests. Tokens are added to the bucket at a fixed rate, and each incoming request requires a token to be processed.
- If there are no tokens available when a request arrives, the request is throttled (rejected or delayed until tokens become available).
- Steady-State Rate:
- This is the rate at which tokens are added to the bucket, essentially the normal request processing rate over time. It is defined in requests per second (RPS).
- Burst Limit:
- The burst is the maximum number of requests that can be handled at once beyond the steady-state rate, without exceeding the limit. This is the maximum bucket size.
- If the number of incoming requests exceeds the available tokens in the bucket (and the bucket is full), excess requests will be throttled or rejected.
3. API Gateway vs Internet Gateway
API Gateway:- A regional service.
- Operates at the
Application Layer 7(request level) - Handle requests coming from outside to access backend resources in VPC.
- Internet Gateway:
- Operates at the
Network Layer 3(IP Address) - Handle requests going outside from a resource within a public subnet VPC.
- Allows instances in your VPC to communicate with the internet, handling outbound traffic like software updates or API calls to third-party services.
- Attached to a VPC and provides a connection between that VPC and the internet.
- It's specific to a VPC, so each VPC can have only one Internet Gateway.
- Operates at the
4. API Gateway Caching
- You can enable caching in
Amazon API Gatewayto cache your endpoint's responses. - Reduces the number of calls to your
endpointand improveslatencyfor API requests. - Caching is enabled for a specific
stagein API Gateway. - API Gateway caches responses for a specified
time-to-live (TTL)period (in seconds). - The default TTL value for API caching is
300 seconds. - The maximum TTL value is
3600 seconds. - Setting
TTL=0means thatcaching is disabled. - Ideal for use cases where
stale datacan be accepted for up to24 hours. - API Gateway responds to requests by looking up responses in the
cache, reducing the need to query the endpoint. - When to Use API Gateway Caching:
- When caching is needed for single-stage API requests and responses.
- For simple TTL-based caching with minimal setup.
- When caching logic is tied directly to an API Gateway stage, reducing complexity.
- Redis Cache is ideal for complex, shared, and flexible caching scenarios, while API Gateway caching is suited for simple, stage-specific caching needs. Example where redis cache is preferred - An e-commerce platform has multiple microservices for product catalog, user recommendations, and inventory management. These services need to access the same frequently queried data (e.g., product details, stock availability) with different requirements
5. Pricing
- Core Pricing: Number of API requests processed + amount of data transferred out of the API Gateway as response.
- Optional: Caching (charged hourly based on size).
6. HTTP_PROXY and AWS_PROXY
HTTP_PROXY and AWS_PROXY are two integration types for Amazon API Gateway when connecting to backend services.
- HTTP_PROXY: A direct integration with an HTTP or HTTPS endpoint. API Gateway forwards client requests to the specified backend endpoint, acting as a transparent HTTP proxy.
- AWS_PROXY: Also known as Lambda Proxy integration, this is a direct integration with AWS Lambda functions or other AWS services (using AWS SDK).
7. Configure a Custom Domain for API Gateway with Route 53
User --> DNS Lookup (Route 53) --> API Gateway Custom Domain (api.example.com) --> Backend API
- Create an API Gateway Regional Endpoint: Deploy the backend microservices in API Gateway with a regional endpoint.
- Get an SSL/TLS Certificate: Use AWS Certificate Manager (ACM) in the same region as API Gateway to request or import a certificate for your domain (e.g.,
api.example.com). - Set Up a Custom Domain in API Gateway: Associate the custom domain name (
api.example.com) with the API Gateway endpoint and attach the ACM certificate in the same region where the API Gateway is hosted. - Configure Route 53: Create an alias record for
api.example.comin the Route 53 hosted zone to point to the API Gateway custom domain. - Test the Setup: Access the API securely via
https://api.example.comand verify functionality and HTTPS encryption.
8. Question:
A company has registered its domain name with Amazon Route 53. The company uses Amazon API Gateway in the ca-central-1 Region as a public interface for its backend microservice APIs. Third-party services consume the APIs securely. The company wants to design its API Gateway URL with the company's domain name and corresponding certificate so that the third-party services can use HTTPS. Which solution will meet these requirements?
Create a Regional API Gateway endpoint. Associate the API Gateway endpoint with the company's domain name. Import the public certificate associated with the company's domain name into AWS Certificate Manager (ACM) in the same Region. Attach the certificate to the API Gateway endpoint. Configure Route 53 to route traffic to the API Gateway endpoint. (Correct Ans)- Create a Regional API Gateway endpoint. Associate the API Gateway endpoint with the company's domain name. Import the public certificate associated with the company's domain name into AWS Certificate Manager (ACM) in the us-east-1 Region. Attach the certificate to the API Gateway APIs. Create Route 53 DNS records with the company's domain name. Point an A record to the company's domain name.
9. Question: Listener rule - redirect http to https
A company has a website hosted on AWS. The website is behind an Application Load Balancer (ALB) that is configured to handle HTTP and HTTPS separately. The company wants to forward all requests to the website so that the requests will use HTTPS. What should a solutions architect do to meet this requirement?
- Update the ALB's network ACL to accept only HTTPS traffic.
- Create a rule that replaces the HTTP in the URL with HTTPS.
Create a listener rule on the ALB toredirectHTTP traffic to HTTPS.(Correct Ans)- Replace the ALB with a Network Load Balancer configured to use Server Name Indication (SNI).
Explanation: It is not possible to replace the HTTP in the URL with HTTPS directly using a single rule on the ALB. The correct action is to redirect all incoming HTTP traffic to HTTPS by create a listener rule using the redirect-to-https action on the ALB that automatically redirects HTTP traffic to HTTPS. This allows the ALB to inspect incoming requests and, when detecting HTTP traffic, redirect it to the HTTPS listener.
8. List of Gateways in AWS
- Networking Gateways
- Storage Gateways
6. AWS Storage Gateway - Application Integration Gateways
7. Amazon API Gateway - Hybrid and Edge Gateways
8. Direct Connect Gateway