AWS 常用CLI命令总结

2024-08-30 04:32
文章标签 总结 命令 常用 cli aws

本文主要是介绍AWS 常用CLI命令总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

AWS 常用CLI命令


对AWS服务操作可以通过管理控制台、各语言SDK、CLI以及API等方式。管理控制台最简单,可以直接通过Web界面操作,但是有些服务或者服务下的某些操作无法直接用控制台调用;API的方式最复杂,需要自己生成哈希值签署请求以及处理请求错误等低级别的操作,AWS 的大部分服务都提供REST的API以及类似于REST的查询API,API提供的服务操作是最新最全面的;SDK 的好处是封装好了请求签署与请求错误处理以及重试机制,用户只需要直接调用相关接口即可,但对于新的服务及操作的支持可能会滞后于API接口的发布。CLI 其实也可以看成SDK的一种,它是AWS 服务操作的一把瑞士军刀。本文针对项目实践中所用到的 AWS 服务的常用 CLI 命令进行一个简单总结,方便以后查阅。

EC2


  1. 挂载 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
  2. 实例操作

    • 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的安全组会出现请求错误。如无特殊要求,建议安全组和子网都不指定,就不会出现这种问题。)
  3. 查看region与AZ

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

    • curl http://169.254.169.254/latest/meta-data/
    • curl http://169.254.169.254/latest/user-data/
  5. 查看ami

    • aws ec2 describe-images
  6. key-pair

    • aws ec2 create-key-pair --key-name mykeyname
  7. 安全组

    • 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


  1. 列出AS组
    • aws autoscaling describe-auto-scaling-groups
  2. 列出AS实例
    • aws autoscaling describe-auto-scaling-instances --instance-ids [instance-id-1 instance-id-2 ...]
  3. 从组中分离实例
    • aws autoscaling detach-instances --auto-scaling-group-name myasgroup --instance-ids instanceid1 instanceid2 [--should-decrement-desired-capacity|--no-should-decrement-desired-capacity]
  4. 附加实例到组
    • aws autoscaling attach-instances --auto-scaling-group-name myasgroup --instance-ids instanceid1 instanceid2
  5. 挂起AS流程
    • aws autoscaling suspend-process --auto-scaling-group-name myasgroup --scaling-processes AZRebalance|AlarmNotification|...
  6. 删除AS组
    • aws autoscaling delete-auto-scaling-group --auto-scaling-group-name myasgroup

S3


  1. 查看

    • aws s3 ls
    • aws s3 ls s3://bucket
    • aws s3 ls s3://bucket/prefix
  2. 拷贝

    • 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
  3. 同步

    • 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
  4. 手动分片上传

    • 文件分片
      • 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部分保存为 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
  5. 获取规范用户ID

    • aws s3api list-buckets --query 'Owner.ID'
  6. 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

IAM


  1. 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

  1. 查看信息
    • aws autoscaling describe-auto-scaling-groups
    • aws autoscaling describe-auto-scaling-instances

STS


  1. 代入ROLE的EC2实例的临时认证信息
    • curl http://169.254.169.254/latest/meta-data/iam/security-credentials/ROLE-NAME

kinesis


  1. 创建流
    • aws kinesis create-stream –stream-name mystream –shard-count
  2. 列出流
    • aws kinesis list-streams
  3. 获取指定流的分片迭代器
    • aws kinesis get-shard-iterator –stream-name mystream –shard-id shard-1 –shard-iterator-type TRIM_HORIZON
  4. 发送数据到流
    • aws kinesis put-record –stream-name mystream –partition-key mykey –data test
  5. 获取流数据
    • aws kinesis get-records –shard-iterator myiterator

这篇关于AWS 常用CLI命令总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1119797

相关文章

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

MyBatis常用XML语法详解

《MyBatis常用XML语法详解》文章介绍了MyBatis常用XML语法,包括结果映射、查询语句、插入语句、更新语句、删除语句、动态SQL标签以及ehcache.xml文件的使用,感兴趣的朋友跟随小... 目录1、定义结果映射2、查询语句3、插入语句4、更新语句5、删除语句6、动态 SQL 标签7、ehc

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

java中ssh2执行多条命令的四种方法

《java中ssh2执行多条命令的四种方法》本文主要介绍了java中ssh2执行多条命令的四种方法,包括分号分隔、管道分隔、EOF块、脚本调用,可确保环境配置生效,提升操作效率,具有一定的参考价值,感... 目录1 使用分号隔开2 使用管道符号隔开3 使用写EOF的方式4 使用脚本的方式大家平时有没有遇到自

Python打包成exe常用的四种方法小结

《Python打包成exe常用的四种方法小结》本文主要介绍了Python打包成exe常用的四种方法,包括PyInstaller、cx_Freeze、Py2exe、Nuitka,文中通过示例代码介绍的非... 目录一.PyInstaller11.安装:2. PyInstaller常用参数下面是pyinstal

Linux命令rm如何删除名字以“-”开头的文件

《Linux命令rm如何删除名字以“-”开头的文件》Linux中,命令的解析机制非常灵活,它会根据命令的开头字符来判断是否需要执行命令选项,对于文件操作命令(如rm、ls等),系统默认会将命令开头的某... 目录先搞懂:为啥“-”开头的文件删不掉?两种超简单的删除方法(小白也能学会)方法1:用“--”分隔命

Python 常用数据类型详解之字符串、列表、字典操作方法

《Python常用数据类型详解之字符串、列表、字典操作方法》在Python中,字符串、列表和字典是最常用的数据类型,它们在数据处理、程序设计和算法实现中扮演着重要角色,接下来通过本文给大家介绍这三种... 目录一、字符串(String)(一)创建字符串(二)字符串操作1. 字符串连接2. 字符串重复3. 字

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块