Skip to content

Instantly share code, notes, and snippets.

@gmr
Last active November 4, 2022 13:59
Show Gist options
  • Save gmr/b3fc52d14d37f7ee1e00c752aef3627b to your computer and use it in GitHub Desktop.
Save gmr/b3fc52d14d37f7ee1e00c752aef3627b to your computer and use it in GitHub Desktop.
Demonstration CloudFormation YAML template for creating a VPC
AWSTemplateFormatVersion: '2010-09-09'
Description: VPC Network Stack
Metadata: {}
Mappings: {}
Conditions: {}
Outputs: {}
Parameters:
CidrBlock:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.0.0/16
Description: VPC CIDR Block (eg 10.0.0.0/16)
Type: String
AvailabilityZone1:
Description: The AvailabilityZone to use for the first subnet
Type: AWS::EC2::AvailabilityZone::Name
AvailabilityZone2:
Description: The AvailabilityZone to use for the second subnet
Type: AWS::EC2::AvailabilityZone::Name
SubnetCIDR1:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.0.0/24
Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
Type: String
SubnetCIDR2:
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}'
Default: 10.0.1.0/24
Description: VPC CIDR Block for the Public Subnet (eg 10.0.0.0/24)
Type: String
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock:
Ref: CidrBlock
EnableDnsHostnames: true
EnableDnsSupport: true
Tags:
- Key: Name
Value:
Ref: AWS::StackName
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value:
Ref: AWS::StackName
GatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId:
Ref: InternetGateway
VpcId:
Ref: VPC
RouteTable:
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: {Ref: 'AWS::StackName'}
VpcId:
Ref: VPC
PublicRoute:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId:
Ref: InternetGateway
RouteTableId:
Ref: RouteTable
Subnet1:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: {Ref: AvailabilityZone1}
CidrBlock: {Ref: SubnetCIDR1}
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value:
Fn::Join:
- '-'
- [{Ref: 'AWS::StackName'}, {Ref: AvailabilityZone1}]
VpcId: {Ref: VPC}
Subnet2:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone: {Ref: AvailabilityZone2}
CidrBlock: {Ref: SubnetCIDR2}
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value:
Fn::Join:
- '-'
- [{Ref: 'AWS::StackName'}, {Ref: AvailabilityZone2}]
VpcId: {Ref: VPC}
SubnetAssoc1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId:
Ref: RouteTable
SubnetId:
Ref: Subnet1
SubnetAssoc2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId:
Ref: RouteTable
SubnetId:
Ref: Subnet2
@dragonfly-net
Copy link

dragonfly-net commented Oct 29, 2019

How do you specify availability zone, say us-east-1e for any of the subnets?

aws cloudformation create-stack --region us-east-1 --template-body file://vpc.yml --stack-name vpc

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