0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWSCLI で 最小環境を構築するには (VPC,Subnet,RouteTable,InternetGateway)

Posted at

ステップ 1: VPC とサブネットを作成する

AWS CLI を使用して VPC およびサブネットを作成するには

  • VPC を作成する。
aws ec2 create-vpc --cidr-block (ネットワークアドレス)/(CIDR)
  • 返された出力内にある、VPC IDをメモる。
{
    "Vpc": {
        "VpcId": "vpc-0c26a2d951052e32c", 
        "InstanceTenancy": "default", 
        "Tags": [], 
        "CidrBlockAssociationSet": [
            {
                "AssociationId": "vpc-cidr-assoc-060fc9f00b6ee0d45", 
                "CidrBlock": "192.168.210.0/24", 
                "CidrBlockState": {
                    "State": "associated"
                }
            }
        ], 
        "Ipv6CidrBlockAssociationSet": [], 
        "State": "pending", 
        "DhcpOptionsId": "dopt-91c238f5", 
        "CidrBlock": "192.168.210.0/24", 
        "IsDefault": false
    }
}

上記出力の、**"VpcId": "vpc-0c26a2d951052e32c"**のところ。

  • サブネットを作成する。
aws ec2 create-subnet --vpc-id (VpcId) --cidr-block (作成するサブネット)

※今回は1サブネットしか作成しないため、サブネット作成は割愛した。

ステップ 2: サブネットをパブリックにする。

VPCおよびサブネットを作成した後、VPCにインターネットゲートウェイをアタッチして、カスタムルートテーブルを作成し、
インターネットゲートウェイへのサブネットからのルーティングを構成する。

  • インターネットゲートウェイを作成する。
aws ec2 create-internet-gateway
  • 返された出力内にある、InternetGatewayIdをメモる。
{
    "InternetGateway": {
        "Tags": [], 
        "Attachments": [], 
        "InternetGatewayId": "igw-0c7b18dc6e4bd7eb5"
    }
}

上記出力の、**"InternetGatewayId": "igw-0c7b18dc6e4bd7eb5"**のところ。

  • VPC にインターネットゲートウェイをアタッチ。
aws ec2 attach-internet-gateway --vpc-id (作成したVpcIdを入力)  --internet-gateway-id (作成したInternetGatewayIdを入力) 
  • VPC に対してカスタムルートテーブルを作成する。
aws ec2 create-route-table --vpc-id (ステップ1で作成したVpcIdを入力)
  • 返された出力内にある、RouteTableIdをメモる。
{
    "RouteTable": {
        "Associations": [], 
        "RouteTableId": "rtb-01a5e9d1d8654077b", 
        "VpcId": "vpc-0c26a2d951052e32c", 
        "PropagatingVgws": [], 
        "Tags": [], 
        "Routes": [
            {
                "GatewayId": "local", 
                "DestinationCidrBlock": "192.168.210.0/24", 
                "State": "active", 
                "Origin": "CreateRouteTable"
            }
        ]
    }
}

上記出力の、**"RouteTableId": "rtb-01a5e9d1d8654077b"**のところ。

  • インターネットゲートウェイへのすべてのトラフィック (0.0.0.0/0) をポイントするルートテーブルでルートを作成。
aws ec2 create-route --route-table-id (作成したRouteTableIdを入力) --destination-cidr-block 0.0.0.0/0 --gateway-id (作成したInternetGatewayIdを入力)
  • 作成したルートが有効になっていることを確認
aws ec2 describe-route-tables --route-table-id (作成したRouteTableIdを入力)
  • このままでは作成たルートテーブルはサブネットに関連付けされていないため、サブネットからのトラフィックがインターネットゲートウェイへルーティングされるよう、VPCのサブネットへ関連づけする。

  • 最初に、describe-subnets コマンドを使用してサブネット ID を取得します。
    --filter オプションを使用して新しい VPC のサブネットだけを返し、
    --query オプションを使用してサブネット ID と CIDR ブロックだけを返します。

aws ec2 describe-subnets --filters "Name=vpc-id,Values=(ステップ1で作成したVpcIdを入力)" --query 'Subnets[*].{ID:SubnetId,CIDR:CidrBlock}'
  • 出力例
[
    {
        "CIDR": "192.168.210.0/24", 
        "ID": "subnet-029c6aa553f1fc6de"
    }
]
  • カスタムルートテーブルに関連付けるサブネットを選択する。
aws ec2 associate-route-table  --subnet-id (ID) --route-table-id (作成したRouteTableIdを入力)
  • サブネット内で起動したインスタンスへ自動的にElastic IPを振り分けるよう設定
aws ec2 modify-subnet-attribute --subnet-id (ID) --map-public-ip-on-launch

以上、EC2インスタンスの作成などは別記事で。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?