Forked from jbesw/gist:f9401b4c52a7446ef1bb71ceea8cc3e8
Created
April 19, 2023 06:10
-
-
Save krishnapuppala97/f1bbac85f2eb7a211d90dbbc4dd867a6 to your computer and use it in GitHub Desktop.
AWS CloudFormation template to create public/private subnets in a VPC with a NAT Gateway.
This file contains 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
# This creates a VPC with two public subnets and two private subnets in two Availability Zones, | |
# together with a NAT Gateway and associated routing. Change the Availability Zone locations as needed. | |
# Important: this configures various AWS services and there are costs associated with these services after the Free Tier usage. | |
# Please see the AWS Pricing pages for details. You are responsible for any AWS costs incurred. | |
# No warranty is implied in this example. | |
# Usage from command line: | |
# aws cloudformation --region <<YOUR-REGION>> create-stack --stack-name vpc- --template-body file://vpc-setup.yaml | |
Resources: | |
## VPC | |
PubPrivateVPC: | |
Type: AWS::EC2::VPC | |
Properties: | |
CidrBlock: 10.0.0.0/16 | |
## SUBNETS | |
PublicSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: !Ref PubPrivateVPC | |
AvailabilityZone: us-east-2a | |
CidrBlock: 10.0.1.0/24 | |
MapPublicIpOnLaunch: true | |
PublicSubnet2: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: !Ref PubPrivateVPC | |
AvailabilityZone: us-east-2b | |
CidrBlock: 10.0.2.0/24 | |
MapPublicIpOnLaunch: true | |
PrivateSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: !Ref PubPrivateVPC | |
AvailabilityZone: us-east-2a | |
CidrBlock: 10.0.3.0/24 | |
MapPublicIpOnLaunch: false | |
PrivateSubnet2: | |
Type: AWS::EC2::Subnet | |
Properties: | |
VpcId: !Ref PubPrivateVPC | |
AvailabilityZone: us-east-2b | |
CidrBlock: 10.0.4.0/24 | |
MapPublicIpOnLaunch: false | |
## INTERNET GATEWAY | |
InternetGateway: | |
Type: AWS::EC2::InternetGateway | |
GatewayToInternet: | |
Type: AWS::EC2::VPCGatewayAttachment | |
Properties: | |
VpcId: !Ref PubPrivateVPC | |
InternetGatewayId: !Ref InternetGateway | |
## PUBLIC ROUTING | |
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 | |
PublicSubnet2RouteTableAssociation: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
SubnetId: !Ref PublicSubnet2 | |
RouteTableId: !Ref PublicRouteTable | |
## NAT GATEWAY | |
NatGateway: | |
Type: AWS::EC2::NatGateway | |
DependsOn: NatPublicIP | |
Properties: | |
SubnetId: !Ref PublicSubnet1 | |
AllocationId: !GetAtt NatPublicIP.AllocationId | |
## ELASTIC IP | |
NatPublicIP: | |
Type: AWS::EC2::EIP | |
DependsOn: PubPrivateVPC | |
Properties: | |
Domain: vpc | |
## PRIVATE ROUTING | |
PrivateRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: !Ref PubPrivateVPC | |
PrivateRoute: | |
Type: AWS::EC2::Route | |
Properties: | |
NatGatewayId: !Ref NatGateway | |
RouteTableId: !Ref PrivateRouteTable | |
DestinationCidrBlock: 0.0.0.0/0 | |
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