Image
create-image
$ aws ec2 create-image --instance-id i-825465ba --name "Prod Template" --description "Prod Template" --no-reboot
执行成功后输出ImageId
create-tags
通过EC2管理控制台查看AMI,执行create-image命令后,列表中的第一项是Name是空的,执行如下命令添加:
$ aws ec2 create-tags --resources ami-19af7b74 --tags "Key=Name,Value=Prod Template"
可一次为资源添加多个标签,如下:
$ aws ec2 create-tags --resources ami-083b84ad9bc6eeeb6 --tags Key=Name,Value=test-bomsrs-master2 Key=CCX,Value=DSC003 Key=Project,Value=ASD
使用标签可以分类管理资源,进行成本核算等。
modify-image-attribute
公有化AMI:
$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Add\": [{\"Group\":\"all\"}]}"
私有化AMI:
$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Remove\": [{\"Group\":\"all\"}]}"
向AWS账号授予权限:
$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Add\": [{\"UserId\":\"123456789012\"}]}"
撤销AWS账号的权限:
$ aws ec2 modify-image-attribute --image-id ami-19af7b74 --launch-permission "{\"Remove\": [{\"UserId\":\"123456789012\"}]}"
deregister-image
执行deregister-image后,不能再启动新实例,但没有删除AMI。
$ aws ec2 deregister-image --image-id ami-19af7b74
Delete Image
创建image时会创建一个snapshot,要删除image,先执行deregister-image,再执行delete-snapshot。 查询SnapshotId:
$ aws ec2 describe-snapshots --query Snapshots[*].[SnapshotId] --filter Name=description,Values=*ami-19af7b74*
删除snapshot:
$ aws ec2 delete-snapshot --snapshot-id snap-8e2718bd
describe-images
查看所有image(包含public的,会显示很多AWS public image):
$ aws ec2 describe-images
查看自己帐号创建的image:
$ aws ec2 describe-images --owners self --query Images[*].[Name,ImageId]
查看private image:
$ aws ec2 describe-images --query Images[*].[Name,ImageId,OwnerId] --filter "Name=is-public,Values=false"
Instance
run-instances
从AMI启动指定数量的EC2实例。
启动Classic EC2实例:
$ aws ec2 run-instances --image-id ami-1a2b3c4d --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups MySecurityGroup
启动VPC EC2实例:
$ aws ec2 run-instances --image-id ami-abc12345 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-903004f8 --subnet-id subnet-6e7f829e
注意: 必须使用--security-group-ids
start-instances
$ aws ec2 start-instances --instance-ids i-1234567890abcdef0
stop-instances
$ aws ec2 stop-instances --instance-ids i-1234567890abcdef0
reboot-instances
$ aws ec2 reboot-instances --instance-ids i-1234567890abcdef5
terminate-instances
$ aws ec2 terminate-instances --instance-ids i-1234567890abcdef0
describe-instances
查询tag Name=A000的实例:
$ aws ec2 describe-instances --filters "Name=tag:Name,Values=A000"
查询instance type为m1.small或m1.medium,Availability Zone为us-west-2c的实例:
$ aws ec2 describe-instances --filters "Name=instance-type,Values=m1.small,m1.medium" "Name=availability-zone,Values=us-west-2c"
monitor-instances
By default, Amazon EC2 sends metric data to CloudWatch in 5-minute periods. To send metric data for your instance to CloudWatch in 1-minute periods, you can enable detailed monitoring on the instance.
To enable detailed monitoring for an existing instance
$ aws ec2 monitor-instances --instance-ids i-1234567890abcdef0
To enable detailed monitoring when launching an instance
$ aws ec2 run-instances --image-id ami-09092360 --monitoring Enabled=true...
To disable detailed monitoring
$ aws ec2 unmonitor-instances --instance-ids i-1234567890abcdef0
Snapshot & Volume
create-snapshot
为EBS volume创建snapshot,snapshot存储在Amazon S3:
$ aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description "This is my root volume snapshot."
create-volum
创建新的volum:
$ aws ec2 create-volume --size 80 --region us-east-1 --availability-zone us-east-1a --volume-type gp2
从snapshot创建IOPS (SSD) volume:
$ aws ec2 create-volume --region us-east-1 --availability-zone us-east-1a --snapshot-id snap-066877671789bd71b --volume-type io1 --iops 1000
attach-volume
附加EBS volume到启动或停止的实例:
$ aws ec2 attach-volume --volume-id vol-1234567890abcdef0 --instance-id i-01474ef662b89480 --device /dev/sdf
delete-volume
删除EBS volume,volume必须为available状态(未附加到实例)
$ aws ec2 delete-volume --volume-id vol-049df61146c4d7901
S3
mb
创建S3 bucket:
$ aws s3 mb s3://mybucket
cp
将一个对象从S3 复制到本地:
$ aws s3 cp s3://my_bucket/my_folder/my_file.ext my_copied_file.ext
将一个对象复制到S3:
$ aws s3 cp my_copied_file.ext s3://my_bucket/my_folder/my_file.ext
将一个对象从S3复制到另一个S3:
$ aws s3 cp s3://my_bucket/my_folder/my_file.ext s3://my_bucket/my_folder/my_file2.ext
sync
同步本地文件到S3:
$ aws s3 sync . s3://mybucket
将整个S3 存储桶下载到本地目录:
$ aws s3 sync s3://remote_S3_bucket local_directory
S3间的同步:
$ aws s3 sync s3://mybucket s3://mybucket2
rb
删除空S3 bucket,对象和版本化对象都必须为空。 指定--force参数可以删除含非版本化对象的bucket。
$ aws s3 rb s3://mybucket --force