こんにちは nakada です。 CLIシリーズのpart3です。今回はec2インスタンスを作成するコマンドをご紹介します。
目次
環境
$ aws --version aws-cli/1.7.44 Python/2.7.9 Linux/3.14.35-28.38.amzn1.x86_64 $ jq -V jq-1.4
EC2の作成
run instances コマンド
今まで紹介したコマンドと違って指定可能なオプションが多いです。
使用するコマンド
aws ec2 run-instances
[--dry-run | --no-dry-run]
--image-id
[--key-name]
[--security-groups
コマンドの実行
次の構成でインスタンスを作成してみます。 * Amazon Linux * t2.micro * key-name の指定 * 既存のsecurity-groupの指定 * subnet-id の指定 * secondary-private-ip-addressesの指定 * associate-public-ip-address * 20GBのEBSの割当
実行結果
$ aws ec2 run-instances --image-id ami-xxxxxxxx --instance-type t2.micro --key-name te st-xxxxxxxx --subnet-id subnet-xxxxxxxx --security-group-ids sg-xxxxxxaa sg-xxxxxxbb --secondary-private-ip-addresses 192.168.1.99 --associate-public-ip-address --block-device-mappings "[{\"DeviceName\": \"/dev/xvda\",\"Ebs\":{\"Vo lumeSize\":20}}]" { "OwnerId": "xxxxxxxxxxxx", "ReservationId": "r-xxxxxxxx", "Groups": [], "Instances": [ { "Monitoring": { "State": "disabled" }, "PublicDnsName": "", "RootDeviceType": "ebs", "State": { "Code": 0, "Name": "pending" }, "EbsOptimized": false, "LaunchTime": "2015-08-18T01:36:45.000Z", "PrivateIpAddress": "192.168.1.178", "ProductCodes": [], "VpcId": "vpc-xxxxxxxx", "StateTransitionReason": "", "InstanceId": "i-xxxxxxxx", "ImageId": "ami-cbf90ecb", "PrivateDnsName": "ip-192-168-1-178.ap-northeast-1.compute.internal", "KeyName": "test-xxxxxxxx", "SecurityGroups": [ { "GroupName": "https only", "GroupId": "sg-xxxxxxaa" }, { "GroupName": "ssh-only", "GroupId": "sg-xxxxxxbb" } ], "ClientToken": "", "SubnetId": "subnet-xxxxxxxx", "InstanceType": "t2.micro", "NetworkInterfaces": [ { "Status": "in-use", "MacAddress": "06:60:51:02:cd:d1", "SourceDestCheck": true, "VpcId": "vpc-xxxxxxxx", "Description": "", "NetworkInterfaceId": "eni-xxxxxxxx", "PrivateIpAddresses": [ { "Primary": true, "PrivateIpAddress": "192.168.1.178" }, { "PrivateDnsName": "", "Primary": false, "PrivateIpAddress": "192.168.1.99" } ], "Attachment": { "Status": "attaching", "DeviceIndex": 0, "DeleteOnTermination": true, "AttachmentId": "eni-attach-xxxxxxxx", "AttachTime": "2015-08-18T01:36:45.000Z" }, "Groups": [ { "GroupName": "https only", "GroupId": "sg-xxxxxxaa" }, { "GroupName": "ssh-only", "GroupId": "sg-xxxxxxxxbb" } ], "SubnetId": "subnet-xxxxxxxx", "OwnerId": "xxxxxxxxxxxx", "PrivateIpAddress": "192.168.1.178" } ], "SourceDestCheck": true, "Placement": { "Tenancy": "default", "GroupName": "", "AvailabilityZone": "ap-northeast-1a" }, "Hypervisor": "xen", "BlockDeviceMappings": [], "Architecture": "x86_64", "StateReason": { "Message": "pending", "Code": "pending" }, "RootDeviceName": "/dev/xvda", "VirtualizationType": "hvm", "AmiLaunchIndex": 0 } ] }
jsonファイルからの読み込み
指定可能なオプションが多いので、雛形のファイルを用意しておいて、そのファイルから作成した方が楽です。 雛形を作成するには、[--generate-cli-skeleton]オプションを使用します。作成した雛形利用して作成してみます。
$ aws ec2 run-instances --generate-cli-skeleton > run-instances-test.json ###編集後のファイル $ cat run-instances-test.json { "ImageId": "ami-xxxxxxxx", "KeyName": "test-local", "SecurityGroupIds": [ "sg-e471f781","sg-xxxxxxxx" ], "InstanceType": "t2.micro", "BlockDeviceMappings": [ { "DeviceName": "/dev/xvda", "Ebs": { "VolumeSize": 20, "VolumeType": "gp2" }, "NoDevice":"" } ], "Monitoring": { "Enabled": false }, "NetworkInterfaces": [ { "DeviceIndex": 0, "DeleteOnTermination": true, "AssociatePublicIpAddress": true } ], "SubnetId": "subnet-xxxxxxxx", "DisableApiTermination": false }
作成したjsonファイルを指定してインスタンスを作成してみます。
$ aws ec2 run-instances --cli-input-json file://run-instances-test.json { "OwnerId": "xxxxxxxx", "ReservationId": "r-xxxxxxxx", "Groups": [], "Instances": [ { "Monitoring": { "State": "disabled" }, "PublicDnsName": "", "RootDeviceType": "ebs", "State": { "Code": 0, "Name": "pending" }, "EbsOptimized": false, "LaunchTime": "2015-08-18T04:29:16.000Z", "PrivateIpAddress": "192.168.1.99", "ProductCodes": [], "VpcId": "vpc-xxxxxxxx", "StateTransitionReason": "", "InstanceId": "i-xxxxxxxx", "ImageId": "ami-cbf90ecb", "PrivateDnsName": "ip-192-168-1-99.ap-northeast-1.compute.internal", "KeyName": "test-xxxxxxxx", "SecurityGroups": [ { "GroupName": "https only", "GroupId": "sg-xxxxxxxx" }, { "GroupName": "ssh-only", "GroupId": "sg-xxxxxxxx" } ], "ClientToken": "", "SubnetId": "subnet-xxxxxxxx", "InstanceType": "t2.micro", "NetworkInterfaces": [ { "Status": "in-use", "MacAddress": "06:70:ba:99:fd:cf", "SourceDestCheck": true, "VpcId": "vpc-xxxxxxxx", "Description": "", "NetworkInterfaceId": "eni-xxxxxxxx", "PrivateIpAddresses": [ { "Primary": true, "PrivateIpAddress": "192.168.1.99" } ], "Attachment": { "Status": "attaching", "DeviceIndex": 0, "DeleteOnTermination": true, "AttachmentId": "eni-attach-xxxxxxxx", "AttachTime": "2015-08-18T04:29:16.000Z" }, "Groups": [ { "GroupName": "https only", "GroupId": "sg-xxxxxxxx" }, { "GroupName": "ssh-only", "GroupId": "sg-xxxxxxxx" } ], "SubnetId": "subnet-xxxxxxxx", "OwnerId": "xxxxxxxx", "PrivateIpAddress": "192.168.1.99" } ], "SourceDestCheck": true, "Placement": { "Tenancy": "default", "GroupName": "", "AvailabilityZone": "ap-northeast-1a" }, "Hypervisor": "xen", "BlockDeviceMappings": [], "Architecture": "x86_64", "StateReason": { "Message": "pending", "Code": "pending" }, "RootDeviceName": "/dev/xvda", "VirtualizationType": "hvm", "AmiLaunchIndex": 0 } ] }
今回はEC2インスタンスの作成コマンドを紹介しました。