Skip to content

Instantly share code, notes, and snippets.

@xelibrion
Last active September 14, 2017 04:26
Show Gist options
  • Select an option

  • Save xelibrion/c3f6753ce61c8906ff646f7ac0561920 to your computer and use it in GitHub Desktop.

Select an option

Save xelibrion/c3f6753ce61c8906ff646f7ac0561920 to your computer and use it in GitHub Desktop.
CloudFormation template for creating VPC and subnets
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
VpcCidr:
Type: String
Description: CIDR for the new VPC
VpcName:
Type: String
Description: Name for the new VPC
PrivateSubnetsRange:
Type: Number
Description: CIDR mask to set size of private subnets
Default: 18
PublicSubnetsRange:
Type: Number
Description: CIDR mask to set size of private subnets
Default: 20
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsSupport: true
EnableDnsHostnames: false
Tags:
- Key: Name
Value: !Ref VpcName
PrivateSubnetA:
Type: AWS::EC2::Subnet
Properties:
MapPublicIpOnLaunch: false
AvailabilityZone: !Join [ "", [ !Ref 'AWS::Region', a ] ]
CidrBlock: !Join [ "/", [ !Join [".", [!Select [0, !Split [".", !Ref VpcCidr]], !Select [1, !Split [".", !Ref VpcCidr]], 0, 0]], !Ref PrivateSubnetsRange ]]
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "private-subnet-a"]]
PrivateSubnetB:
Type: AWS::EC2::Subnet
Properties:
MapPublicIpOnLaunch: false
AvailabilityZone: !Join [ "", [ !Ref 'AWS::Region', b ] ]
CidrBlock: !Join [ "/", [ !Join [".", [!Select [0, !Split [".", !Ref VpcCidr]], !Select [1, !Split [".", !Ref VpcCidr]], 64, 0]], !Ref PrivateSubnetsRange ]]
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "private-subnet-b"]]
PrivateSubnetC:
Type: AWS::EC2::Subnet
Properties:
MapPublicIpOnLaunch: false
AvailabilityZone: !Join [ "", [ !Ref 'AWS::Region', c ] ]
CidrBlock: !Join [ "/", [ !Join [".", [!Select [0, !Split [".", !Ref VpcCidr]], !Select [1, !Split [".", !Ref VpcCidr]], 128, 0]], !Ref PrivateSubnetsRange ]]
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "private-subnet-c"]]
PublicSubnetA:
Type: AWS::EC2::Subnet
Properties:
MapPublicIpOnLaunch: false
AvailabilityZone: !Join [ "", [ !Ref 'AWS::Region', a ] ]
CidrBlock: !Join [ "/", [ !Join [".", [!Select [0, !Split [".", !Ref VpcCidr]], !Select [1, !Split [".", !Ref VpcCidr]], 192, 0]], !Ref PublicSubnetsRange ]]
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "public-subnet-a"]]
VpcInternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "igw"]]
VpcGatewayAttach:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref VpcInternetGateway
VpcId: !Ref VPC
NatElasticIP:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
NatGateway:
DependsOn: VpcGatewayAttach
Type: AWS::EC2::NatGateway
Properties:
AllocationId: !GetAtt NatElasticIP.AllocationId
SubnetId: !Ref PublicSubnetA
PublicSubnetsRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "public-rt"]]
PrivateSubnetsRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join ["-", [!Ref VpcName, "private-rt"]]
PublicSubnetsRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PublicSubnetsRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref VpcInternetGateway
PrivateSubnetsRoute:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref PrivateSubnetsRouteTable
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway
PrivateSubnetARouteTable:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateSubnetsRouteTable
SubnetId: !Ref PrivateSubnetA
PrivateSubnetBRouteTable:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateSubnetsRouteTable
SubnetId: !Ref PrivateSubnetB
PrivateSubnetCRouteTable:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateSubnetsRouteTable
SubnetId: !Ref PrivateSubnetC
PublicSubnetARouteTable:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PublicSubnetsRouteTable
SubnetId: !Ref PublicSubnetA
Outputs:
VpcId:
Value: !Ref VPC
PrivateSubnetA:
Value: !Ref PrivateSubnetA
PrivateSubnetB:
Value: !Ref PrivateSubnetB
PrivateSubnetC:
Value: !Ref PrivateSubnetC
PublicSubnetA:
Value: !Ref PublicSubnetA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment