Network with Public Subnets

AWSTemplateFormatVersion: 2010-09-09
Description: VPC with two public subnets.

Overview

Network-Public.png

This CloudFormation template creates a VPC with two public subnets.

Parameters

Parameters:
Param Value
DeploymentName test

DeploymentName

  DeploymentName:
    Type: String
    Description: A name for this deployment

A deployment is a deployed application, potentially comprised of many CloudFormation stacks. This is sometimes called an "environment", but that is an overloaded and confusing term. Use the DeploymentName to indicate which logical deployment a stack belongs to.

If a deployment is completely specified by exactly one CloudFormation template, the DeploymentName and the AWS::StackName refer to the same things. In that case, consider not using a DeploymentName parameter.

  VPCCIDR:
    Description: CIDR range for this VPC
    Type: String
    Default: 10.192.0.0/16

  PublicSubnet1CIDR:
    Description: CIDR range for public subnet in 1st AZ
    Type: String
    Default: 10.192.10.0/24

  PublicSubnet2CIDR:
    Description: CIDR range for public subnet in 2nd AZ
    Type: String
    Default: 10.192.11.0/24

Resources

Resources:

VPC

  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VPCCIDR
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Sub "${DeploymentName}"

Internet Gateway

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub "${DeploymentName}"

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

Route Table

  PublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub "${DeploymentName} Public"
  DefaultPublicRoute:
    Type: AWS::EC2::Route
    DependsOn: InternetGatewayAttachment
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

Subnets

  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [0, !GetAZs ""]
      CidrBlock: !Ref PublicSubnet1CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub "${DeploymentName} Public (AZ1)"

  PublicSubnet1RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet1
  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [1, !GetAZs  ""]
      CidrBlock: !Ref PublicSubnet2CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub "${DeploymentName} Public (AZ2)"

  PublicSubnet2RouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref PublicRouteTable
      SubnetId: !Ref PublicSubnet2

Outputs

Outputs:
  VpcId:
    Description: A reference to the created VPC
    Value: !Ref VPC
    Export:
      Name: !Sub "${DeploymentName}-VpcId"

  VpcCidr:
    Description: The VPC CIDR range
    Value: !GetAtt VPC.CidrBlock
    Export:
      Name: !Sub "${DeploymentName}-VpcCidr"

  PublicSubnet1:
    Description: The public subnet in the 1st AZ
    Value: !Ref PublicSubnet1
    Export:
      Name: !Sub "${DeploymentName}-PublicSubnet1"

  PublicSubnet2:
    Description: The public subnet in the 2nd AZ
    Value: !Ref PublicSubnet2
    Export:
      Name: !Sub "${DeploymentName}-PublicSubnet2"