1. Introduction
The introduction provides an overview of the article's focus, which is to present a comprehensive guide on integrating AWS API Gateway with AWS Lambda. It may briefly explain the significance of this integration in building serverless applications and providing a scalable and efficient API infrastructure.
2. Purpose
This section states the purpose of the article, which is to educate readers about AWS API Gateway and AWS Lambda and demonstrate how to set up API Gateway with Lambda integration. It aims to provide both AWS console and Prismscaler methods for different experience levels.
3. What is AWS API Gateway?
Amazon Web Services (AWS) API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs (Application Programming Interfaces) at any scale. It acts as a front-door for your applications, allowing you to expose backend services as APIs to external clients such as web applications, mobile applications, or other AWS services.
Here are some key features and components of AWS API Gateway:
- API Creation: API Gateway allows you to create RESTful APIs or WebSocket APIs, making it flexible for building various types of applications.
- API Deployment: You can deploy different versions or stages of your APIs, such as development, testing, and production, making it easy to manage different environments.
- Endpoint Configuration: You can configure various types of endpoints, including HTTP, HTTPS, Lambda functions, AWS services, or even custom domain names.
- Security: API Gateway provides multiple security mechanisms, including authentication and authorization options like API keys, IAM (Identity and Access Management) roles, Cognito user pools, and custom authorizers.
- Rate Limiting and Throttling: You can control the rate at which clients can make requests to your API to prevent abuse or overuse of resources.
- Request and Response Transformation: API Gateway supports request and response transformation, allowing you to modify or shape data as it passes through the API Gateway.
- CORS Support: For web applications, Cross-Origin Resource Sharing (CORS) support is available to control which domains can access your API.
- Monitoring and Logging: API Gateway provides detailed logs and metrics that help you monitor the usage and performance of your APIs. You can integrate it with AWS CloudWatch for monitoring and AWS X-Ray for tracing.
- Integration with AWS Services: You can easily integrate your API Gateway with other AWS services like AWS Lambda, AWS Step Functions, Amazon S3, and more.
- Developer Portal: AWS offers a developer portal that you can use to publish API documentation and make it easy for developers to discover and use your APIs.
- Custom Domains: You can map custom domain names to your API Gateway, which is useful for branding and creating a more user-friendly API endpoint.
- Versioning and Rollback: API Gateway supports API versioning, which allows you to make changes to your APIs while maintaining backward compatibility. You can also roll back to previous versions if needed.
- Access Control: You can control access to your APIs at different levels, including at the method, resource, or API level, using IAM policies or custom authorizers.
4. What is AWS Lambda function?
AWS Lambda is a serverless compute service provided by Amazon Web Services (AWS). It enables you to run code without having to manage servers. With AWS Lambda, you can execute your code in response to various events or triggers, such as HTTP requests, changes to data in an Amazon S3 bucket, updates to a database in Amazon DynamoDB, or custom events generated by other AWS services.
Key features and characteristics of AWS Lambda include:
- Event-Driven: AWS Lambda functions are event-driven, meaning they are triggered by specific events. These events can come from various AWS services or custom sources using AWS SDKs.
- Serverless: You don't need to provision or manage servers when using AWS Lambda. AWS automatically handles the infrastructure, including scaling and server maintenance, allowing you to focus solely on your code.
- Multiple Languages: Lambda supports multiple programming languages, including Node.js, Python, Java, Ruby, C#, PowerShell, and custom runtime support, allowing you to write functions in your preferred language.
- Pay-as-You-Go Pricing: You are charged based on the number of requests for your functions and the compute time consumed. There's no charge when your code is not running.
- Scalability: AWS Lambda automatically scales your functions in response to incoming requests. You don't need to worry about capacity planning or managing resources.
- Integration with AWS Services: Lambda integrates seamlessly with other AWS services, such as Amazon S3, Amazon DynamoDB, Amazon RDS, Amazon API Gateway, and more, making it a versatile tool for building serverless applications.
- Custom Execution Role: You can assign AWS Identity and Access Management (IAM) roles to your Lambda functions, allowing them to access specific AWS resources securely.
- Stateless: Lambda functions are stateless, meaning they don't store information between invocations. However, you can use other AWS services like Amazon S3 or DynamoDB to store persistent data.
- Versioning and Aliases: AWS Lambda supports versioning and aliases, allowing you to publish different versions of your functions and direct traffic to specific versions or aliases.
- Monitoring and Logging: AWS CloudWatch provides monitoring and logging capabilities for Lambda functions, helping you troubleshoot issues and track performance.
- Microservices: Building serverless microservices that respond to API requests or events.
- Data Processing: Performing data transformation, analysis, and ETL (Extract, Transform, Load) tasks.
- Real-time File Processing: Processing files uploaded to Amazon S3, such as image resizing or data validation.
- IoT Backend: Handling data from IoT devices and sensors.
- Chatbots: Implementing serverless chatbots that respond to user interactions.
- Scheduled Tasks: Running code on a schedule, such as backups, cleanup, or report generation.
5. Creating AWS API Gateway with Lambda integration with AWS console
This section provides a step-by-step guide on how to set up AWS API Gateway with Lambda integration using the AWS Management Console. It covers creating API endpoints, defining Lambda integrations, and handling requests and responses.
Step 1. Create Lambda function
Go to Lambda service → click to Create function → enter the necessary information like function name, runtime and architecture:
By default, Lambda will create an execution role with permissions to upload logs to Amazon CloudWatch Logs. You can customize this default role later when adding triggers. Or you can choose an existing role.
Then, click to Create function.
After function creation successful, edit your code into Code source section:
Step 2. Create API Gateway
Go to API Gateway service → Navigate to APIs → Click to Build:
Step 1 is choosing API integration type → choose Lambda → select your function you just created:
Configure API routes in step 2:
Define stage in step 3:
Step 4 is reviewing and creating API Gateway.
Here is the deployed API Gateway:
Now, you can use the Invoke URL as API endpoint.
With DevOps knowledge: 4-6 hours
Without DevOps knowledge: 2-3 days
6. Creating AWS API Gateway with Lambda integration with Prismscaler
Prism Scaler provides you with an intuitive model and a concise form, you just need to fill in the necessary information and press create, now PrismScaler will automatically build a simple web application/system that moves on a container on AWS.
Automatically build a serverless API module on AWS
・ If you want to implement the API easily with serverless
・ If you want to carry out API security, logging, certification, version control, etc. at once
・ When there is no need to save customer data and perform the same processing and response regardless of user
Advanced cases:
API Gateway with Lambda function integration, and that function has permission to access S3 bucket or DynamoDB:
5-15 minutes (without DevOps knowledge)
7. How to setup IAM role and permissions for Lambda function
Find out the Lambda function execution role in Configuration section:
If you want your Lambda function can access to other AWS resources, you must grant permission for execution role.
Add AWS policy
Edit your custom policy
Example policies for S3 bucket:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::<YOUR_BUCKET_NAME>",
"arn:aws:s3:::<YOUR_BUCKET_NAME>/*"
]
}
]
}
Example policies for DynamoDB:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "dynamodb:*",
"Resource": "arn:aws:dynamodb:<YOUR_REGION>:<YOUR_ACCOUNT_ID>:table/<YOUR_TABLE_NAME>"
}
]
}
8. Reference
The reference section lists the sources or external materials used in the article, allowing readers to explore further or verify the information provided.
Tutorial: Using Lambda with API Gateway - AWS Lambda
Tutorial: Build a Hello World REST API with Lambda proxy integration - Amazon API Gateway