# Serverless Proxy with AWS API Gateway and AWS Lambda

DevFeed: [Serverless Proxy with AWS API Gateway and AWS Lambda](<https://devfeed.tech/articles/serverless-proxy-with-aws-api-gateway-and-aws-lambda-23691.md>)

Original publisher: [Read original article](<https://medium.com/betclic-tech/serverless-proxy-with-aws-api-gateway-and-aws-lambda-c09bd1c20c84?source=rss----7e406d68d94b---4>)

Author: Guillaume Lannebere

Published: 2021-11-10T08:23:55Z

Content type: tutorial

Language: en

Sources: [Betclic Tech](<https://devfeed.tech/sources/betclic-tech-medium.md>)

Topics: [Serverless](<https://devfeed.tech/topics/serverless.md>), [AWS Lambda](<https://devfeed.tech/topics/aws-lambda.md>), [proxy](<https://devfeed.tech/topics/proxy.md>), [Amazon VPC](<https://devfeed.tech/topics/amazon-vpc.md>), [gateway](<https://devfeed.tech/topics/gateway.md>), [REST API](<https://devfeed.tech/topics/rest-api.md>), [API](<https://devfeed.tech/topics/api.md>), [serverless framework](<https://devfeed.tech/topics/serverless-framework.md>)

Tags: [api-gateway](<https://devfeed.tech/tags/api-gateway.md>), [aws](<https://devfeed.tech/tags/aws.md>), [aws-lambda](<https://devfeed.tech/tags/aws-lambda.md>), [ec2](<https://devfeed.tech/tags/ec2.md>), [github](<https://devfeed.tech/tags/github.md>), [network-interface](<https://devfeed.tech/tags/network-interface.md>), [proxy](<https://devfeed.tech/tags/proxy.md>), [python](<https://devfeed.tech/tags/python.md>), [security](<https://devfeed.tech/tags/security.md>), [serverless](<https://devfeed.tech/tags/serverless.md>), [urllib3](<https://devfeed.tech/tags/urllib3.md>), [use-case](<https://devfeed.tech/tags/use-case.md>), [vpc](<https://devfeed.tech/tags/vpc.md>)

## AI overview

A public API Gateway can relay requests to a private VPC backend through a VPC-attached Lambda, which forwards the request using Python and urllib3. The article gives a GitHub webhook-to-private-EC2 example and a public-subnet-to-private-subnet example, noting that the Lambda connects through an ENI. It recommends REST API when calls cannot be authenticated, such as GitHub webhooks; HTTP API may suit authenticated calls when price matters. REST API supports features unavailable or harder to configure for HTTP API in the described Serverless Framework setup, including API keys, resource policies, and throttling configuration. The article also recommends restricting Lambda egress to the backend, setting a short timeout, and protecting the public gateway with suitable throttling and authentication or other controls. A private ALB/NLB integration via VPC Link is presented as a simpler, less customizable alternative where supported.

## Source excerpt

It's sometimes difficult to communicate between public and private subnet in AWS. We will see how we can easily do that with the help of API Gateway and AWS Lambda. Use case exampleExample 1 It's sometimes necessary to have a Github repository communicating with a private EC2 instance. But when an instance is private, it can be really complicated without impacting the security of the instance. Solution Github Webhook calls a Public API Gateway, API Gateway triggers a Lambda attached to VPC. The role of this Lambda is to forward the content of the Github Webhook to the EC2 instance. Remark: An AWS Lambda attached to a VPC isn't deployed inside the VPC, an Elastic Network Interface (ENI) is created to link the Lambda function and the different subnets inside the VPC. Example 2 Other example, we may need to communicate between a public instance inside a public subnet and a private instance inside a private subnet inside a VPC. For security reason, direct routing between Public subnet and private Subnet is not possible. So, to facilitate this, it's possible to use this solution of Serverless Proxy. Solution On the same way as Solution 1, we can communicate between Public and Private instance via a Serverless Proxy thanks to AWS Api Gateway and AWS Lambda. This solution is easy to implement, highly scalable, secure and not expensive. ImplementationAWS Lambda Lambda function is quite simple. The goal of this lambda is just to forward the information received from API Gateway to the destination inside VPC. Here we use python and Lambda function is as simple as that: https://medium.com/media/f5360ab428b2536b8f1d4aa84cb140fc/href To redirect requests I use the python lib urllib3, for more information or if you want to update the code check the official documentation. To facilitate the deployment, backend url is passed in environment variable. This lambda need to be attached to a VPC. To know how to optimize Lambda for cost and/or performance, 2 tools exists: the well know AW