AWS学习笔记(一)--CLI基础知识(测试)

Installing the AWS CLI

Install the AWS CLI Using pip on linux
1) Install python
---Check to see if Python is already installed---
$ python --version
---Install python---
$ sudo yum install python

2) Install pip
---check pip---
$ pip -V
---install pip---
$ curl -O https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

如果已安装过老版本的pip,再次执行以上命令是不会更新的,请执行以下命令:
pip install --upgrade pip

3) Install AWS CLI
$ sudo pip install awscli

4) Test AWS CLI
$ aws help
输入q退出

Configuring the AWS CLI

$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: cn-north-1
Default output format [None]: json

The AWS CLI will prompt you for four pieces of information.
这些信息将分别保存在~/.aws/credentials,~/.aws/config文件内。下次再运行此命令,不需改动的项直接按回车即可。

To get your access key ID and secret access key
1) Open the IAM console.
2) In the navigation pane, choose Users.
3) Choose your IAM user name (not the check box).
4) Choose the Security Credentials tab and then choose Create Access Key.
5) To see your access key, choose Show User Security Credentials. Your credentials will look something like this:
Access Key ID: AKIAIOSFODNN7EXAMPLE
Secret Access Key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
6) Choose Download Credentials, and store the keys in a secure location

注意一定要保存下来,仅有这一次机会。如未保存只能删除重建,可以创建多个密钥。

Named Profiles
The AWS CLI supports named profiles stored in the config and credentials files. You can configure additional profiles by using aws configure with the --profile option or by adding entries to the config and credentials files.

Command Line Options
--profile – name of a profile to use, or "default" to use the default profile.
--region – AWS region to call.
--output – output format. 支持json(默认),text,table
--endpoint-url – The endpoint to make the call against. The endpoint can be the address of a proxy or an endpoint URL for the in-use AWS region. 一般情况下不需指定,CLI基于使用的region决定。

示例:describe instances
$ aws ec2 describe-instances --output table --region cn-north-1
以表格的形式显示cn-north-1 region下的所有instance

注意: 机器时间一定要与服务器同步,否则会报以下错误:AWS was not able to validate the provided access credentials

时间同步方法:
$ sudo yum install ntp
$ sudo service ntpd start

Using the AWS CLI

1) 查看帮助
$ aws help
$ aws ec2 help
$ aws ec2 describe-instances help

2)Filter & Query
--filter 设置过滤条件:
$ aws ec2 describe-instances --output table --region cn-north-1 --filter Name=availability-zone,Values=cn-north-1b
如果Values包含空格要使用引号,filter Name支持的参数请查看帮助。

多种条件组合语法如下:
$ aws ec2 describe-instances --filters Name=instance-type,Values=m1.small,m1.medium Name=availability-zone,Values=us-west-2c

从文件加载参数:
$ aws ec2 describe-instances --filters file://filter.json

[
  {
    "Name": "instance-type",
    "Values": ["t2.micro", "m1.medium"]
  },
  {
    "Name": "availability-zone",
    "Values": ["us-west-2c"]
  }
]  

根据自定义的tag查找:
aws ec2 describe-instances --filter Name=tag:Name,Values=prod-asd-app1-1a

--query 选项自定义输出的内容和样式
显示 Volumes 列表中的第一个卷

···
$ aws ec2 describe-volumes --query ‘Volumes[0]‘
{
"AvailabilityZone": "us-west-2a",
"Attachments": [
{
"AttachTime": "2013-09-17T00:55:03.000Z",
"InstanceId": "i-a071c394",
"VolumeId": "vol-e11a5288",
"State": "attached",
"DeleteOnTermination": true,
"Device": "/dev/sda1"
}
],
"VolumeType": "standard",
"VolumeId": "vol-e11a5288",
"State": "in-use",
"SnapshotId": "snap-f23ec1c8",
"CreateTime": "2013-09-17T00:55:03.000Z",
"Size": 30
}
···

循环访问整个列表,并筛选出三个元素:VolumeId、AvailabilityZone 和 Size,并指定别名:

···
$ aws ec2 describe-volumes --query ‘Volumes[*].{ID:VolumeId,AZ:AvailabilityZone,Size:Size}‘
[
{
"AZ": "us-west-2a",
"ID": "vol-e11a5288",
"Size": 30
},
{
"AZ": "us-west-2a",
"ID": "vol-2e410a47",
"Size": 8
}
]
···

使用key1.key2[0].key3 语法来筛选深度嵌套在结构中的元素:

···
$ aws ec2 describe-volumes --query ‘Volumes[*].{ID:VolumeId,InstanceId:Attachments[0].InstanceId,AZ:AvailabilityZone,Size:Size}‘
[
{
"InstanceId": "i-a071c394",
"AZ": "us-west-2a",
"ID": "vol-e11a5288",
"Size": 30
},
{
"InstanceId": "i-4b41a37c",
"AZ": "us-west-2a",
"ID": "vol-2e410a47",
"Size": 8
}
]
···

如未指定别名,将按顺序输出:

···
$ aws ec2 describe-volumes --query ‘Volumes[*].[VolumeId, Attachments[0].InstanceId, AvailabilityZone, Size]‘
[
[
"vol-e11a5288",
"i-a071c394",
"us-west-2a",
30
],
[
"vol-2e410a47",
"i-4b41a37c",
"us-west-2a",
8
]
]
···

按特定字段的值筛选结果:
···
$ aws ec2 describe-volumes --query ‘Volumes[?AvailabilityZone==us-west-2a]‘
···

查询所有running 的 EC2 Instances
···
aws ec2 describe-instances --query ‘Reservations[].Instances[].{State:State.Name,Ip:PrivateIpAddress,InstanceId:InstanceId,Name:Tags[0].Value}‘ --filter Name=instance-state-name,Values=running
···

3)Generate CLI Skeleton and CLI Input JSON Parameters
大多数 AWS CLI 命令支持 --generate-cli-skeleton 和 --cli-input-json 参数,可使用这些参数在 JSON 中存储参数并从文件中读取参数。
当传入大块数据时,将 JSON 保存为一个文件并从命令行引用它可能更为简单。文件中的 JSON 数据更容易读取、编辑和与他人共享。

generate-cli-skeleton
···
$ aws ec2 run-instances --generate-cli-skeleton
···
{
"DryRun": true,
"ImageId": "",
"MinCount": 0,
"MaxCount": 0,
"KeyName": "",
"SecurityGroups": [
""
],
"SecurityGroupIds": [
""
],
"UserData": "",
"InstanceType": "",
"Placement": {
"AvailabilityZone": "",
"GroupName": "",
"Tenancy": "",
"HostId": "",
"Affinity": ""
},
"KernelId": "",
"RamdiskId": "",
"BlockDeviceMappings": [
{
"VirtualName": "",
"DeviceName": "",
"Ebs": {
"SnapshotId": "",
"VolumeSize": 0,
"DeleteOnTermination": true,
"VolumeType": "",
"Iops": 0,
"Encrypted": true
},
"NoDevice": ""
}
],
"Monitoring": {
"Enabled": true
},
"SubnetId": "",
"DisableApiTermination": true,
"InstanceInitiatedShutdownBehavior": "",
"PrivateIpAddress": "",
"ClientToken": "",
"AdditionalInfo": "",
"NetworkInterfaces": [
{
"NetworkInterfaceId": "",
"DeviceIndex": 0,
"SubnetId": "",
"Description": "",
"PrivateIpAddress": "",
"Groups": [
""
],
"DeleteOnTermination": true,
"PrivateIpAddresses": [
{
"PrivateIpAddress": "",
"Primary": true
}
],
"SecondaryPrivateIpAddressCount": 0,
"AssociatePublicIpAddress": true
}
],
"IamInstanceProfile": {
"Arn": "",
"Name": ""
},
"EbsOptimized": true
}

将skeleton保存到文件
···
$ aws ec2 run-instances --generate-cli-skeleton > ec2runinst.json
···

使用时删除不必要的参数并设置合适的参数值。
···
{
"DryRun": true,
"ImageId": "ami-dfc39aef",
"KeyName": "mykey",
"SecurityGroupIds": [
"sg-aa737dcf"
],
"InstanceType": "t2.micro",
"SubnetId": "subnet-ab9035dc"
}
···
将 DryRun 参数设置为 true 可使用 EC2 的空运行功能,可利用此功能在不创建资源的情况下测试配置。

cli-input-json
···
$ aws ec2 run-instances --cli-input-json file://ec2runinst.json
A client error (DryRunOperation) occurred when calling the RunInstances operation: Request would have succeeded, but DryRun flag is set.
···
空运行错误表明,JSON 格式正确且参数值有效。
将 DryRun 参数设置为 false,再次运行 run-instances 命令可启动实例。

Amazon Web Services
AWS中国
AWS文档
AWS CLI 文档
AWS CLI 用户指南
AWS CLI Command Reference

原文地址:http://blog.51cto.com/7308310/2071493

时间: 2024-08-01 06:46:20

AWS学习笔记(一)--CLI基础知识(测试)的相关文章

APUE 学习笔记(一) Unix基础知识

1. Unix 体系结构 内核的接口被称为系统调用 公用函数库构建在系统调用接口之上 应用软件既可以调用公用函数库,也可以直接进行系统调用 2. 文件和目录 目录操作函数:opendir---> readdir---> closedir struct dirent 结构体 stat 系统调用 3.程序.进程.线程 程序:存放在磁盘上.并处于某个目录中的一个可执行文件.使用exec系列函数将程序从磁盘读入存储器,并使其执行 进程:程序的执行实体.进程控制的3个函数:fork.exec.waitp

学习笔记-《Linux基础知识之挂载详解(mount,umount及开机自动挂载)》

<Linux基础知识之挂载详解(mount,umount及开机自动挂载)>来源:Linux社区  作者:chawan 原文链接 http://www.linuxidc.com/Linux/2016-08/134666.htm 以下是学习作者这篇文章做的一些摘要及学习体会. 重要概念(经典原文引述) 挂载概念简述: 根文件系统之外的其他文件要想能够被访问,都必须通过"关联"至根文件系统上的某个目录来实现,此关联操作即为"挂载",此目录即为"挂载点

【 学习笔记 】memcached基础知识

源地址:http://kb.cnblogs.com/page/42731/ 仔细学习了下,以下是记录的笔记备忘内容. 一.memcached是什么?    memcached是高性能的分布式内存缓存服务器.    一般使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态web应用的速度.提高可扩展性 memcached的特征:        1. 协议简单        2. 基于libevent的事件处理        3. 内置内存存储方式        4. memcache

【学习笔记】Linux基础知识

Linux的基本原则 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆为文件: 3.尽量避免捕获用户接口: 4.配置文件保存为纯文本格式: GUI接口: CLI接口: 命令提示符:prompt,bash(shell) #:root $:普通用户 命令: 命令格式: 命令 选项 参数 选项: 短选项:- 多个选项可以组合:-a -b = -ab 长选项:-- 参数:命令的作用对象(多个参数间用空格隔开) 使用凭证: linux系统严格区分大小写 虚拟终端(terminal):Ctr

libevent学习笔记 一、基础知识

欢迎转载,转载请注明原文地址:http://blog.csdn.net/majianfei1023/article/details/46485705 一.libevent是什么 libevent是一个轻量级的开源的高性能的事件触发的网络库,适用于windows.linux.bsd等多种平台,内部使用select.epoll.kqueue等系统调用管理事件机制. 它被众多的开源项目使用,例如大名鼎鼎的memcached等. 特点: 事件驱动,高性能; 轻量级,专注于网络(相对于ACE); 开放源码

MySQL学习笔记01_数据库基础知识

01_1 mysql数据库启动与停止 以<管理员权限>启动cmd: 输入net stop mysql停止mysql服务: 输入net start mysql启动mysql服务: 输入mysql -u root –p回车,然后输入密码进入到mysql的操作系统,并具有root权限: 在命令提示符窗口使用services.msc查看mysql服务的状态. 01_2 数据库的简单介绍 按照数据库的发展时间顺序,主要出现了以下类型数据库系统: 网状型数据库 层次型数据库 关系型数据库 面向对象数据库

学习笔记-Linux系统基础知识2

shell GUI :graphic User Interface x-window CS架构 Gnome KDE Xface CLI: Command Line Interface sh bash csh ksh zsh tcsh dll: Dynamic Link Library .so: shared object login: 用户名:用户ID 认证机制: Authentication 授权:Authorization 审计:Audition(日志) prompt:命令提示符 命令: m

AWS学习笔记(二)--CLI管理Image,Instance,Snapshot,S3

1. Image create-image $ aws ec2 create-image --instance-id i-825465ba --name "Prod Template" --description "Prod Template" --no-reboot 执行成功后会输出ImageId. create-tags通过EC2 Management Console查看AMIs时,列表中的第一项是Name,执行create-image命令后,这项是空的,还需执

Spring基础学习笔记-Bean的基础知识

一. Bean的定义,初始化,使用和销毁 二.ref指定依赖的三种模式 三.Bean的五种自动装配模式(autowire) 四.Bean依赖检查的4种模式:配合atuowire使用,dependency-check="" 五.集合的注入方式 六.管理Bean config.xml文件<!--Bean的配置文档--><!--首先定义为XML的方式来存储Bean的配置--><?xml version="1.0" encoding="