Created
May 28, 2019 06:21
-
-
Save jcyuyi/baf34ee0b5aac5852ff5f69238a1bb91 to your computer and use it in GitHub Desktop.
VPC with public and private subnet
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
AWSTemplateFormatVersion: "2010-09-09" | |
Description: 'Core network & security group stack template' | |
Parameters: | |
app: | |
Description: 'App name tag' | |
Type: String | |
env: | |
Description: 'Environment tag' | |
Type: String | |
AllowedPattern: 'dev|stg|prd' | |
cidrBlock: | |
Description: 'VPC CIDR Block (e.g: 10.0.0.0/16)' | |
Type: String | |
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}' | |
cidrBlockPublic1: | |
Description: 'Public 1 subnet CIDR Block (e.g: 10.0.1.0/24)' | |
Type: String | |
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}' | |
cidrBlockPublic2: | |
Description: 'Public 2 subnet CIDR Block (e.g: 10.0.2.0/24)' | |
Type: String | |
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}' | |
cidrBlockPrivate1: | |
Description: 'Private 1 subnet CIDR Block (e.g: 10.0.3.0/24)' | |
Type: String | |
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}' | |
cidrBlockPrivate2: | |
Description: 'Private 2 subnet CIDR Block (e.g: 10.0.4.0/24)' | |
Type: String | |
AllowedPattern: '((\d{1,3})\.){3}\d{1,3}/\d{1,2}' | |
az1: | |
Description: The AvailabilityZone to use for the first subnet | |
Type: AWS::EC2::AvailabilityZone::Name | |
az2: | |
Description: The AvailabilityZone to use for the second subnet | |
Type: AWS::EC2::AvailabilityZone::Name | |
Resources: | |
vpc: | |
Type: AWS::EC2::VPC | |
Properties: | |
CidrBlock: !Ref cidrBlock | |
Tags: | |
- Key: 'Name' | |
Value: !Join [ "-", [ !Ref 'AWS::StackName', vpc ] ] | |
- Key: 'app' | |
Value: !Ref app | |
- Key: 'env' | |
Value: !Ref env | |
internetGateway: | |
Type: AWS::EC2::InternetGateway | |
Properties: | |
Tags: | |
- Key: 'Name' | |
Value: !Join [ "-", [ !Ref 'AWS::StackName', internetGateway ] ] | |
- Key: 'app' | |
Value: !Ref app | |
- Key: 'env' | |
Value: !Ref env | |
gatewayAttachment: | |
Type: AWS::EC2::VPCGatewayAttachment | |
Properties: | |
InternetGatewayId: !Ref internetGateway | |
VpcId: !Ref vpc | |
# ---- public subnet ---- | |
publicRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: !Ref vpc | |
publicRoute: | |
Type: AWS::EC2::Route | |
Properties: | |
DestinationCidrBlock: 0.0.0.0/0 | |
GatewayId: !Ref internetGateway | |
RouteTableId: !Ref publicRouteTable | |
publicSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
AvailabilityZone: !Ref az1 | |
CidrBlock: !Ref cidrBlockPublic1 | |
MapPublicIpOnLaunch: true | |
VpcId: !Ref vpc | |
publicSubnetAssoc1: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
RouteTableId: !Ref publicRouteTable | |
SubnetId: !Ref publicSubnet1 | |
publicSubnet2: | |
Type: AWS::EC2::Subnet | |
Properties: | |
AvailabilityZone: !Ref az2 | |
CidrBlock: !Ref cidrBlockPublic2 | |
MapPublicIpOnLaunch: true | |
VpcId: !Ref vpc | |
publicSubnetAssoc2: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
RouteTableId: !Ref publicRouteTable | |
SubnetId: !Ref publicSubnet2 | |
# ---- private subnet ---- | |
privateRouteTable: | |
Type: AWS::EC2::RouteTable | |
Properties: | |
VpcId: !Ref vpc | |
privateSubnet1: | |
Type: AWS::EC2::Subnet | |
Properties: | |
AvailabilityZone: !Ref az1 | |
CidrBlock: !Ref cidrBlockPrivate1 | |
VpcId: !Ref vpc | |
privateSubnetAssoc1: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
RouteTableId: !Ref privateRouteTable | |
SubnetId: !Ref privateSubnet1 | |
privateSubnet2: | |
Type: AWS::EC2::Subnet | |
Properties: | |
AvailabilityZone: !Ref az2 | |
CidrBlock: !Ref cidrBlockPrivate2 | |
VpcId: !Ref vpc | |
privateSubnetAssoc2: | |
Type: AWS::EC2::SubnetRouteTableAssociation | |
Properties: | |
RouteTableId: !Ref privateRouteTable | |
SubnetId: !Ref privateSubnet2 | |
Outputs: | |
app: | |
Description: 'App name tag' | |
Value: !Ref app | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", app ] ] | |
env: | |
Description: 'Environment tag' | |
Value: !Ref env | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", env ] ] | |
vpc: | |
Description: 'VPC ID' | |
Value: !Ref vpc | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", vpc ] ] | |
publicSubnet1: | |
Description: 'public subnet1' | |
Value: !Ref publicSubnet1 | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", publicSubnet1 ] ] | |
publicSubnet2: | |
Description: 'public subnet2' | |
Value: !Ref publicSubnet2 | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", publicSubnet2 ] ] | |
publicRouteTable: | |
Description: 'route table for public subnets' | |
Value: !Ref publicRouteTable | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", publicRouteTable]] | |
privateSubnet1: | |
Description: 'private subnet1' | |
Value: !Ref privateSubnet1 | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", privateSubnet1 ] ] | |
privateSubnet2: | |
Description: 'private subnet2' | |
Value: !Ref privateSubnet2 | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", privateSubnet2 ] ] | |
privateRouteTable: | |
Description: 'route table for private subnets' | |
Value: !Ref privateRouteTable | |
Export: | |
Name: !Join [ "-", [ !Ref "AWS::StackName", privateRouteTable ] ] | |
vpcCidr: | |
Description: 'VPC CidrBlock' | |
Value: !GetAtt vpc.CidrBlock |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment