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

相关文章

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

Linux查询服务器 IP 地址的命令详解

《Linux查询服务器IP地址的命令详解》在服务器管理和网络运维中,快速准确地获取服务器的IP地址是一项基本但至关重要的技能,下面我们来看看Linux中查询服务器IP的相关命令使用吧... 目录一、hostname 命令:简单高效的 IP 查询工具命令详解实际应用技巧注意事项二、ip 命令:新一代网络配置全

Linux grep 命令的使用指南

《Linuxgrep命令的使用指南》本文给大家介绍Linuxgrep命令的使用指南,包括基础搜索语法、实践指南,感兴趣的朋友跟随小编一起看看吧... 目录linux grep 命令全面使用指南一、基础搜索语法1. 基本文本搜索2. 多文件搜索二、常用选项详解1. 输出控制选项2. 上下文控制选项三、正则表达