Exposing AWS Lambda with API Gateway: Build a Serverless API
I build and deploy cloud-native applications using AWS and DevOps practices. I share practical tutorials on CI/CD pipelines, serverless architectures, and real project learnings, and Iโm exploring MLOps.
In the previous article, we deployed our backend logic using AWS Lambda and packaged the application code from our GitHub repository.
At this stage, the Lambda function is running in the cloud, but it cannot yet be accessed by users or frontend applications.
To make the backend accessible over HTTP, we need an API management layer.
This is where Amazon API Gateway becomes essential.
API Gateway acts as the entry point for serverless applications by receiving requests from clients and forwarding them to backend services like Lambda.
In this article, we will configure API Gateway and integrate it with our Lambda function to create a fully functional serverless API endpoint.
Architecture Overview
Our serverless architecture now looks like this:
User
โ
CloudFront
โ
S3 (Frontend)
โ
API Gateway
โ
Lambda
Flow of a request:
The user interacts with the frontend application
The frontend sends an HTTP request to API Gateway
API Gateway invokes the Lambda function
Lambda processes the request and returns a response
The response is sent back to the frontend
This pattern is widely used in modern serverless applications.
Why Use API Gateway
Using Amazon API Gateway provides several advantages.
Secure API Management
API Gateway allows authentication, authorization, and request validation.
Automatic Scaling
It automatically handles traffic spikes without manual scaling.
Built-in Monitoring
Integration with Amazon CloudWatch helps monitor API usage and performance.
Simplified Integration
API Gateway directly integrates with AWS Lambda, making serverless APIs easy to build.
๐ ๏ธ Step-by-Step Implementation
โ Step 1: Create an API
Open AWS Console โ Navigate to API Gateway
Click Create API
Choose REST API (Note: HTTP APIs are simpler and cheaper, but REST API is used here for flexibility)
Select:
New API
API Name:
ecommerce-apiEndpoint Type: Regional
Name of the API
API endpoint type Regional
Step 2: Add Lambda Integration
๐ Step 2: Add Lambda Integration
- Click Create Method
Choose:
Method Type:
ANYIntegration Type: Lambda Function
Enable Lambda Proxy Integration
Select your Lambda function:
This step connects API Gateway with the Lambda function.
Step 3: Create an API Route
๐ Step 3: Create API Routes
- Now define the routes your API will handle.
Create Base Resource:
/
Now define the routes your API will handle.
Create Base Resource:
/api
Create Proxy Resource:
/api/{proxy+}
This allows dynamic routing such as:
POST /api/products
GET /api/products/{id}
DELETE /api/products/{id}
POST /api/login
Step 4: Enable CORS
Since the frontend runs on CloudFront and S3, we must enable CORS.
In API Gateway:
๐ Step 4: Enable CORS
Since your frontend is hosted on S3 and served via CloudFront, CORS must be enabled.
Configure:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Enable CORS for:
/api/{proxy+}
Also enable responses for:
4XX errors
5XX errors
Click on Enable CORS for /api/{proxy+}
This allows the frontend application to communicate with the API.
Step 5: Deploy the API
- Click Deploy API
Create a new stage:
Stage Name: dev
- Deploy
You will receive an endpoint like:
https://abc123.execute-api.ap-south-1.amazonaws.com
This endpoint can now be used by the frontend.
Step 6: Test the API
You can test the API using curl.
Example:
curl https://abc123.execute-api.ap-south-1.amazonaws.com/api/products
Expected response:
{
"message": "Yoy will get the products list"
}
If the response appears, the Lambda integration is working correctly.
Connecting the Frontend to the API
Now update the frontend code to call the API.
Example JavaScript request:
<script>
window.API_BASE_URL = 'API_GATWAY_URL';
</script>
The frontend application hosted on S3 + CloudFront can now communicate with the backend through API Gateway.
Benefits of This Architecture
Using Lambda + API Gateway provides a fully managed backend architecture.
Key benefits include:
โ No server management
โ Automatic scaling
โ Pay-per-request pricing
โ High availability
This architecture is widely used by modern cloud-native applications.
Conclusion
In this article, we integrated Amazon API Gateway with AWS Lambda to expose our backend function as a public API.
This step completes the core serverless backend architecture, allowing the frontend application to interact with backend logic through secure HTTP endpoints.
With this setup, we now have:
Frontend hosted on S3 + CloudFront
Backend logic running in Lambda
API layer managed by API Gateway
๐ฎ Whatโs Next?
In the next article, we will enhance our backend by integrating:
MongoDB Atlas for persistent storage
Redis Cloud for caching
AWS Systems Manager Parameter Store for secure configuration
This will make our application more scalable, performant, and production-ready.