Thursday, February 29, 2024
HomeSoftware EngineeringCreate a VPC in CloudFormation

Create a VPC in CloudFormation


It’s very simple to deploy a VPC utilizing CloudFormation:

AWSTemplateFormatVersion: 2010-09-09
Description: Deploy a VPC

Sources:
  VPC:
    Kind: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      Tags:
      - Key: Identify
        Worth: Lab VPC
<meta charset="utf-8">Outputs:
  VPC:
    Description: VPC
    Worth: !Ref VPC

This may also output the created VPC useful resource data.

However what in case you additionally need to create Subnets and an connected Web Gateway?

AWSTemplateFormatVersion: 2010-09-09
Description: Deploy a VPC

Sources:
  VPC:
    Kind: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      Tags:
      - Key: Identify
        Worth: Lab VPC

  InternetGateway:
    Kind: AWS::EC2::InternetGateway
    Properties:
      Tags:
      - Key: Identify
        Worth: Lab Web Gateway

  AttachGateway:
    Kind: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway

  PublicSubnet1:
    Kind: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: !Choose 
        - '0'
        - !GetAZs ''
      Tags:
        - Key: Identify
          Worth: Public Subnet 1

  PrivateSubnet1:
    Kind: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      CidrBlock: 10.0.1.0/24
      AvailabilityZone: !Choose 
        - '0'
        - !GetAZs ''
      Tags:
        - Key: Identify
          Worth: Personal Subnet 1

  PublicRouteTable:
    Kind: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Identify
          Worth: Public Route Desk

  PublicRoute:
    Kind: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref InternetGateway

  PublicSubnetRouteTableAssociation1:
    Kind: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubnet1
      RouteTableId: !Ref PublicRouteTable

  PrivateRouteTable:
    Kind: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
      - Key: Identify
        Worth: Personal Route Desk

  PrivateSubnetRouteTableAssociation1:
    Kind: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PrivateSubnet1
      RouteTableId: !Ref PrivateRouteTable

Outputs:
  VPC:
    Description: VPC
    Worth: !Ref VPC
  AZ1:
    Description: Availability Zone 1
    Worth: !GetAtt 
      - PublicSubnet1
      - AvailabilityZone
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments