Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 030/c21e2f263c6c9937d780b5a94116218a to your computer and use it in GitHub Desktop.
Save 030/c21e2f263c6c9937d780b5a94116218a to your computer and use it in GitHub Desktop.
CloudFormation template implementing Private network which can be used by Serverless to deploy Lambda into VPCs an maintaining internet access
# Add the following to your existing VPC CF stack
# create 2 subnets, lambdas like to be in multiple subnets
Private1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs ]
CidrBlock: !Ref Private1CIDR
Private2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [ 0, !GetAZs ]
CidrBlock: !Ref Private2CIDR
NATIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway:
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NATIP.AllocationId
SubnetId: !Ref Subnet1 # PUBLIC SUBNET!
PrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Sub "${Name} Private (Lambda)"
DefaultPrivateRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway
Private1RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable
SubnetId: !Ref Private1
Private2RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable
SubnetId: !Ref Private2
Outputs:
PrivateSubnet1:
Value: !Ref Private1
Export:
Name: !Sub "${Pipe}-PrivateSubnet1"
PrivateSubnet2:
Value: !Ref Private2
Export:
Name: !Sub "${Pipe}-PrivateSubnet2"
@simonguldstrand
Copy link

Thanks for this!

@030
Copy link
Author

030 commented Apr 17, 2020

@simonguldstrand You have to thank @romaninsh I only forked it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment