对AWS服务操作可以通过管理控制台、各语言SDK、CLI以及API等方式。管理控制台最简单,可以直接通过Web界面操作,但是有些服务或者服务下的某些操作无法直接用控制台调用;API的方式最复杂,需要自己生成哈希值签署请求以及处理请求错误等低级别的操作,AWS 的大部分服务都提供REST的API以及类似于REST的查询API,API提供的服务操作是最新最全面的;SDK 的好处是封装好了请求签署与请求错误处理以及重试机制,用户只需要直接调用相关接口即可,但对于新的服务及操作的支持可能会滞后于API接口的发布。CLI 其实也可以看成SDK的一种,它是AWS 服务操作的一把瑞士军刀。本文针对项目实践中所用到的 AWS 服务的常用 CLI 命令进行一个简单总结,方便以后查阅。
挂载 EBS
lsblk
sudo mkfs -t ext4 /dev/xvdb
sudo mount /dev/xvdb /mnt/mydir
sudo umount /dev/xvdb
yourdiskid
实例操作
aws ec2 describe-instances
aws ec2 describe-instances --instance-ids "instanceid1" "instanceid2"
aws ec2 start-instances --instance-ids "instanceid1"
"instanceid2"
aws ec2 stop-intances --instance-ids "instanceid1" "instanceid2"
aws ec2 run-instances --image-id ami-b6b62b8f --security-group-ids sg-xxxxxxxx --key-name mytestkey --block-device-mappings "[{\"DeviceName\": \"/dev/sdh\",\"Ebs\":{\"VolumeSize\":100}}]" --instance-type t2.medium --count 1 --subnet-id subnet-e8330c9c --associate-public-ip-address
查看region与AZ
aws ec2 describe-region
aws ec2 describe-availability-zones --region region-name
查看实例元数据和用户数据
curl http://169.254.169.254/latest/meta-data/
curl http://169.254.169.254/latest/user-data/
查看ami
aws ec2 describe-images
key-pair
aws ec2 create-key-pair --key-name mykeyname
安全组
aws ec2 create-security-group --group-name mygroupname --description mydescription --vpc-id vpc-id
(若不指定vpc,则在默认vpc中创建安全组)aws ec2 authorize-security-group-ingress --group-id sg-xxxxyyyy --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-xxxxyyyy --protocol tcp --port 9999 --source-group sg-xxxxxxxx
aws autoscaling describe-auto-scaling-groups
aws autoscaling describe-auto-scaling-instances --instance-ids [instance-id-1 instance-id-2 ...]
aws autoscaling detach-instances --auto-scaling-group-name myasgroup --instance-ids instanceid1 instanceid2 [--should-decrement-desired-capacity|--no-should-decrement-desired-capacity]
aws autoscaling attach-instances --auto-scaling-group-name myasgroup --instance-ids instanceid1 instanceid2
aws autoscaling suspend-process --auto-scaling-group-name myasgroup --scaling-processes AZRebalance|AlarmNotification|...
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name myasgroup
查看
aws s3 ls
aws s3 ls s3://bucket
aws s3 ls s3://bucket/prefix
拷贝
aws s3 cp /to/local/path s3://bucket/prefix
aws s3 cp s3://bucket/prefix /to/local/path
aws s3 cp s3://bucket1/prefix1 s3://bucket2/prefix2
同步
aws sync [--delete] /to/local/dir s3://bucket/prefixdir
aws sync [--delete] s3://bucket/prefixdir /to/local/dir
aws sync [--delete] s3://bucket1/prefixdir1 s3://bucket2/prefixdir2
手动分片上传
split -b 40m myfile myfile-part-
aws s3api create-multipart-upload --bucket bucketname --key prefix
记录返回值
{
"Bucket": "bucketname",
"UploadId": "uploadeid",
"Key": "prefix"
}
上传分片
aws s3api upload-part --bucket bucketname --key prefix --part-number [分片上传编号(e.g. 1,2,3...)] --body myfile-[x] --upload-id uploadid
aws s3api list-parts --bucket bucketname --key prefix --upload-id uploadid
{"Parts": [
{
"PartNumber": 1,
"ETag": "\"xxxxxxx\""
},
{
"PartNumber": 2,
"ETag": "\"xxxxxxxx\""
}
]
}
aws s3api complete-multipart-upload --multipart-upload file://temp --bucket bucketname --key prefix --upload-id uploadid
获取规范用户ID
aws s3api list-buckets --query 'Owner.ID'
AWSCLI 访问阿里云 OSS
aws configure --p aliyun #设置key与secret,其他默认
aws configure set s3.addressing_style virtual --p aliyun
aws s3 ls --endpoint-url [url/(e.g. http://oss-cn-hangzhou.aliyuncs.com)] --p aliyun
aws iam create-role MY-ROLE-NAME --assum-role-policy-document file://path/to/trustpolicy.json
aws iam put-role-policy --role-name MY-ROLE-NAME --policy-name MY-PERM-POLICY --policy-document file://path/to/permissionpolicy.json
aws iam create-instance-profile --instance-profile-name MY-INSTANCE-PROFILE
aws iam add-role-to-instance-profile --instance-profile-name MY-INSTANCE-PROFILE --role-name MY-ROLE-NAME
aws autoscaling describe-auto-scaling-groups
aws autoscaling describe-auto-scaling-instances
curl http://169.254.169.254/latest/meta-data/iam/security-credentials/ROLE-NAME