Skip to content

Instantly share code, notes, and snippets.

@kencoba
Created September 22, 2024 07:58
Show Gist options
  • Save kencoba/1aa4e96d7ee6da4365236f2f86b13c1e to your computer and use it in GitHub Desktop.
Save kencoba/1aa4e96d7ee6da4365236f2f86b13c1e to your computer and use it in GitHub Desktop.
AWS CloudFormation template that create a VPC with a public subnet and a private subnet.
AWSTemplateFormatVersion: '2010-09-09'
Description: CloudFormation template to create a VPC with one public subnet and one private subnet, with customizable CIDR blocks and a common prefix for all resource names.
Parameters:
VpcCidr:
Type: String
Default: 10.0.0.0/16
Description: CIDR block for the VPC
PublicSubnetCidr:
Type: String
Default: 10.0.1.0/24
Description: CIDR block for the public subnet
PrivateSubnetCidr:
Type: String
Default: 10.0.2.0/24
Description: CIDR block for the private subnet
Prefix:
Type: String
Default: MyApp
Description: Prefix for all resource names
Resources:
# Create the VPC
MyVPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Sub "${Prefix}-VPC"
# Create the Internet Gateway
MyInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Sub "${Prefix}-InternetGateway"
# Attach the Internet Gateway to the VPC
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref MyVPC
InternetGatewayId: !Ref MyInternetGateway
# Create the Public Subnet
MyPublicSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: !Ref PublicSubnetCidr
MapPublicIpOnLaunch: true
AvailabilityZone: !Select [ 0, !GetAZs '' ]
Tags:
- Key: Name
Value: !Sub "${Prefix}-PublicSubnet"
# Create the Private Subnet
MyPrivateSubnet:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref MyVPC
CidrBlock: !Ref PrivateSubnetCidr
AvailabilityZone: !Select [ 0, !GetAZs '' ]
Tags:
- Key: Name
Value: !Sub "${Prefix}-PrivateSubnet"
# Create the Public Route Table
MyPublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: !Sub "${Prefix}-PublicRouteTable"
# Create a route in the Public Route Table to the Internet Gateway
PublicRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref MyPublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref MyInternetGateway
# Associate the Public Subnet with the Public Route Table
PublicSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref MyPublicSubnet
RouteTableId: !Ref MyPublicRouteTable
# Create the NAT Gateway in the public subnet
MyNATGatewayEIP:
Type: AWS::EC2::EIP
DependsOn: AttachGateway
Properties:
Domain: vpc
MyNATGateway:
Type: AWS::EC2::NatGateway
Properties:
SubnetId: !Ref MyPublicSubnet
AllocationId: !GetAtt MyNATGatewayEIP.AllocationId
Tags:
- Key: Name
Value: !Sub "${Prefix}-NATGateway"
# Create the Private Route Table
MyPrivateRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref MyVPC
Tags:
- Key: Name
Value: !Sub "${Prefix}-PrivateRouteTable"
# Create a route in the Private Route Table to the NAT Gateway
PrivateRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref MyPrivateRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref MyNATGateway
# Associate the Private Subnet with the Private Route Table
PrivateSubnetRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref MyPrivateSubnet
RouteTableId: !Ref MyPrivateRouteTable
Outputs:
VPCId:
Description: VPC ID
Value: !Ref MyVPC
Export:
Name: !Sub "${Prefix}-VPCId"
VPCArn:
Description: VPC ARN
Value: !Sub "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/${MyVPC}"
Export:
Name: !Sub "${Prefix}-VPCArn"
PublicSubnetId:
Description: Public Subnet ID
Value: !Ref MyPublicSubnet
Export:
Name: !Sub "${Prefix}-PublicSubnetId"
PublicSubnetArn:
Description: Public Subnet ARN
Value: !Sub "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:subnet/${MyPublicSubnet}"
Export:
Name: !Sub "${Prefix}-PublicSubnetArn"
PrivateSubnetId:
Description: Private Subnet ID
Value: !Ref MyPrivateSubnet
Export:
Name: !Sub "${Prefix}-PrivateSubnetId"
PrivateSubnetArn:
Description: Private Subnet ARN
Value: !Sub "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:subnet/${MyPrivateSubnet}"
Export:
Name: !Sub "${Prefix}-PrivateSubnetArn"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment