DEV Community

se-piyush
se-piyush

Posted on

Simplify REST API Management with AWS API Gateway Proxy Integration and Lambda

Deploying a REST API using AWS Lambda and API Gateway with proxy integration can greatly streamline your serverless architecture. This method eliminates the need to maintain individual routes in the API Gateway, allowing you to manage all routing within your Express.js application. In this blog post, I’ll guide you through setting up an Express.js application deployed on a Lambda function and integrated with an API Gateway configured as a proxy resource.

Benefits of Using API Gateway as a Proxy

  • Simplified Route Management: No need to define each route in the API Gateway. All routing is handled within your Express app.
  • Centralized CORS Management: Manage CORS policies directly in your application code.
  • Flexibility: Easily add or modify routes without changing the API Gateway configuration.

Step-by-Step Guide to Deploying REST API with Lambda and API Gateway as a Proxy

Step 1: Set Up Your Express.js Application

First, set up your Express.js application. Here’s a basic example:

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.get('/api/example', (req, res) => {
  res.json({ message: 'This is an example route' });
});

module.exports = app;
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Lambda Function

Create a Lambda function that will serve as the backend for your API. The function will use the Express.js application.

  1. Handler Function: Create a handler file (index.js).
const awsServerlessExpress = require('aws-serverless-express');
const app = require('./app'); // Path to your Express app
const server = awsServerlessExpress.createServer(app);

exports.handler = (event, context) => {
  awsServerlessExpress.proxy(server, event, context);
};
Enter fullscreen mode Exit fullscreen mode
  1. Deploy Lambda: Package and deploy your Lambda function using the AWS CLI or any CI/CD pipeline.

Step 3: Set Up API Gateway

  1. Create API Gateway: In the AWS Management Console, create a new API Gateway of type REST API. Image description

Image description

  1. Create Proxy Resource:
    • Go to Resources.
    • Create a new resource and enable the proxy resource toggle.
    • Name the resource {proxy+}.

Image description

Image description

  1. Integration with Lambda:
    • After creating the proxy resource, select it and click on Actions -> Create Method.
    • Choose ANY method and click the checkmark.
    • Select Lambda Function as the integration type.
    • Enable Use Lambda Proxy Integration.
    • Enter the name of your Lambda function.
    • Click Save.

Image description

Image description

  1. Deploy API:
    • Go to Stages.
    • Create a new stage (e.g., dev).
    • Deploy the API to the newly created stage.

Image description

Step 4: Testing and Configuring CORS

With the proxy integration, you don't need to configure each route in the API Gateway. Your Lambda function handles all the routing. This setup also simplifies CORS configuration.

  1. Enable CORS in Code:
    • Use the cors middleware in your Express app.
const cors = require('cors');
app.use(cors());
Enter fullscreen mode Exit fullscreen mode
  1. Testing:
    • Test your API endpoints to ensure they work as expected.
    • You can use tools like Postman or curl to send requests to your API.

Conclusion

By using API Gateway as a proxy for your Lambda function, you can deploy a scalable and flexible REST API with minimal configuration overhead. This setup allows you to focus on your application logic and routing within the Express.js framework, while AWS handles the infrastructure scaling and availability.

This method not only simplifies the management of your routes but also ensures a more efficient and streamlined serverless architecture. By centralizing your CORS management and routing logic within your codebase, you gain greater control and flexibility over your API deployments.

Top comments (1)

Collapse
 
raajaryan profile image
Deepak Kumar

Hello everyone,

I hope you're all doing well. I recently launched an open-source project called the Ultimate JavaScript Project, and I'd love your support. Please check it out and give it a star on GitHub: Ultimate JavaScript Project. Your support would mean a lot to me and greatly help in the project's growth.

Thank you!