読者です 読者をやめる 読者になる 読者になる

AWS CLIの使い方

AWS CLIAWS Command Line Interface)の使い方。

インストール

$ brew install awscli
$ aws --version
aws-cli/1.6.5 Python/2.7.5 Darwin/13.4.0

セットアップ

configure オプションが用意されているので、アクセスキーやシークレットキー、リージョンと出力フォーマットを入力。

$ aws configure
AWS Access Key ID [None]: xxxxxxxxx
AWS Secret Access Key [None]: xxxxxxxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

すると ~/.aws ディレクトリが生成される。

  • ~/.aws/config
  • ~/.aws/credentials

試す

aws help をするとオプションや利用できるコマンド一覧が表示される。

  • autoscaling
  • cloudformation
  • cloudsearch
  • cloudsearchdoma
  • cloudtrail
  • cloudwatch
  • cognito-identit
  • cognito-sync
  • configservice
  • configure
  • datapipeline
  • deploy
  • directconnect
  • dynamodb
  • ec2
  • elasticache
  • elasticbeanstal
  • elastictranscod
  • elb
  • emr
  • help
  • iam
  • importexport
  • kinesis
  • kms
  • lambda
  • logs
  • opsworks
  • rds
  • redshift
  • route53
  • route53domains
  • s3
  • s3api
  • ses
  • sns
  • sqs
  • storagegateway
  • sts
  • support
  • swf

aws ec2 help のように打つとコマンド別のヘルプも見られる。ec2 コマンドは多すぎてつらい…。

  • accept-vpc-peering-connection
  • allocate-address
  • assign-private-ip-addresses
  • associate-address
  • associate-dhcp-options
  • associate-route-table
  • attach-internet-gateway
  • attach-network-interface
  • attach-volume
  • attach-vpn-gateway
  • authorize-security-group-egress
  • authorize-security-group-ingress
  • bundle-instance
  • cancel-bundle-task
  • cancel-conversion-task
  • cancel-export-task
  • cancel-reserved-instances-listing
  • cancel-spot-instance-requests
  • confirm-product-instance
  • copy-image
  • copy-snapshot
  • create-customer-gateway
  • create-dhcp-options
  • create-image
  • create-instance-export-task
  • create-internet-gateway
  • create-key-pair
  • create-network-acl
  • create-network-acl-entry
  • create-network-interface
  • create-placement-group
  • create-reserved-instances-listing
  • create-route
  • create-route-table
  • create-security-group
  • create-snapshot
  • create-spot-datafeed-subscription
  • create-subnet
  • create-tags
  • create-volume
  • create-vpc
  • create-vpc-peering-connection
  • create-vpn-connection
  • create-vpn-connection-route
  • create-vpn-gateway
  • delete-customer-gateway
  • delete-dhcp-options
  • delete-internet-gateway
  • delete-key-pair
  • delete-network-acl
  • delete-network-acl-entry
  • delete-network-interface
  • delete-placement-group
  • delete-route
  • delete-route-table
  • delete-security-group
  • delete-snapshot
  • delete-spot-datafeed-subscription
  • delete-subnet
  • delete-tags
  • delete-volume
  • delete-vpc
  • delete-vpc-peering-connection
  • delete-vpn-connection
  • delete-vpn-connection-route
  • delete-vpn-gateway
  • deregister-image
  • describe-account-attributes
  • describe-addresses
  • describe-availability-zones
  • describe-bundle-tasks
  • describe-conversion-tasks
  • describe-customer-gateways
  • describe-dhcp-options
  • describe-export-tasks
  • describe-image-attribute
  • describe-images
  • describe-instance-attribute
  • describe-instance-status
  • describe-instances
  • describe-internet-gateways
  • describe-key-pairs
  • describe-network-acls
  • describe-network-interface-attribute
  • describe-network-interfaces
  • describe-placement-groups
  • describe-regions
  • describe-reserved-instances
  • describe-reserved-instances-listings
  • describe-reserved-instances-modifications
  • describe-reserved-instances-offerings
  • describe-route-tables
  • describe-security-groups
  • describe-snapshot-attribute
  • describe-snapshots
  • describe-spot-datafeed-subscription
  • describe-spot-instance-requests
  • describe-spot-price-history
  • describe-subnets
  • describe-tags
  • describe-volume-attribute
  • describe-volume-status
  • describe-volumes
  • describe-vpc-attribute
  • describe-vpc-peering-connections
  • describe-vpcs
  • describe-vpn-connections
  • describe-vpn-gateways
  • detach-internet-gateway
  • detach-network-interface
  • detach-volume
  • detach-vpn-gateway
  • disable-vgw-route-propagation
  • disassociate-address
  • disassociate-route-table
  • enable-vgw-route-propagation
  • enable-volume-io
  • get-console-output
  • get-password-data
  • help
  • import-key-pair
  • modify-image-attribute
  • modify-instance-attribute
  • modify-network-interface-attribute
  • modify-reserved-instances
  • modify-snapshot-attribute
  • modify-subnet-attribute
  • modify-volume-attribute
  • modify-vpc-attribute
  • monitor-instances
  • purchase-reserved-instances-offering
  • reboot-instances
  • register-image
  • reject-vpc-peering-connection
  • release-address
  • replace-network-acl-association
  • replace-network-acl-entry
  • replace-route
  • replace-route-table-association
  • report-instance-status
  • request-spot-instances
  • reset-image-attribute
  • reset-instance-attribute
  • reset-network-interface-attribute
  • reset-snapshot-attribute
  • revoke-security-group-egress
  • revoke-security-group-ingress
  • run-instances
  • start-instances
  • stop-instances
  • terminate-instances
  • unassign-private-ip-addresses
  • unmonitor-instances
  • wait

EC2 インスタンスの一覧を取得したい場合

$ aws ec2 describe-instances

これで全インスタンスJSON フォーマットで返ってくるが、ただの JSON なので可読性が皆無。

JSON 操作用の jq コマンドをインストールして必要な項目だけを出力する。

$ brew install jq
$ jq --version
jq-1.4

ただ、暗記するのはちょっと不可能…。チートシート化してコピペで使うのが良さげ。

$ aws ec2 describe-instances | jq '.Reservations[].Instances[] | {InstanceId, PrivateIpAddress, InstanceName: (.Tags[] | select(.Key=="Name").Value)}'
{
  "InstanceId": "i-xxxxxxxx",
  "PrivateIpAddress": "10.91.26.15",
  "InstanceName": "dev-web-01"
}
{
  "InstanceId": "i-yyyyyyyy",
  "PrivateIpAddress": "10.91.27.237",
  "InstanceName": "dev-web-02"
}
{
  "InstanceId": "i-zzzzzzzz",
  "PrivateIpAddress": "10.91.26.51",
  "InstanceName": "dev-web-03"
}