AWS常用命令(AWSCLI)

EC2
挂载 EBS

linux
查看块设备: lsblk
格式化磁盘: sudo mkfs -t ext4 /dev/xvdb
挂载卷: sudo mount /dev/xvdb /mnt/mydir
卸载卷: sudo umount /dev/xvdb
windows
diskpart
san policy=onlineall
list disk
disk yourdiskid
attributes disk clear readonly
online disk
实例操作

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
(Note: 若不指定subnet-id则会在默认vpc中去选,此时若指定了非默认vpc的安全组会出现请求错误。如无特殊要求,建议安全组和子网都不指定,就不会出现这种问题。)
查看region与AZ

aws ec2 describe-region
aws ec2 describe-availability-zones --region region-name
查看实例元数据和用户数据

curl http://192.168.253.124/latest...
curl http://192.168.253.124/latest...
查看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
AutoScaling
列出AS组
aws autoscaling describe-auto-scaling-groups
列出AS实例
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
挂起AS流程
aws autoscaling suspend-process --auto-scaling-group-name myasgroup --scaling-processes AZRebalance|AlarmNotification|...
删除AS组
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name myasgroup
S3
查看

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"

}

1
2
3
4
5
上传分片

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部分保存为 temp 文件

{"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
IAM
Role 操作
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
AUTO-SCALING
查看信息
aws autoscaling describe-auto-scaling-groups
aws autoscaling describe-auto-scaling-instances
STS
代入ROLE的EC2实例的临时认证信息
curl http://169.254.169.254/latest.../iam/security-credentials/ROLE-NAME
kinesis
创建流
aws kinesis create-stream –stream-name mystream –shard-count
列出流
aws kinesis list-streams
获取指定流的分片迭代器
aws kinesis get-shard-iterator –stream-name mystream –shard-id shard-1 –shard-iterator-type TRIM_HORIZON
发送数据到流
aws kinesis put-record –stream-name mystream –partition-key mykey –data test
获取流数据
aws kinesis get-records –shard-iterator myiterator

创建一个Key,查看内容并生成pem文件。

aws ec2 create-key-pair --key-name MyCypayTestCalifornia --query 'KeyMaterial' \
--output text > MyTestCalifornia.pem
创建一个VPC安全组
aws ec2 create-security-group --group-name MyCATest --description MyCATest \
--vpc-id vpc-efxxxx8a
列举当前安全组名称和ID
aws ec2 describe-security-groups --query SecurityGroups[*].[GroupName,GroupId,VpcId]
添加安全组规则
aws ec2 authorize-security-group-ingress --group-id sg-d1xxxxb4 --protocol tcp \
--port 22 --cidr 202.x.x.120/29 --protocol tcp --port 8080-8082 --cidr 10.10.0.0/16 \
--protocol tcp --port 80 --cidr 0.0.0.0/0
查看当前安全组规则
aws ec2 describe-security-groups --group-ids sg-d1xxxxb4
创建实例
aws ec2 run-instances --image-id ami-7axxxx3f --count 1 --instance-type t1.micro \
--key-name MyTestCalifornia --security-group-ids sg-dxxxxbb4 \
--placement AvailabilityZone=us-west-1c --subnet-id subnet-5exxxx3b \
--block-device-mappings "[{\"DeviceName\": \"/dev/sdf\",\"Ebs\":{\"VolumeSize\":100}}]"\
--user-data "/sbin/mkfs.ext4 /dev/xvdf && /bin/mount /dev/xvdf /home"

注释

--image-id 指定AMI ID
--key-name 可选,登录服务器用
--placement AvailabilityZone=us-west-1c 选择AZ,此项可选,有默认值
--block-device-mappings "[{\"DeviceName\": \"/dev/sdf\",\"Ebs\":{\"VolumeSize\":100}}]"
再额外加100G的EBS磁盘,记得在powershell或者在CMD下双引号需要转义
如果size大于快照大小,需要开机后用resize2fs命令在线动态扩展文件系统
size小于快照中的大小则不生效,按照快照中的磁盘大小创建
--user-data "/sbin/mkfs.ext4 /dev/xvdf && /bin/mount /dev/xvdf /home"
指定开机初始化脚本,可用file://xxxx.sh 来指定网络或者本地文件
给实例打标签

aws ec2 create-tags --resources i-3xxxxb6d --tags Key=Name,Value=APITest \
Key=PROJECT,Value=cypay
创建个EIP

aws ec2 allocate-address --domain vpc
{
"PublicIp": "54.x.x.12",
"Domain": "vpc",
"AllocationId": "eipalloc-axxxxxcd"
}
将VPC中的EIP与VPC中的实例关联

aws ec2 associate-address --instance-id i-3xxxxx6d --allocation-id eipalloc-afxxxx8cd
此时可以ssh链接自己的实例

ssh -i MyTestCalifornia.pem [email protected]
列举出所有用户和他的AccessKey ID

aws iam list-users --query 'Users[*].[UserName,CreateDate,UserId]' --output table
列举出当前实例的相关信息

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, \
Placement.AvailabilityZone, State.Name, InstanceType, PublicIpAddress, \
PrivateIpAddress, Tags[0].Value, Tags[1].Value]' --output text
参考链接:

3 ways to check EC2 Instance ID in AWS

你可能感兴趣的:(aws-lambda)