Last active
November 9, 2020 11:22
-
-
Save jweisman/e7960060022b9b9af18b81c9366f3b45 to your computer and use it in GitHub Desktop.
CloudFormation template to create resources necessary to run a Lambda function with a static public IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### | |
# Based on https://docs.aws.amazon.com/lambda/latest/dg/configuration-vpc.html | |
# and https://github.com/awsdocs/aws-lambda-developer-guide/blob/master/templates/vpc-privatepublic.yaml | |
#### | |
pubPrivateVPC: | |
Type: AWS::EC2::VPC | |
Properties: | |
CidrBlock: 172.31.0.0/16 | |
Tags: | |
- Key: Name | |
Value: | |
Ref: AWS::StackName | |
publicSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: pubPrivateVPC | |
AvailabilityZone: | |
Fn::Select: | |
- 0 | |
- Fn::GetAZs: "" | |
CidrBlock: 172.31.0.0/24 | |
MapPublicIpOnLaunch: true | |
privateSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: pubPrivateVPC | |
AvailabilityZone: | |
Fn::Select: | |
- 0 | |
- Fn::GetAZs: "" | |
CidrBlock: 172.31.3.0/24 | |
MapPublicIpOnLaunch: false | |
privateSubnet2: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: | |
Ref: pubPrivateVPC | |
AvailabilityZone: | |
Fn::Select: | |
- 1 | |
- Fn::GetAZs: "" | |
CidrBlock: 172.31.2.0/24 | |
MapPublicIpOnLaunch: false | |
internetGateway: | |
Type: AWS::EC2::InternetGateway | |
gatewayToInternet: | |
Type: AWS::EC2::VPCGatewayAttachment | |
Properties: | |
VpcId: | |
Ref: pubPrivateVPC | |
InternetGatewayId: | |
Ref: internetGateway | |
publicRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: | |
Ref: pubPrivateVPC | |
publicRoute: | |
Type: AWS::EC2::Route | |
DependsOn: gatewayToInternet | |
Properties: | |
RouteTableId: | |
Ref: publicRouteTable | |
DestinationCidrBlock: 0.0.0.0/0 | |
GatewayId: | |
Ref: internetGateway | |
publicSubnet1RouteTableAssociation: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: publicSubnet1 | |
RouteTableId: | |
Ref: publicRouteTable | |
natGateway: | |
Type: AWS::EC2::NatGateway | |
DependsOn: natPublicIP | |
Properties: | |
AllocationId: | |
Fn::GetAtt: [ natPublicIP, AllocationId ] | |
SubnetId: | |
Ref: publicSubnet1 | |
natPublicIP: | |
Type: AWS::EC2::EIP | |
DependsOn: pubPrivateVPC | |
Properties: | |
Domain: vpc | |
privateRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: | |
Ref: pubPrivateVPC | |
privateRoute: | |
Type: AWS::EC2::Route | |
Properties: | |
RouteTableId: | |
Ref: privateRouteTable | |
DestinationCidrBlock: 0.0.0.0/0 | |
NatGatewayId: | |
Ref: natGateway | |
privateSubnet1RouteTableAssociation: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: privateSubnet1 | |
RouteTableId: | |
Ref: privateRouteTable | |
privateSubnet2RouteTableAssociation: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: | |
Ref: privateSubnet2 | |
RouteTableId: | |
Ref: privateRouteTable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be used as follows:
Requires this policy in the Lambda execution role:
To get the IP address in the output: