0
1

#1 AWS CLIでVPCを構築する

Last updated at Posted at 2023-07-04

はじめに

AWS CLIでVPCを構築する手順です。構成図は以下の通りです。
vpc.png

手順

以下の流れで環境を構築します。

  • VPCの作成
  • サブネットの作成
  • SGの作成
  • ルートテーブルの作成
  • ルートテーブルのアタッチ
  • IGWの作成
  • IGWのアタッチ
  • ルートテーブルの編集

1.VPCの作成

設定値の指定

#VPCのタグ設定
EC2_VPC_TAG_NAME='handson-test'
#VPCのアドレス指定
EC2_VPC_CIDR='10.0.0.0/16'

設定値を確認します。

cat << END

  # 0. AWS_REGION:"ap-northeast-1"
       AWS_REGION="${AWS_REGION}"
  # 1. EC2_VPC_TAG_NAME:"handson-test"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_VPC_CIDR:"10.0.0.0/16"
       EC2_VPC_CIDR="${EC2_VPC_CIDR}"

END

処理の実行

  • タグ文字列の生成は
    ResourceType=vpc,Tags=[{Key=Name,Value=<指定した設定値>}]とします。
  • VPCの作成は
    create-vpcコマンド
    --cidr-brock --tag-specificationsオプションを使います。
#タグ文字列の生成
string_ec2_vpc_tag="ResourceType=vpc,Tags=[{Key=Name,Value=${EC2_VPC_TAG_NAME}}]" \
   && echo ${string_ec2_vpc_tag}

#VPCの作成(変数の確認)
cat << END
 
   # EC2_VPC_CIDR:"10.0.0.0/16"
     EC2_VPC_CIDR="${EC2_VPC_CIDR}"
   # string_ec2_vpc_tag:"ResourceType=vpc,Tags=[{Key=Name,Value=handson-test}]"
     string_ec2_vpc_tag="${string_ec2_vpc_tag}"
 
END

#VPCの作成
aws ec2 create-vpc \
  --cidr-block ${EC2_VPC_CIDR} \
  --tag-specifications ${string_ec2_vpc_tag}

出力例

{
    "Vpc": {
        "CidrBlock": "10.0.0.0/16",
        "DhcpOptionsId": "dopt-xxxxxxxx",
        "State": "pending",
        "VpcId": "vpc-xxxxxxxx",
        "OwnerId": "xxxxxxxx",
        "InstanceTenancy": "default",
        "Ipv6CidrBlockAssociationSet": [],
        "CidrBlockAssociationSet": [
            {
                "AssociationId": "vpc-cidr-assoc-xxxxxxxx",
                "CidrBlock": "10.0.0.0/16",
                "CidrBlockState": {
                    "State": "associated"
                }
            }
        ],
        "IsDefault": false,
        "Tags": [
            {
                "Key": "Name",
                "Value": "handson-test"
            }
        ]
    }
}

作業後の確認

  • describe-vpcsコマンドで確認します。
    オプション指定により、出力結果を絞り込みます。
  • --filters --queryでリソースをフィルタリングします。
  • --outputでコマンド出力の書式設定スタイルを決めます。
    【参考:コマンドリファレンス】
#作成したVPCの確認
aws ec2 describe-vpcs \
  --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME} \
  --query 'Vpcs[].Tags[?Key == `Name`].Value' \
  --output text
#作成したVPCのCIDR確認
aws ec2 describe-vpcs \
  --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME} \
  --query "Vpcs[].CidrBlock" \
  --output text

2.サブネットの作成

4つのサブネットを作成します。手順は例として、10.0.2.0/24のサブネットを作成しています。

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#VPCのタグ名
EC2_VPC_TAG_NAME='handson-test'

#サブネットのタグ名
EC2_SUBNET_TAG_NAME='handson-test-private-1'

#アドレスレンジ
EC2_SUBNET_CIDR='10.0.2.0/24'

#サブネットの配置AZ
EC2_AZ_CODE="a"

EC2_AZ_NAME="${AWS_DEFAULT_REGION}${EC2_AZ_CODE}" \
  && echo ${EC2_AZ_NAME}

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_VPC_TAG_NAME:"handson-test"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_SUBNET_TAG_NAME:"handson-test-private-1"
       EC2_SUBNET_TAG_NAME="${EC2_SUBNET_TAG_NAME}"
  # 3. EC2_SUBNET_CIDR:"10.0.2.0/24"
       EC2_SUBNET_CIDR="${EC2_SUBNET_CIDR}"
  # 4. EC2_AZ_NAME:"ap-northeast-1a"
       EC2_AZ_NAME="${EC2_AZ_NAME}"

END

処理の実行

  • create-subnetコマンドを使います。
    オプションを指定し、出力結果を絞り込みます。
  • --tag-specificationsでリソース作成時にタグを生成します。
#VPCIDの取得
EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

#タグ文字列の生成
STRING_EC2_SUBNET_TAG="ResourceType=subnet,Tags=[{Key=Name,Value=${EC2_SUBNET_TAG_NAME}}]" \
  && echo ${STRING_EC2_SUBNET_TAG}

#サブネットの作成(変数の確認)
cat << END

  # EC2_VPC_ID:"vpc-xxxx"
    EC2_VPC_ID="${EC2_VPC_ID}"
  # EC2_SUBNET_CIDR:"10.0.2.0/24"
    EC2_SUBNET_CIDR="${EC2_SUBNET_CIDR}"
  # EC2_AZ_NAME:"ap-northeast-1a"
    EC2_AZ_NAME="${EC2_AZ_NAME}"
  # STRING_EC2_SUBNET_TAG:"ResourceType=subnet,Tags=[{Key=Name,Value=handson-test-private-1}]"
    STRING_EC2_SUBNET_TAG="${STRING_EC2_SUBNET_TAG}"

END

#サブネットの作成
aws ec2 create-subnet \
  --vpc-id ${EC2_VPC_ID} \
  --cidr-block ${EC2_SUBNET_CIDR} \
  --availability-zone ${EC2_AZ_NAME} \
  --tag-specifications ${STRING_EC2_SUBNET_TAG}

出力例

{
    "Subnet": {
        "AvailabilityZone": "ap-northeast-1a",
        "AvailabilityZoneId": "apne1-az4",
        "AvailableIpAddressCount": 251,
        "CidrBlock": "10.0.2.0/24",
        "DefaultForAz": false,
        "MapPublicIpOnLaunch": false,
        "State": "available",
        "SubnetId": "subnet-xxxxxxxx",
        "VpcId": "vpc-xxxxxxxx",
        "OwnerId": "xxxxxxxx",
        "AssignIpv6AddressOnCreation": false,
        "Ipv6CidrBlockAssociationSet": [],
        "Tags": [
            {
                "Key": "Name",
                "Value": "handson-test-private-1"
            }
        ],
        "SubnetArn": "arn:aws:ec2:ap-northeast-1:xxxxxxxx:subnet/subnet-xxxxxxxx",
        "EnableDns64": false,
        "Ipv6Native": false,
        "PrivateDnsNameOptionsOnLaunch": {
            "HostnameType": "ip-name",
            "EnableResourceNameDnsARecord": false,
            "EnableResourceNameDnsAAAARecord": false
        }
    }
}

作業後の確認

  • describe-subnetsで確認します。
  • それぞれ--queryオプションで絞り込むことで、タグ名 サブネット範囲 AZ名を出力します。
aws ec2 describe-subnets \
  --filters Name=vpc-id,Values=${EC2_VPC_ID} \
            Name=tag:Name,Values=${EC2_SUBNET_TAG_NAME}  \
  --query 'Subnets[].Tags[?Key == `Name`].Value' \
  --output text
  
  
aws ec2 describe-subnets \
  --filters Name=vpc-id,Values=${EC2_VPC_ID} \
            Name=tag:Name,Values=${EC2_SUBNET_TAG_NAME}  \
  --query "Subnets[].CidrBlock" \
  --output text
  
  
aws ec2 describe-subnets \
  --filters Name=vpc-id,Values=${EC2_VPC_ID} \
            Name=tag:Name,Values=${EC2_SUBNET_TAG_NAME}  \
  --query "Subnets[].AvailabilityZone" \
  --output text

3.セキュリティグループの作成

WEB用、DB用、ELB用の合計3つのセキュリティグループを作成します。

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#VPCのタグ名
EC2_VPC_TAG_NAME='handson-test'

#SG名
EC2_SECURITY_GROUP_NAME='handson-cli-db-sg'

#SGの説明
EC2_SECURITY_GROUP_DESCRIPTION='handson-cli-db-SecurityGroup.'

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_VPC_TAG_NAME:"handson-test"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_SECURITY_GROUP_NAME:"handson-cli-db-sg"
       EC2_SECURITY_GROUP_NAME="${EC2_SECURITY_GROUP_NAME}"
  # 3. EC2_SECURITY_GROUP_DESCRIPTION:"handson-cli-db-SecurityGroup."
       EC2_SECURITY_GROUP_DESCRIPTION="${EC2_SECURITY_GROUP_DESCRIPTION}"

END

処理の実行

  • create-security-groupを使います。
#VPCIDの取得
EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

#ここまでの設定値の確認
cat << END

  # EC2_SECURITY_GROUP_NAME:"handson-cli-db-sg"
    EC2_SECURITY_GROUP_NAME="${EC2_SECURITY_GROUP_NAME}"
  # EC2_SECURITY_GROUP_DESCRIPTION:"handson-cli-db- SecurityGroup."
    EC2_SECURITY_GROUP_DESCRIPTION="${EC2_SECURITY_GROUP_DESCRIPTION}"
  # EC2_VPC_ID:"vpc-xxxxxxxx"
    EC2_VPC_ID="${EC2_VPC_ID}"

END

#SGの作成
aws ec2 create-security-group \
  --group-name ${EC2_SECURITY_GROUP_NAME} \
  --description "${EC2_SECURITY_GROUP_DESCRIPTION}" \
  --vpc-id ${EC2_VPC_ID}

作業後の確認

  • describe-security-groupsで確認します。
  • 先程と同様、filter queryで情報を絞りこみ、outputで出力形式を決めます。
aws ec2 describe-security-groups \
  --filter Name=vpc-id,Values=${EC2_VPC_ID} \
    Name=group-name,Values=${EC2_SECURITY_GROUP_NAME} \
  --query 'SecurityGroups[].GroupName' \
  --output text

ルールの追加

  • aws ec2 authorize-security-group-ingressで、ルールを追加します。
  • パブリックサブネットプライベートサブネットELBの3つに割り当てる、それぞれのSGにルールを追加する手順を記載しています。
#ルールの追加(WEB)
aws ec2 authorize-security-group-ingress --group-id sg-xxxx --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-xxxx --protocol tcp --port 80 --cidr 0.0.0.0/0

#ルールの追加(DB)
aws ec2 authorize-security-group-ingress --group-id sg-xxxx --protocol tcp --port 3306 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-xxxx --protocol tcp --port 80 --cidr 0.0.0.0/0

#ルールの追加(ELB)
aws ec2 authorize-security-group-ingress --group-id sg-xxxx --protocol tcp --port 80 --cidr 0.0.0.0/0

4.ルートテーブルの作成

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#VPCのタグ名
EC2_VPC_TAG_NAME='handson-test'

#ルートテーブルのタグ名
EC2_ROUTE_TABLE_TAG_NAME='handson-test-private-routetable'

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_VPC_TAG_NAME:"handson-test"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_ROUTE_TABLE_TAG_NAME:"handson-test-private-routetable"
       EC2_ROUTE_TABLE_TAG_NAME="${EC2_ROUTE_TABLE_TAG_NAME}"

END

処理の実行

  • create-route-tableを使います。
#VPCIDの取得
EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

#タグ文字列の生成
STRING_EC2_ROUTE_TABLE_TAG="ResourceType=route-table,Tags=[{Key=Name,Value=${EC2_ROUTE_TABLE_TAG_NAME}}]" \
  && echo ${STRING_EC2_ROUTE_TABLE_TAG}

#ルートテーブルの作成(変数の確認)
cat << END

  # EC2_VPC_ID:"vpc-xxxxxxxxxxxxxxxxx"
    EC2_VPC_ID="${EC2_VPC_ID}"
  # STRING_EC2_ROUTE_TABLE_TAG:"ResourceType=route-table,Tags=[{Key=Name,Value=handson-test-private-routetable}]"
    STRING_EC2_ROUTE_TABLE_TAG="${STRING_EC2_ROUTE_TABLE_TAG}"

END

#ルートテーブルの作成
aws ec2 create-route-table \
  --vpc-id ${EC2_VPC_ID} \
  --tag-specifications ${STRING_EC2_ROUTE_TABLE_TAG}

作業後の確認

  • aws ec2 describe-route-tablesで確認します。
aws ec2 describe-route-tables \
  --filters Name=vpc-id,Values=${EC2_VPC_ID} \
            Name=tag:Name,Values=${EC2_ROUTE_TABLE_TAG_NAME}  \
  --query "RouteTables[].Tags[?Key == \`Name\`].Value" \
  --output text

5.ルートテーブルのアタッチ

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#VPCのタグ名
EC2_VPC_TAG_NAME='handson-test'

#ルートテーブルのタグ名
EC2_ROUTE_TABLE_TAG_NAME='handson-test-private-routetable'

#関連付けたいサブネットのタグ名
EC2_SUBNET_TAG_NAME='handson-test-private-2'

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_VPC_TAG_NAME:"handson-test"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_ROUTE_TABLE_TAG_NAME:"handson-test-public-routetable"
       EC2_ROUTE_TABLE_TAG_NAME="${EC2_ROUTE_TABLE_TAG_NAME}"
  # 3. EC2_SUBNET_TAG_NAME:"handson-test-public-1"
       EC2_SUBNET_TAG_NAME="${EC2_SUBNET_TAG_NAME}"

END

処理の実行

  • associate-route-tableを使います。
  • オプションで--subnet-id --route-table-idを指定します。
#VPCIDの取得
EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

#ルートテーブルIDの取得
EC2_ROUTE_TABLE_ID=$( \
  aws ec2 describe-route-tables \
    --filters Name=vpc-id,Values=${EC2_VPC_ID} \
              Name=tag:Name,Values=${EC2_ROUTE_TABLE_TAG_NAME}  \
    --query "RouteTables[].RouteTableId" \
    --output text \
) \
  && echo ${EC2_ROUTE_TABLE_ID}

#サブネットIDの取得
EC2_SUBNET_ID=$( \
  aws ec2 describe-subnets \
    --filters Name=vpc-id,Values=${EC2_VPC_ID} \
              Name=tag:Name,Values=${EC2_SUBNET_TAG_NAME}  \
    --query "Subnets[].SubnetId" \
    --output text \
) \
  && echo ${EC2_SUBNET_ID}

#テーブルとサブネットの関連付け(変数の確認)
cat << END

  # EC2_SUBNET_ID:"subnet-xxxxxxxxxxxxxxxxx"
    EC2_SUBNET_ID="${EC2_SUBNET_ID}"
  # EC2_ROUTE_TABLE_ID:"rtb-xxxxxxxxxxxxxxxxx"
    EC2_ROUTE_TABLE_ID="${EC2_ROUTE_TABLE_ID}"

END

#テーブルとサブネットの関連付け
aws ec2 associate-route-table \
  --subnet-id ${EC2_SUBNET_ID} \
  --route-table-id ${EC2_ROUTE_TABLE_ID}

完了確認

  • aws ec2 describe-route-tableで確認します。
aws ec2 describe-route-tables \
  --route-table-ids ${EC2_ROUTE_TABLE_ID} \
  --query "RouteTables[].Associations[?SubnetId == \`${EC2_SUBNET_ID}\`].RouteTableAssociationId" \
  --output text

6.インターネットゲートウェイの作成

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#IGWのタグ名
EC2_INTERNET_GATEWAY_TAG_NAME='handson-test-igw'

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_INTERNET_GATEWAY_TAG_NAME:"handson-test-igw"
       EC2_INTERNET_GATEWAY_TAG_NAME="${EC2_INTERNET_GATEWAY_TAG_NAME}"

END

処理の実行

  • create-internet-gatewayを使います。
  • オプションで--tag-specificationsを入力し、タグを作成します。
#変数の設定
STRING_EC2_INTERNET_GATEWAY_TAG="ResourceType=internet-gateway,Tags=[{Key=Name,Value=${EC2_INTERNET_GATEWAY_TAG_NAME}}]" \
  && echo ${STRING_EC2_INTERNET_GATEWAY_TAG}


#IGWの作成(変数の確認)
cat << END

  # STRING_EC2_INTERNET_GATEWAY_TAG:"ResourceType=internet-gateway,Tags=[{Key=Name,Value=handson-cli-igw}]"
    STRING_EC2_INTERNET_GATEWAY_TAG="${STRING_EC2_INTERNET_GATEWAY_TAG}"

END

#IGWの作成
aws ec2 create-internet-gateway \
  --tag-specifications ${STRING_EC2_INTERNET_GATEWAY_TAG}

完了確認

  • aws ec2 describe-internet-gatewaysで確認します。
aws ec2 describe-internet-gateways \
  --filters Name=tag:Name,Values=${EC2_INTERNET_GATEWAY_TAG_NAME}  \
  --query "InternetGateways[].Tags[].Value" \
  --output text

7.インターネットゲートウェイのアタッチ

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#VPCタグ名
EC2_VPC_TAG_NAME='handson-test'

#IGWタグ名
EC2_INTERNET_GATEWAY_TAG_NAME='handson-test-igw'

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_VPC_TAG_NAME:"handson-test-vpc"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_INTERNET_GATEWAY_TAG_NAME:"handson-test-igw"
       EC2_INTERNET_GATEWAY_TAG_NAME="${EC2_INTERNET_GATEWAY_TAG_NAME}"

END

処理の実行

  • attach-internet-gatewayを使います。
  • オプションで--vpc-id --internet-gateway-idを指定します。
#VPCIDの取得
EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

#IGWのID取得
EC2_INTERNET_GATEWAY_ID=$( \
  aws ec2 describe-internet-gateways \
    --filters Name=tag:Name,Values=${EC2_INTERNET_GATEWAY_TAG_NAME} \
    --query "InternetGateways[].InternetGatewayId" \
    --output text \
) \
  && echo ${EC2_INTERNET_GATEWAY_ID}

#アタッチ(変数の確認)
cat << END

  # EC2_VPC_ID:"vpc-xxxxxxxxxxxxxxxxx"
    EC2_VPC_ID="${EC2_VPC_ID}"
  # EC2_INTERNET_GATEWAY_ID:"igw-xxxxxxxxxxxxxxxxx"
    EC2_INTERNET_GATEWAY_ID="${EC2_INTERNET_GATEWAY_ID}"

END

#アタッチ
aws ec2 attach-internet-gateway \
  --vpc-id ${EC2_VPC_ID} \
  --internet-gateway-id ${EC2_INTERNET_GATEWAY_ID}

作業後の確認

  • aws ec2 describe-internet-gatewaysで確認します。
aws ec2 describe-internet-gateways \
  --filters Name=tag:Name,Values=${EC2_INTERNET_GATEWAY_TAG_NAME}  \
  --query "InternetGateways[].Attachments[?VpcId == \`${EC2_VPC_ID}\`].VpcId" \
  --output text

8.ルートテーブルの編集

設定値の指定

#リージョンの指定
export AWS_DEFAULT_REGION='ap-northeast-1'

#VPCタグ名
EC2_VPC_TAG_NAME='handson-test'

#ルートテーブルのタグ名
EC2_ROUTE_TABLE_TAG_NAME='handson-test-public-routetable'

#宛先アドレス
EC2_ROUTE_DESTINATION_CIDR='0.0.0.0/0'

#IGWのタグ名
EC2_INTERNET_GATEWAY_TAG_NAME='handson-test-igw'

設定値を確認します。

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. EC2_VPC_TAG_NAME:"handson-cli-vpc"
       EC2_VPC_TAG_NAME="${EC2_VPC_TAG_NAME}"
  # 2. EC2_ROUTE_TABLE_TAG_NAME:"handson-cli-public-routetable"
       EC2_ROUTE_TABLE_TAG_NAME="${EC2_ROUTE_TABLE_TAG_NAME}"
  # 3. EC2_ROUTE_DESTINATION_CIDR:"0.0.0.0/0"
       EC2_ROUTE_DESTINATION_CIDR="${EC2_ROUTE_DESTINATION_CIDR}"
  # 4. EC2_INTERNET_GATEWAY_TAG_NAME:"handson-cli-igw"
       EC2_INTERNET_GATEWAY_TAG_NAME="${EC2_INTERNET_GATEWAY_TAG_NAME}"

END

処理の実行

  • create-routeを使います。
  • --route-table-idでルートテーブルを指定します。
  • --destination-cidr-block今回は編集で宛先として0.0.0.0/0を設定しています。
  • --gateway-idインターネットゲートウェイを指定します。
#VPCIDの取得
EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

#ルートテーブルのID取得
EC2_ROUTE_TABLE_ID=$( \
  aws ec2 describe-route-tables \
    --filters Name=vpc-id,Values=${EC2_VPC_ID} \
              Name=tag:Name,Values=${EC2_ROUTE_TABLE_TAG_NAME}  \
    --query "RouteTables[].RouteTableId" \
    --output text \
) \
  && echo ${EC2_ROUTE_TABLE_ID}

#IGWのID取得
EC2_INTERNET_GATEWAY_ID=$( \
  aws ec2 describe-internet-gateways \
    --filters Name=tag:Name,Values=${EC2_INTERNET_GATEWAY_TAG_NAME} \
    --query "InternetGateways[].InternetGatewayId" \
    --output text \
) \
  && echo ${EC2_INTERNET_GATEWAY_ID}

#ルートの作成(変数の確認)
cat << END

  # EC2_ROUTE_TABLE_ID:"rtb-xxxxxxxxxxxxxxxxx"
    EC2_ROUTE_TABLE_ID="${EC2_ROUTE_TABLE_ID}"
  # EC2_ROUTE_DESTINATION_CIDR:"0.0.0.0/0"
    EC2_ROUTE_DESTINATION_CIDR="${EC2_ROUTE_DESTINATION_CIDR}"
  # EC2_INTERNET_GATEWAY_ID:"igw-xxxxxxxxxxxxxxxxx"
    EC2_INTERNET_GATEWAY_ID="${EC2_INTERNET_GATEWAY_ID}"

END

#ルートの作成
aws ec2 create-route \
  --route-table-id ${EC2_ROUTE_TABLE_ID} \
  --destination-cidr-block ${EC2_ROUTE_DESTINATION_CIDR} \
  --gateway-id ${EC2_INTERNET_GATEWAY_ID}

完了確認

  • aws ec2 describe-route-tables aws ec2 describe-route-tablesで確認します。
#ルートが存在することを確認
aws ec2 describe-route-tables \
  --filters Name=vpc-id,Values=${EC2_VPC_ID} \
            Name=tag:Name,Values=${EC2_ROUTE_TABLE_TAG_NAME}  \
  --query "RouteTables[].Routes[?DestinationCidrBlock == \`${EC2_ROUTE_DESTINATION_CIDR}\`].DestinationCidrBlock" \
  --output text

#ルートがIGWに設定されていることを確認
aws ec2 describe-route-tables \
  --filters Name=vpc-id,Values=${EC2_VPC_ID} \
            Name=tag:Name,Values=${EC2_ROUTE_TABLE_TAG_NAME}  \
  --query "RouteTables[].Routes[?DestinationCidrBlock == \`${EC2_ROUTE_DESTINATION_CIDR}\`].GatewayId" \
  --output text

参考ドキュメント

#2 EC2の作成に進む

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1