(dev301) advanced usage of the aws cli | aws re:invent 2014

104
© 2014 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified, or distributed in whole or in part without the express consent of Amazon.com, Inc. November 12, 2014 | Las Vegas Advanced Usage of the AWS CLI James Saryerwinnie, Amazon Web Services

Upload: amazon-web-services

Post on 02-Jul-2015

2.419 views

Category:

Technology


1 download

DESCRIPTION

The AWS CLI provides an easy-to-use command line interface to AWS and allows you to create powerful automation scripts. In this session, you learn advanced techniques that open up new scenarios for using the CLI. We demonstrate how to filter and transform service responses, how to chain and script commands, and how to write custom plugins.

TRANSCRIPT

Page 1: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

©  2014  Amazon.com,  Inc.  and  its  affiliates.  All  rights  reserved.  May  not  be  copied,  modified,  or  distributed  in  whole  or  in  part  without  the  express  consent  of  Amazon.com,  Inc.

November 12, 2014 | Las Vegas

Advanced Usage of the AWS CLI

James Saryerwinnie, Amazon Web Services

Page 2: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Crash Course Foundation Advanced ScenariosIntro to the AWS CLI Exploring Key Functionality Looking at Advanced CLI Features

Page 3: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Novice

Proficient

AdvancedCrash CourseIntro to the AWS CLI

Page 4: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

AWS Command Line InterfaceUnified tool to manage your AWS services

Page 5: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

MSI (Windows)

Bundled (cross platform)

pip (cross platform)

Page 6: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure

Page 7: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

$  aws  ec2  describe-­‐instances

service (command) operation (subcommand)

Page 8: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

$  aws  iam  list-­‐access-­‐keys

service (command) operation (subcommand)

Page 9: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

--output json

{          "Places":  [                  {                          "City":  "Seattle",                          "State":  "WA"                  },                  {                          "City":  "Las  Vegas",                          "State":  "NV"                  }          ]  }

Page 10: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

--output text

PLACES    Seattle      WA  PLACES    Las  Vegas  NV

Page 11: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

--output table

-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  |        SomeOperationName      |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  ||                Places                ||  |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|  ||        City        |    State    ||  |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|  ||    Seattle      |    WA          ||  ||    Las  Vegas  |    NV          ||  |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|

Page 12: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

All Outputs

{          "Places":  [                  {                          "City":  "Seattle",                          "State":  "WA"                  },                  {                          "City":  "Las  Vegas",                          "State":  "NV"                  }          ]  }

PLACES   Seattle        WA  PLACES   Las  Vegas    NV

-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  |        SomeOperationName      |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  ||                Places                ||  |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|  ||        City        |    State    ||  |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|  ||    Seattle      |    WA          ||  ||    Las  Vegas  |    NV          ||  |+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+|

JSON Text Table

Page 13: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Basic AWS CLI Usage

DEMO

Page 14: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Novice

Proficient

AdvancedFoundationExploring Key Functionality

Page 15: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Configuration

Page 16: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure

Page 17: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure

AWS  Access  Key  ID  [**ABCD]:  AWS  Secret  Access  Key  [****************EFGH]:  Default  region  name  [us-­‐west-­‐2]:  Default  output  format  [None]:

Page 18: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  <subcommand>

Page 19: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  <subcommand>

list  - List common configuration sources!get    - Get the value of a single config var!set    - Set the value of a single config var

Page 20: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  get  region

Page 21: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  set  profile.prod.region  us-­‐west-­‐2

Page 22: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

A profile is a group of configuration values

Page 23: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  -­‐-­‐profile  prod

Page 24: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Configuration Files~/.aws/credentials ~/.aws/config

• supported by all AWS SDKs!• only contains credentials

• Used only by the CLI!• Can contain credentials (but not

the default behavior)

Page 25: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

~/.aws/credentials ~/.aws/config

Page 26: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  set  profile.prod.aws_access_key_id  foo

~/.aws/credentials ~/.aws/config

Page 27: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  set  profile.prod.aws_access_key_id  foo

[prod]aws_access_key_id = foo

~/.aws/credentials ~/.aws/config

Page 28: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  set  profile.prod.aws_secret_access_key  bar

[prod]aws_access_key_id = foo

~/.aws/credentials ~/.aws/config

Page 29: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

[prod]aws_access_key_id = fooaws_secret_access_key = bar

~/.aws/credentials ~/.aws/config

aws  configure  set  profile.prod.aws_secret_access_key  bar

Page 30: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  set  profile.prod.region  us-­‐west-­‐2

[prod]aws_access_key_id = fooaws_secret_access_key = bar

~/.aws/credentials ~/.aws/config

Page 31: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

[profile prod]region = us-west-2

[prod]aws_access_key_id = fooaws_secret_access_key = bar

~/.aws/credentials ~/.aws/config

aws  configure  set  profile.prod.region  us-­‐west-­‐2

Page 32: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  configure  set  profile.prod.output  text

[profile prod]region = us-west-2

[prod]aws_access_key_id = fooaws_secret_access_key = bar

~/.aws/credentials ~/.aws/config

Page 33: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

[profile prod]region = us-west-2output = text

[prod]aws_access_key_id = fooaws_secret_access_key = bar

~/.aws/credentials ~/.aws/config

aws  configure  set  profile.prod.output  text

Page 34: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  reinvent-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  reinvent-­‐user  \          -­‐-­‐query  'AccessKey.[AccessKeyId,SecretAccessKey]'  \          -­‐-­‐output  text)  access_key_id=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  1)  secret_access_key=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  2)  aws  configure  set  profile.reinvent.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.reinvent.secret_access_key  "$secret_access_key"  

Page 35: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  reinvent-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  reinvent-­‐user  \          -­‐-­‐query  'AccessKey.[AccessKeyId,SecretAccessKey]'  \          -­‐-­‐output  text)  access_key_id=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  1)  secret_access_key=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  2)  aws  configure  set  profile.reinvent.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.reinvent.secret_access_key  "$secret_access_key"  

Page 36: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  reinvent-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  reinvent-­‐user  \          -­‐-­‐query  'AccessKey.[AccessKeyId,SecretAccessKey]'  \          -­‐-­‐output  text)  access_key_id=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  1)  secret_access_key=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  2)  aws  configure  set  profile.reinvent.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.reinvent.secret_access_key  "$secret_access_key"  

Page 37: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  reinvent-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  reinvent-­‐user  \          -­‐-­‐query  'AccessKey.[AccessKeyId,SecretAccessKey]'  \          -­‐-­‐output  text)  access_key_id=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  1)  secret_access_key=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  2)  aws  configure  set  profile.reinvent.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.reinvent.secret_access_key  "$secret_access_key"  

Page 38: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  reinvent-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  reinvent-­‐user  \          -­‐-­‐query  'AccessKey.[AccessKeyId,SecretAccessKey]'  \          -­‐-­‐output  text)  access_key_id=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  1)  secret_access_key=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  2)  aws  configure  set  profile.reinvent.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.reinvent.secret_access_key  "$secret_access_key"  

Page 39: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  reinvent-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  reinvent-­‐user  \          -­‐-­‐query  'AccessKey.[AccessKeyId,SecretAccessKey]'  \          -­‐-­‐output  text)  access_key_id=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  1)  secret_access_key=$(echo  $credentials  |  cut  -­‐d'  '  -­‐f  2)  aws  configure  set  profile.reinvent.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.reinvent.secret_access_key  "$secret_access_key"  

Page 40: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Use the aws configure suite of subcommands

Page 41: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Query

Page 42: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

-­‐-­‐query  (string)  !A  JMESPath  query  to  use  in  filtering  the  response  data.

Page 43: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

aws ec2 ...

parse params

HTTP request

parse response

display response

format response

Retry Pagination

Page 44: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

aws ec2 ...

parse params

HTTP request

parse response

display response

format response

Retry Pagination

parse response

parse response body

apply --query

Page 45: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

<ListUsersResponse  xmlns="...">      <ListUsersResult>          <Users>              <member>                  <UserId>userid</UserId>                  <Path>/</Path>                  <UserName>james</UserName>                  <Arn>arn:aws:iam::..:user/james</Arn>                  <CreateDate>2013-­‐03-­‐09T23:36:32Z</CreateDate>              </member>          <Users>      </ListUsersResult>  <ListUsersResponse>  !!

Page 46: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

{      "Users":  [          {              "Arn":  "arn:aws:iam::...:user/james",              "UserId":  "userid",              "CreateDate":  "2013-­‐03-­‐09T23:36:32Z",              "Path":  "/",              "UserName":  "james"          }      ]  }

Page 47: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

-­‐-­‐query  Users[0].[UserName,Path,UserId]

{      "Users":  [          {              "Arn":  "arn:aws:iam::...:user/james",              "UserId":  "userid",              "CreateDate":  "2013-­‐03-­‐09T23:36:32Z",              "Path":  "/",              "UserName":  "james"          }      ]  }

Page 48: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

[          [                  "james",  "/",  "id"          ],  ]

-­‐-­‐query  Users[0].[UserName,Path,UserId]

Page 49: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  |                              ListUsers                              |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+  |    james                                |    /  |    userid    |  +-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐+-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐+

Page 50: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details --query Processing

aws ec2 ...

parse params

HTTP request

parse response

display response

format response

Retry Pagination

Page 51: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

http://jmespath.orgA Query Language For JSON

Page 52: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Waiters

Page 53: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Amazon EC2 Instance State Transitions

pending

running

shutting-down

terminated

stopping stoppedrebooting Stop

Terminate

Reboot

Terminate

Start

Page 54: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-instance-running.sh

#!/bin/bash  instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  while  [  "$instance_state"  !=  "running"  ]  do          sleep  1          instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  done  

Page 55: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-instance-running.sh

#!/bin/bash  instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  while  [  "$instance_state"  !=  "running"  ]  do          sleep  1          instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  done  

Page 56: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-instance-running.sh

#!/bin/bash  instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  while  [  "$instance_state"  !=  "running"  ]  do          sleep  1          instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  done  

Page 57: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-instance-running.sh

#!/bin/bash  instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  while  [  "$instance_state"  !=  "running"  ]  do          sleep  1          instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  done  

Page 58: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-instance-running.sh

#!/bin/bash  instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  while  [  "$instance_state"  !=  "running"  ]  do          sleep  1          instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  done  

Page 59: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-instance-running.sh

#!/bin/bash  instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  while  [  "$instance_state"  !=  "running"  ]  do          sleep  1          instance_state=$(aws  ec2  describe-­‐instances  -­‐-­‐instance-­‐ids  $instance_id  \                  -­‐-­‐query  'Reservations[].Instances[].State.Name')  done  

• No timeouts

• Failure states

• Hand-written code

Page 60: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-waiters.sh

instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  aws  ec2  wait  instance-­‐running  -­‐-­‐instance-­‐ids  $instance_id

Page 61: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

ec2-waiters.sh

instance_id=$(aws  ec2  run-­‐instances  -­‐-­‐image-­‐id  ami-­‐12345  \                  -­‐-­‐query  Reservations[].Instances[].InstanceId  \                  -­‐-­‐output  text)  aws  ec2  wait  instance-­‐running  -­‐-­‐instance-­‐ids  $instance_id

subcommand

waiter name

describe-instances options

Page 62: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Novice

Proficient

AdvancedAdvanced ScenariosLooking at advanced AWS CLI features

Page 63: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Templates

Page 64: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

The AWS CLI is data driven

Page 65: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details JSON Models

"RunInstancesRequest":{      "type":"structure",      "required":[          "ImageId",          "MinCount",          "MaxCount"      ],      "members":{          "ImageId":{"shape":"String"},          "MinCount":{"shape":"Integer"},          "MaxCount":{"shape":"Integer"},          "KeyName":{"shape":"String"},          "SecurityGroups":{              "shape":"SecurityGroupStringList",              "locationName":"SecurityGroup"          },  

Page 66: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details JSON Models

"RunInstancesRequest":{      "type":"structure",      "required":[          "ImageId",          "MinCount",          "MaxCount"      ],      "members":{          "ImageId":{"shape":"String"},          "MinCount":{"shape":"Integer"},          "MaxCount":{"shape":"Integer"},          "KeyName":{"shape":"String"},          "SecurityGroups":{              "shape":"SecurityGroupStringList",              "locationName":"SecurityGroup"          },  

-­‐-­‐image-­‐id

-­‐-­‐min-­‐count

-­‐-­‐max-­‐count

-­‐-­‐key-­‐name

-­‐-­‐security-­‐groups

Page 67: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details JSON Models

"RunInstancesRequest":{      "type":"structure",      "required":[          "ImageId",          "MinCount",          "MaxCount"      ],      "members":{          "ImageId":{"shape":"String"},          "MinCount":{"shape":"Integer"},          "MaxCount":{"shape":"Integer"},          "KeyName":{"shape":"String"},          "SecurityGroups":{              "shape":"SecurityGroupStringList",              "locationName":"SecurityGroup"          },  

{      "ImageId":  "ami-­‐12345",      "MinCount":  1,      "MaxCount":  1,      "KeyName":  "id_rsa",      "SecurityGroups":  ["SSH",  "web"],  }  

arguments.json

Page 68: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  ec2  run-­‐instances  -­‐-­‐cli-­‐input-­‐json  file://arguments.json

Page 69: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

What else can we do?

Page 70: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  ec2  run-­‐instances  -­‐-­‐generate-­‐cli-­‐skeleton

Page 71: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Creating and using JSON templates

DEMO

Page 72: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Credential Providers

Page 73: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/configCredentialLocator

Amazon  EC2  Instance  Metadata

Page 74: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 75: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 76: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 77: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 78: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

CredentialLocator

AssumeRole

Page 79: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Delegate access to AWS resources usingAWS Identity and Access Management (IAM) roles

Page 80: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Production Development

Page 81: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Production Development

RolePolicy

Trust Policy

Page 82: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Production Development

AssumeRole

Page 83: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Production Development

AssumeRole

Page 84: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Production Development

AssumeRole

Page 85: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

configure-role.sh

aws  configure  set  profile.prodrole.role_arn  arn:aws:iam...

aws  configure  set  profile.prodrole.source_profile  dev

Page 86: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

~/.aws/credentials ~/.aws/config

[dev]  aws_access_key_id  =  foo  aws_secret_access_key  =  bar

[profile  prodrole]  role_arn  =  arn:aws:iam  source_profile  =  dev

Page 87: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

[dev]  aws_access_key_id  =  foo  aws_secret_access_key  =  bar

[profile  prodrole]  role_arn  =  arn:aws:iam  source_profile  =  dev

~/.aws/credentials ~/.aws/config

Page 88: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Using roles with the AWS CLI

DEMO

Page 89: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Amazon S3 Streaming

Page 90: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  s3  cp

Page 91: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

We want to avoid disk

Page 92: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  s3  cp  -­‐  s3://bucket/key

Page 93: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

aws  s3  cp  s3://bucket/key  -­‐

Page 94: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Compress

Page 95: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

s3-compress.sh

aws  s3  cp  s3://bucket/key  -­‐  |  \  bzip2  -­‐-­‐best  |  \  aws  s3  cp  -­‐  s3://bucket/key.bz2

Page 96: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Amazon S3 Streaming

DEMO

Page 97: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

botocore

Page 98: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details Architecture

AWS CLI

botocore

Providers Serializers Parsers

HTTP

Page 99: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Implementation Details Architecture

AWS CLI

botocore

Providers Serializers Parsers

HTTP

boto3

Page 100: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

list_buckets.py

import  botocore  !s  =  botocore.session.get_session()  s3  =  s.create_client('s3',  region_name='us-­‐west-­‐2')  !for  bucket  in  s3.list_buckets()['Buckets']:          print("Bucket:  {bucket}".format(bucket=bucket['Name']))

Page 101: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Use botocore in the python REPL

DEMO

Page 102: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Wrapping Up

• Configuration!• Waiters!• Query

• Templates!• Credential Providers!• Amazon S3 Streaming

Page 103: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

For More Information• https://github.com/aws/aws-cli!• http://docs.aws.amazon.com/cli/latest/userguide/!• https://forums.aws.amazon.com/forum.jspa?forumID=150!• http://docs.aws.amazon.com/cli/latest/reference/!• http://jmespath.org/!• Boto3 Talk

Page 104: (DEV301) Advanced Usage of the AWS CLI | AWS re:Invent 2014

Please give us your feedback on this presentation

© 2014 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified, or distributed in whole or in part without the express consent of Amazon.com, Inc.

Join the conversation on Twitter with #reinvent

DEV301