0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS CLIでVPC作成と情報取得方法

Posted at

はじめに

今回はAWS CLIを利用してVPC作成を実施します。
AWS CLIを利用することでスクリプト等を使用して情報取得することができます。
特にVPCID取得するには他CLIコマンドで利用することも多いかと思いますので、
投稿します。

VPC作成

# 基本的利用方法
$ aws ec2 create-vpc \
  --cidr-block <VPC CIDR> \
  --tag-specifications <VPC TAGNAME>

利用例:

# 変数定義
$ VPC_TAG_NAME='example-cli-vpc'
$ VPC_CIDR='10.0.0.0/16'
$ STRING_VPC_TAG="ResourceType=vpc,Tags=[{Key=Name,Value=${VPC_TAG_NAME}}]" \
&& echo ${STRING_VPC_TAG}
ResourceType=vpc,Tags=[{Key=Name,Value=${VPC_TAG_NAME}}]

$ aws ec2 create-vpc \
  --cidr-block ${VPC_CIDR} \
  --tag-specifications ${STRING_VPC_TAG}

{
    "Vpc": {
        "CidrBlock": "10.0.0.0/16",
        "DhcpOptionsId": "dopt-09b680c4b58db5453",
        "State": "pending",
        "VpcId": "vpc-00da815ef79e7d0c5",
        "OwnerId": "030422977218",
        "InstanceTenancy": "default",
        "Ipv6CidrBlockAssociationSet": [],
        "CidrBlockAssociationSet": [
            {
                "AssociationId": "vpc-cidr-assoc-07b3e0f35900f52d1",
                "CidrBlock": "10.0.0.0/16",
                "CidrBlockState": {
                    "State": "associated"
                }
            }
        ],
        "IsDefault": false,
        "Tags": [
            {
                "Key": "Name",
                "Value": "example-cli-vpc"
            }
        ]
    }
}

各変数は以下。
VPC_TAG_NAME='example-cli-vpc':作成したいVPC名
VPC_CIDR='10.0.0.0/16':作成したいVPCのCIDR設定
STRING_VPC_TAG="ResourceType=vpc,Tags=[{Key=Name,Value=${VPC_TAG_NAME}}]" :VPC名とタグ名を設定

VPC内容取得

# 基本的利用方法
$ aws ec2 describe-vpcs \
  --filters Name=tag:Name,Values=<VPC TAGNAME>  \
  --query 'Vpcs[].Tags[?Key == `Name`].Value' \
  --output text

利用例:

# VPC情報取得
$ aws ec2 describe-vpcs \
  --filters Name=tag:Name,Values=${VPC_TAG_NAME}\

{
    "Vpcs": [
        {
            "CidrBlock": "11.0.0.0/16",
            "DhcpOptionsId": "dopt-09b680c4b58db5453",
            "State": "available",
            "VpcId": "vpc-00da815ef79e7d0c5",
            "OwnerId": "030422977218",
            "InstanceTenancy": "default",
            "CidrBlockAssociationSet": [
                {
                    "AssociationId": "vpc-cidr-assoc-07b3e0f35900f52d1",
                    "CidrBlock": "11.0.0.0/16",
                    "CidrBlockState": {
                        "State": "associated"
                    }
                }
            ],
            "IsDefault": false,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "example-cli-vpc"
                }
            ]
        }
    ]
}

色々取得してみる

# VPCIDを取得する場合
$ aws ec2 describe-vpcs \
   --filter Name=tag:Name,Values=${VPC_TAG_NAME} \
   --query 'Vpcs[].VpcId' \
   --output text
vpc-0c9e269716aebf8ff

# VPC CIDRBLOCKを取得する場合
$ aws ec2 describe-vpcs \
   --filter Name=tag:Name,Values=${VPC_TAG_NAME} \
   --query 'Vpcs[].CidrBlock' \
   --output text

VPC削除

# 基本的利用方法
$ aws ec2 delete-vpc
  --vpc-id <VPC ID>

利用例:

# VPC IDを変数に取得
$ EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

# VPC削除
$ aws ec2 delete-vpc \
  --vpc-id ${EC2_VPC_ID}
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?