Sometimes you need to move your application lambdas inside VPC subnets (eg. if you need them to be able to access an RDS cluster, or another service that doesn't mesh perfectly with serverless). Doing so removes them from the default VPC, and with that they lose internet access.
The templates included here show how you can deploy lambdas inside a VPC, and create a route for them back to the internet.
You can still use API Gateway and trigger your lambdas with events, but now they'll be able to reach the internet, too.
template.yaml
creates the VPC, which is then used by all the other resources.
template-nat.yaml
creates:
- An Internet Gateway for the VPC
- 3x private subnets (for your lambdas, or for RDS, or other resources...)
- 1x public subnet with a NAT Gateway
- Routing tables and routes...
- A routing table for the private subnets, routing internet traffic to the NAT Gateway
- A routing table for the public subnet, routing internet traffic to the Internet Gateway
A security group for the lambdas, and 3x private subnets appear in the outputs from template-nat.yaml
.
They can be applied to other resources (eg. the sample lambda in template.yaml
).