# 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 - Medium](<https://devfeed.tech/sources/betclic-tech-medium.md>)

Topics: [Amazon API Gateway](<https://devfeed.tech/topics/amazon-api-gateway.md>), [AWS Lambda](<https://devfeed.tech/topics/aws-lambda.md>), [Amazon Web Services](<https://devfeed.tech/topics/aws.md>), [VPC](<https://devfeed.tech/topics/vpc.md>), [Serverless](<https://devfeed.tech/topics/serverless.md>), [Amazon EC2](<https://devfeed.tech/topics/amazon-ec2.md>), [Python](<https://devfeed.tech/topics/python.md>), [GitHub](<https://devfeed.tech/topics/github.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>), [python](<https://devfeed.tech/tags/python.md>), [rest](<https://devfeed.tech/tags/rest.md>), [serverless](<https://devfeed.tech/tags/serverless.md>), [vpc](<https://devfeed.tech/tags/vpc.md>)

## AI overview

This tutorial explains how to use a public AWS API Gateway and a Lambda function attached to a VPC as a serverless proxy between public and private subnets or instances. It covers forwarding GitHub webhook requests to a private EC2 instance, Lambda configuration with Python and an environment variable, and the distinction between REST and HTTP APIs.

## 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