{CloudFormation}VPC作成

https://qiita.com/tyoshitake/items/c5176c0ef4de8d7cf5d8

作るもの
VPC 172.20.0.0/16
インターネットゲートウェイ
サブネット 172.20.1.0/24
ルートテーブル

※ネットワークACLはデフォルトで作成される全許可を使用


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version

-- 1.2 jqインストール
sudo yum -y install jq

-- 2. スタック作成

vim a.yaml

AWSTemplateFormatVersion: "2010-09-09"
Description: Provision VPC

Resources:
  # 1. 
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 172.20.0.0/16
      Tags:
        - Key: Name
          Value: VPC
  # 2. 
  IGW:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: IGW
  # 3. IGWをVPCにアタッチ
  VPCIGW:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref IGW
  # 4. 
  PublicSubneta:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: ap-northeast-1a
      VpcId: !Ref VPC
      CidrBlock: 172.20.1.0/24
      Tags:
        - Key: Name
          Value: PublicSubneta
  # 5. 
  PublicRTa:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: PublicRTa
  # 6. ルーティングテーブル設定
  PublicRTa001:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref PublicRTa
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref IGW
  # 7. ルートテーブルをサブネットに関連付け
  PublicSubnetaPublicRTa:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref PublicSubneta
      RouteTableId: !Ref PublicRTa

 

aws cloudformation validate-template \
--template-body file://a.yaml


aws cloudformation create-stack \
--stack-name stack01 \
--template-body file://a.yaml

 

VPC作成時にメインのルートテーブルが自動で作成される

 

-- 3. スタック一覧

aws cloudformation list-stacks

aws cloudformation describe-stacks \
--stack-name stack01

-- 4. スタックリソース一覧

aws cloudformation describe-stack-resources \
--stack-name stack01

 

-- 5. クリーンアップ

-- スタック削除
aws cloudformation delete-stack \
--stack-name stack01

aws ec2 describe-vpcs
aws ec2 describe-internet-gateways
aws ec2 describe-subnets
aws ec2 describe-route-tables