Skip to main content

Command Palette

Search for a command to run...

Exposing AWS Lambda with API Gateway: Build a Serverless API

Updated
โ€ข5 min read
L

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:

  1. The user interacts with the frontend application

  2. The frontend sends an HTTP request to API Gateway

  3. API Gateway invokes the Lambda function

  4. Lambda processes the request and returns a response

  5. 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

  1. Open AWS Console โ†’ Navigate to API Gateway

  2. Click Create API

  1. Choose REST API (Note: HTTP APIs are simpler and cheaper, but REST API is used here for flexibility)

  2. Select:

    • New API

    • API Name: ecommerce-api

    • Endpoint Type: Regional

      Name of the API

API endpoint type Regional


Step 2: Add Lambda Integration

๐Ÿ”— Step 2: Add Lambda Integration

  1. Click Create Method
  1. Choose:

    • Method Type: ANY

    • Integration Type: Lambda Function

  1. Enable Lambda Proxy Integration

  2. Select your Lambda function:

This step connects API Gateway with the Lambda function.


Step 3: Create an API Route

๐ŸŒ Step 3: Create API Routes

  1. 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

  1. Click Deploy API
  1. Create a new stage:

    Stage Name: dev
    
  1. 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.