いつもAWS Management Consoleでやってる作業をAWS Command Line Interfaceでやってみる

今日はAWS Command Line Interface User Guide (Version 1.0.0)を読んで勉強したので、いつもAWS Management Consoleからやってる作業をCLIからやってみた。



インストール
$ sudo easy_install awscli

セットアップ

IAM Role for EC2でPowerUsers設定してるからAccess KeyとSecret Access Keyは設定しない。
$ aws configure
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: us-west-2
Default output format [None]: json
Outputはjson、table、textが使える。違いはこんな感じ。tableめっちゃかっこいい。
$ aws ec2 describe-volumes --output json
{
    "Volumes": [
        {
            "AvailabilityZone": "us-west-2c",
            "Attachments": [
                {
                    "AttachTime": "2013-11-20T04:49:04.000Z",
                    "InstanceId": "i-e71117d3",
                    "VolumeId": "vol-806f02d6",
                    "State": "attached",
                    "DeleteOnTermination": true,
                    "Device": "/dev/sda1"
                }
            ],
            "VolumeType": "standard",
            "VolumeId": "vol-806f02d6",
            "State": "in-use",
            "SnapshotId": "snap-68c27457",
            "CreateTime": "2013-11-20T04:49:04.000Z",
            "Size": 8
        }
    ]
}
$ aws ec2 describe-volumes --output table
---------------------------------------------------------------------------------------------------------------------
|                                                  DescribeVolumes                                                  |
+-------------------------------------------------------------------------------------------------------------------+
||                                                     Volumes                                                     ||
|+------------------+---------------------------+-------+----------------+---------+----------------+--------------+|
|| AvailabilityZone |        CreateTime         | Size  |  SnapshotId    |  State  |   VolumeId     | VolumeType   ||
|+------------------+---------------------------+-------+----------------+---------+----------------+--------------+|
||  us-west-2c      |  2013-11-20T04:49:04.000Z |  8    |  snap-68c27457 |  in-use |  vol-806f02d6  |  standard    ||
|+------------------+---------------------------+-------+----------------+---------+----------------+--------------+|
|||                                                  Attachments                                                  |||
||+---------------------------+------------------------+-------------+--------------+------------+----------------+||
|||        AttachTime         |  DeleteOnTermination   |   Device    | InstanceId   |   State    |   VolumeId     |||
||+---------------------------+------------------------+-------------+--------------+------------+----------------+||
|||  2013-11-20T04:49:04.000Z |  True                  |  /dev/sda1  |  i-e71117d3  |  attached  |  vol-806f02d6  |||
||+---------------------------+------------------------+-------------+--------------+------------+----------------+||
$ aws ec2 describe-volumes --output text
VOLUMES us-west-2c      2013-11-20T04:49:04.000Z        8       snap-68c27457   in-use  vol-806f02d6    standard
ATTACHMENTS     2013-11-20T04:49:04.000Z        True    /dev/sda1       i-e71117d3      attached        vol-806f02d6

設定ファイルは~/.aws/configに作成される。
$ cat ./.aws/config 
[default]
output = json
region = us-west-2

設定は複数保持することが出来、--profileで切り替えられる。別の設定を追加してみる。
$ aws configure --profile table
AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]: us-west-2
Default output format [None]: table
$ cat ./.aws/config
[default]
output = json
region = us-west-2
[profile table]
output = table
region = us-west-2

ヘルプ
末尾にhelpを付けることでhelpが表示される。
トップレベルでのhelp。サブコマンドが列挙される。
$ aws help
各サブコマンドに対するヘルプ。例えばec2。
aws ec2 help
サブコマンドの実行コマンドに対するヘルプ。
$ aws ec2 describe-volumes help
やること

こんな感じ。



VPCを作る
まずはVPCを作る。CIDRは10.0.0.0/16を指定。
$ aws ec2 create-vpc --cidr-block 10.0.0.0/16
{
    "Vpc": {
        "InstanceTenancy": "default",
        "State": "pending",
        "VpcId": "vpc-c92822ab",
        "CidrBlock": "10.0.0.0/16",
        "DhcpOptionsId": "dopt-3bdbbf50"
    }
}

Subnetを作る

Public SubnetとPrivate Subnetの二つのSubnetを作る。AZの指定が出来るので、二重化の観点だと意味が無いけど別々にしてみた。
$ aws ec2 create-subnet --vpc-id vpc-c92822ab --cidr-block 10.0.1.0/24 --availability-zone us-west-2a
{
    "Subnet": {
        "VpcId": "vpc-c92822ab",
        "CidrBlock": "10.0.1.0/24",
        "State": "pending",
        "AvailabilityZone": "us-west-2a",
        "SubnetId": "subnet-5694be22",
        "AvailableIpAddressCount": 251
    }
}
$ aws ec2 create-subnet --vpc-id vpc-c92822ab --cidr-block 10.0.2.0/24 --availability-zone us-west-2b
{
    "Subnet": {
        "VpcId": "vpc-c92822ab",
        "CidrBlock": "10.0.2.0/24",
        "State": "pending",
        "AvailabilityZone": "us-west-2b",
        "SubnetId": "subnet-9d858eff",
        "AvailableIpAddressCount": 251
    }
}

Internet Gatewayを作る

Public Subnetは外部からのアクセスを可能にしたいのでInternet Gatewayを作る。
$ aws ec2 create-internet-gateway
{
    "InternetGateway": {
        "Tags": [],
        "InternetGatewayId": "igw-1f878c7d",
        "Attachments": []
    }
}
作成したInternet GatewayはVPCにAttachしてやる必要がある。
$ aws ec2 attach-internet-gateway --internet-gateway-id igw-1f878c7d --vpc-id vpc-c92822ab
{
    "return": "true"
}

Route Tablesを作る

Route Tablesも二つ作る。一つはPublic Subnet用。
$ aws ec2 create-route-table --vpc-id vpc-c92822ab
{
    "RouteTable": {
        "Associations": [],
        "RouteTableId": "rtb-b9cec4db",
        "VpcId": "vpc-c92822ab",
        "PropagatingVgws": [],
        "Tags": [],
        "Routes": [
            {
                "GatewayId": "local",
                "DestinationCidrBlock": "10.0.0.0/16",
                "State": "active"
            }
        ]
    }
}
Public Subnet用のRoute TableはでInternet GatewayをDefault Gatewayとする。
$ aws ec2 create-route --route-table-id rtb-b9cec4db --destination-cidr-block 0.0.0.0/0 --gateway-id igw-1f878c7d
{
    "return": "true"
}
Public Subnet用のRoute TableをPublic Subnetに紐つける。
$ aws ec2 associate-route-table --subnet-id subnet-5694be22 --route-table-id rtb-b9cec4db
{
    "AssociationId": "rtbassoc-50f2f832"
}

もう一つはPrivate Subnet用。これはDefault Gatewayを持たず、VPCのCIDRにしか経路を持たない。
$ aws ec2 create-route-table --vpc-id vpc-c92822ab
{
    "RouteTable": {
        "Associations": [],
        "RouteTableId": "rtb-9acec4f8",
        "VpcId": "vpc-c92822ab",
        "PropagatingVgws": [],
        "Tags": [],
        "Routes": [
            {
                "GatewayId": "local",
                "DestinationCidrBlock": "10.0.0.0/16",
                "State": "active"
            }
        ]
    }
}
Private Subnet用のRoute TableをPrivate Subnetに紐つける。
$ aws ec2 associate-route-table --subnet-id subnet-9d858eff --route-table-id rtb-9acec4f8
{
    "AssociationId": "rtbassoc-51f2f833"
}
Security Groupを作る

EC2をLaunchする前にSecurityGroupを作成しておく。
$ aws ec2 create-security-group --group-name EC2s --description EC2s --vpc-id vpc-c92822ab
{
    "return": "true",
    "GroupId": "sg-46a3b424"
}
このSecurityGruopではInboundのSSH(Source IPはany)を許可する。
$ aws ec2 authorize-security-group-ingress --group-id sg-46a3b424 --protocol tcp --port 22 --cidr 0.0.0.0/0
{
    "return": "true"
}
EC2を作る

それではEC2のLaunchをする。Public Subnetを指定する。
$ aws ec2 run-instances --image-id ami-be1c848e --count 1 --instance-type t1.micro --key-name mykey --security-group-ids sg-46a3b424 --subnet-id subnet-5694be22
{
    "OwnerId": "8XXXXXXXXXXX",
    "ReservationId": "r-ba0b018e",
    "Groups": [],
    "Instances": [
        {
            "Monitoring": {
                "State": "disabled"
            },
            "PublicDnsName": null,
            "KernelId": "aki-fc8f11cc",
            "State": {
                "Code": 0,
                "Name": "pending"
            },
            "EbsOptimized": false,
            "LaunchTime": "2013-11-20T06:15:54.000Z",
            "PrivateIpAddress": "10.0.1.152",
            "ProductCodes": [],
            "VpcId": "vpc-c92822ab",
            "StateTransitionReason": null,
            "InstanceId": "i-d6a907e2",
            "ImageId": "ami-be1c848e",
            "PrivateDnsName": "ip-10-0-1-152.us-west-2.compute.internal",
            "KeyName": "mykey",
            "SecurityGroups": [
                {
                    "GroupName": "EC2s",
                    "GroupId": "sg-46a3b424"
                }
            ],
            "ClientToken": null,
            "SubnetId": "subnet-5694be22",
            "InstanceType": "t1.micro",
            "NetworkInterfaces": [
                {
                    "Status": "in-use",
                    "SourceDestCheck": true,
                    "VpcId": "vpc-c92822ab",
                    "Description": null,
                    "NetworkInterfaceId": "eni-44457930",
                    "PrivateIpAddresses": [
                        {
                            "Primary": true,
                            "PrivateIpAddress": "10.0.1.152"
                        }
                    ],
                    "Attachment": {
                        "Status": "attaching",
                        "DeviceIndex": 0,
                        "DeleteOnTermination": true,
                        "AttachmentId": "eni-attach-9f49d9ab",
                        "AttachTime": "2013-11-20T06:15:54.000Z"
                    },
                    "Groups": [
                        {
                            "GroupName": "EC2s",
                            "GroupId": "sg-46a3b424"
                        }
                    ],
                    "SubnetId": "subnet-5694be22",
                    "OwnerId": "865720186868",
                    "PrivateIpAddress": "10.0.1.152"
                }
            ],
            "SourceDestCheck": true,
            "Placement": {
                "Tenancy": "default",
                "GroupName": null,
                "AvailabilityZone": "us-west-2a"
            },
            "Hypervisor": "xen",
            "BlockDeviceMappings": [],
            "Architecture": "x86_64",
            "StateReason": {
                "Message": "pending",
                "Code": "pending"
            },
            "RootDeviceName": "/dev/sda1",
            "VirtualizationType": "paravirtual",
            "RootDeviceType": "ebs",
            "AmiLaunchIndex": 0
        }
    ]
}
同様にPrivate Subnetを指定してEC2を立てる。結果は省略。
$ aws ec2 run-instances --image-id ami-be1c848e --count 1 --instance-type t1.micro --key-name mykey --security-group-ids sg-46a3b424 --subnet-id subnet-9d858eff

まとめ

AWS Command Line Interfaceは豊富なコマンド/サブコマンドが用意されているので、Management Consoleでやってることはほぼ全部やれてしまう。
すごくhelpが充実しているので簡単に使える。こういった部分に力が入ってるのはホントAWSのすごさだなぁと思う。



Amazon Web Services クラウドデザインパターン実装ガイドAmazon Web Services クラウドデザインパターン実装ガイド
大澤 文孝,玉川 憲,片山 暁雄,鈴木 宏康

日経BP社
売り上げランキング : 79039

Amazonで詳しく見る by AZlink

このブログの人気の投稿

転職のお知らせ

40代の減量戦略 〜体重-14kg、体脂肪率-12%を実現した具体的な方法〜

45歳になりました