Simplify REST API Management with AWS API Gateway Proxy Integration and Lambda
Tags: lambda, apigateway, node, aws
Published: 2024-07-01 14:04:55

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:

```javascript
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;
```

### 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`).

```javascript
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);
};
```

2. **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1lrwt2v5rae7xr9pm5w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bi1gw8xqcijfvueyywch.png)

2. **Create Proxy Resource**:
   - Go to **Resources**.
   - Create a new resource and enable the **proxy resource** toggle.
   - Name the resource `{proxy+}`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkrl86gun20rb028o8ln.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l99samgemqy7siw7os4l.png)

3. **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pfh4vhhnk32ctu0jywh.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ptsh1dsi21ekr9cymlh.png)

4. **Deploy API**:
   - Go to **Stages**.
   - Create a new stage (e.g., `dev`).
   - Deploy the API to the newly created stage.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qeo3oa2ozrnpyo3ip7xk.png)

### 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.
   
```javascript
const cors = require('cors');
app.use(cors());
```

2. **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.