Masterclass Advanced Usage of the AWS CLI

109
Masterclass Advanced Usage of the AWS CLI Danilo Poccia AWS Technical Evangelist @danilop danilop

Transcript of Masterclass Advanced Usage of the AWS CLI

Page 1: Masterclass Advanced Usage of the AWS CLI

MasterclassAdvanced Usage of the AWS CLI

Danilo PocciaAWS Technical Evangelist

@danilopdanilop

Page 2: Masterclass Advanced Usage of the AWS CLI

MasterclassA technical deep dive beyond the basics

Help educate you on how to get the best from AWS technologies Show you how things work and how to get things done

Broaden your knowledge in ~45 minutes

Page 3: Masterclass Advanced Usage of the AWS CLI

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

Page 4: Masterclass Advanced Usage of the AWS CLI

Novice

Proficient

AdvancedCrash CourseIntro to the AWS CLI

Page 5: Masterclass Advanced Usage of the AWS CLI

AWS Command Line InterfaceUnified tool to manage your AWS services

Page 6: Masterclass Advanced Usage of the AWS CLI

MSI (Windows)

Bundled (cross platform)

pip (cross platform)

Page 7: Masterclass Advanced Usage of the AWS CLI

aws  configure

Page 8: Masterclass Advanced Usage of the AWS CLI

$  aws  ec2  describe-­‐instances

service (command) operation (subcommand)

Page 9: Masterclass Advanced Usage of the AWS CLI

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

service (command) operation (subcommand)

Page 10: Masterclass Advanced Usage of the AWS CLI

--output json

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

Page 11: Masterclass Advanced Usage of the AWS CLI

--output text

PLACES    Seattle      WA  PLACES    Las  Vegas  NV

Page 12: Masterclass Advanced Usage of the AWS CLI

--output table

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

Page 13: Masterclass Advanced Usage of the AWS CLI

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 14: Masterclass Advanced Usage of the AWS CLI

Basic AWS CLI Usage

DEMO

Page 15: Masterclass Advanced Usage of the AWS CLI

Novice

Proficient

AdvancedFoundationExploring Key Functionality

Page 16: Masterclass Advanced Usage of the AWS CLI

Configuration

Page 17: Masterclass Advanced Usage of the AWS CLI

aws  configure

Page 18: Masterclass Advanced Usage of the AWS CLI

aws  configure

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

Page 19: Masterclass Advanced Usage of the AWS CLI

aws  configure  <subcommand>

Page 20: Masterclass Advanced Usage of the AWS CLI

aws  configure  <subcommand>

list  - List common configuration sourcesget    - Get the value of a single config varset    - Set the value of a single config var

Page 21: Masterclass Advanced Usage of the AWS CLI

aws  configure  get  region

Page 22: Masterclass Advanced Usage of the AWS CLI

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

Page 23: Masterclass Advanced Usage of the AWS CLI

A profile is a group of configuration values

Page 24: Masterclass Advanced Usage of the AWS CLI

aws  configure  -­‐-­‐profile  prod

Page 25: Masterclass Advanced Usage of the AWS CLI

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 26: Masterclass Advanced Usage of the AWS CLI

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

Page 27: Masterclass Advanced Usage of the AWS CLI

aws  configure  set  profile.prod.aws_access_key_id  foo

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

Page 28: Masterclass Advanced Usage of the AWS CLI

aws  configure  set  profile.prod.aws_access_key_id  foo

[prod]aws_access_key_id = foo

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

Page 29: Masterclass Advanced Usage of the AWS CLI

aws  configure  set  profile.prod.aws_secret_access_key  bar

[prod]aws_access_key_id = foo

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

Page 30: Masterclass Advanced Usage of the AWS CLI

[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 31: Masterclass Advanced Usage of the AWS CLI

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

[prod]aws_access_key_id = fooaws_secret_access_key = bar

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

Page 32: Masterclass Advanced Usage of the AWS CLI

[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 33: Masterclass Advanced Usage of the AWS CLI

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 34: Masterclass Advanced Usage of the AWS CLI

[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 35: Masterclass Advanced Usage of the AWS CLI

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  masterclass-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  masterclass-­‐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.masterclass.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.masterclass.secret_access_key  "$secret_access_key"  

Page 36: Masterclass Advanced Usage of the AWS CLI

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  masterclass-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  masterclass-­‐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.masterclass.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.masterclass.secret_access_key  "$secret_access_key"  

Page 37: Masterclass Advanced Usage of the AWS CLI

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  masterclass-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  masterclass-­‐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.masterclass.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.masterclass.secret_access_key  "$secret_access_key"  

Page 38: Masterclass Advanced Usage of the AWS CLI

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  masterclass-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  masterclass-­‐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.masterclass.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.masterclass.secret_access_key  "$secret_access_key"  

Page 39: Masterclass Advanced Usage of the AWS CLI

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  masterclass-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  masterclass-­‐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.masterclass.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.masterclass.secret_access_key  "$secret_access_key"  

Page 40: Masterclass Advanced Usage of the AWS CLI

create-new-user.sh

#!/bin/bash  #  Create  a  new  user  and  create  a  new  profile.  aws  iam  create-­‐user  -­‐-­‐user-­‐name  masterclass-­‐user  credentials=$(aws  iam  create-­‐access-­‐key  -­‐-­‐user-­‐name  masterclass-­‐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.masterclass.aws_access_key_id  "$access_key_id"  aws  configure  set  profile.masterclass.secret_access_key  "$secret_access_key"  

Page 41: Masterclass Advanced Usage of the AWS CLI

Use the aws configure suite of subcommands

Page 42: Masterclass Advanced Usage of the AWS CLI

Query

Page 43: Masterclass Advanced Usage of the AWS CLI

-­‐-­‐query  (string)  

A  JMESPath  query  to  use  in  filtering  the  response  data.

Page 44: Masterclass Advanced Usage of the AWS CLI

Implementation Details --query Processing

aws ec2 ...

parse params

HTTP request

parse response

display response

format response

Retry Pagination

Page 45: Masterclass Advanced Usage of the AWS CLI

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 46: Masterclass Advanced Usage of the AWS CLI

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 47: Masterclass Advanced Usage of the AWS CLI

Implementation Details --query Processing

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

Page 48: Masterclass Advanced Usage of the AWS CLI

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 49: Masterclass Advanced Usage of the AWS CLI

Implementation Details --query Processing

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

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

Page 50: Masterclass Advanced Usage of the AWS CLI

Implementation Details --query Processing

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

Page 51: Masterclass Advanced Usage of the AWS CLI

Implementation Details --query Processing

aws ec2 ...

parse params

HTTP request

parse response

display response

format response

Retry Pagination

Page 52: Masterclass Advanced Usage of the AWS CLI

http://jmespath.orgA Query Language For JSON

Page 53: Masterclass Advanced Usage of the AWS CLI

Waiters

Page 54: Masterclass Advanced Usage of the AWS CLI

Amazon EC2 Instance State Transitions

pending

running

shutting-down

terminated

stopping stoppedrebooting Stop

Terminate

Reboot

Terminate

Start

Page 55: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

Novice

Proficient

AdvancedAdvanced ScenariosLooking at advanced AWS CLI features

Page 63: Masterclass Advanced Usage of the AWS CLI

Templates

Page 64: Masterclass Advanced Usage of the AWS CLI

The AWS CLI is data driven

Page 65: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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: Masterclass Advanced Usage of the AWS CLI

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

Page 69: Masterclass Advanced Usage of the AWS CLI

What else can we do?

Page 70: Masterclass Advanced Usage of the AWS CLI

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

Page 71: Masterclass Advanced Usage of the AWS CLI

Creating and using JSON templates

DEMO

Page 72: Masterclass Advanced Usage of the AWS CLI

Credential Providers

Page 73: Masterclass Advanced Usage of the AWS CLI

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/configCredentialLocator

Amazon  EC2  Instance  Metadata

Page 74: Masterclass Advanced Usage of the AWS CLI

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 75: Masterclass Advanced Usage of the AWS CLI

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 76: Masterclass Advanced Usage of the AWS CLI

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 77: Masterclass Advanced Usage of the AWS CLI

CredentialLocator

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

Page 78: Masterclass Advanced Usage of the AWS CLI

export  AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon  EC2  Instance  Metadata

CredentialLocator

AssumeRole

Page 79: Masterclass Advanced Usage of the AWS CLI

AWS Identity and Access Management (IAM) rolesDelegate access to AWS resources using

Page 80: Masterclass Advanced Usage of the AWS CLI

Production Development

Page 81: Masterclass Advanced Usage of the AWS CLI

Production Development

RolePolicy

Trust Policy

Page 82: Masterclass Advanced Usage of the AWS CLI

Production Development

AssumeRole

Page 83: Masterclass Advanced Usage of the AWS CLI

Production Development

AssumeRole

Page 84: Masterclass Advanced Usage of the AWS CLI

Production Development

AssumeRole

Page 85: Masterclass Advanced Usage of the AWS CLI

configure-role.sh

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

aws  configure  set  profile.prodrole.source_profile  dev

Page 86: Masterclass Advanced Usage of the AWS CLI

~/.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: Masterclass Advanced Usage of the AWS CLI

[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: Masterclass Advanced Usage of the AWS CLI

Amazon S3 Streaming

Page 89: Masterclass Advanced Usage of the AWS CLI

aws  s3  cp

Page 90: Masterclass Advanced Usage of the AWS CLI

We want to avoid disk

Page 91: Masterclass Advanced Usage of the AWS CLI

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

Page 92: Masterclass Advanced Usage of the AWS CLI

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

Page 93: Masterclass Advanced Usage of the AWS CLI

Compress

Page 94: Masterclass Advanced Usage of the AWS CLI

s3-compress.sh

aws  s3  cp  s3://bucket/key  -­‐  |  gzip  |  aws  s3  cp  -­‐  s3://bucket/key.gz

Page 95: Masterclass Advanced Usage of the AWS CLI

Amazon S3 Streaming

DEMO

Page 96: Masterclass Advanced Usage of the AWS CLI

Working in Parallel

Page 97: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

Page 98: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

$  aws  iam  list…

[ "a", "b", ”c", "d", "e" ]

$  aws  iam  …  -­‐-­‐foo  <here>

$  aws  iam  …  -­‐-­‐foo  <here>

$  aws  iam  …  -­‐-­‐foo  <here>

$  aws  iam  …  -­‐-­‐foo  <here>

$  aws  iam  …  -­‐-­‐foo  <here>

Page 99: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

for  name  in  $(aws  iam  list-­‐users  \          -­‐-­‐query  "Users[].[UserName]"  -­‐-­‐output  text);  do      aws  iam  delete-­‐user  -­‐-­‐user-­‐name  "$name"  done

Page 100: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

for  name  in  $(aws  iam  list-­‐users  \          -­‐-­‐query  "Users[].[UserName]"  -­‐-­‐output  text);  do      aws  iam  delete-­‐user  -­‐-­‐user-­‐name  "$name"  done

User1  User2  User3  User4  User5

Page 101: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

for  name  in  $(aws  iam  list-­‐users  \          -­‐-­‐query  "Users[].[UserName]"  -­‐-­‐output  text);  do      aws  iam  delete-­‐user  -­‐-­‐user-­‐name  "$name"  done

Page 102: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

aws  iam  list-­‐users  -­‐-­‐query  "Users[].[UserName]"  -­‐-­‐output  text  |      xargs  -­‐I  {}  -­‐P  10  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  "{}"

Page 103: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

aws  iam  list-­‐users  -­‐-­‐query  "Users[].[UserName]"  -­‐-­‐output  text  |      xargs  -­‐I  {}  -­‐P  10  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  "{}"

Page 104: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

aws  iam  list-­‐users  -­‐-­‐query  "Users[].[UserName]"  -­‐-­‐output  text  |      xargs  -­‐I  {}  -­‐P  10  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  "{}"

Parallel

Page 105: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

\-­‐+=  72730  james  -­‐bash      \-­‐+-­‐  76343  james  xargs  -­‐I  {}  –P  9  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  {}          |-­‐-­‐-­‐  76348  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user1          |-­‐-­‐-­‐  76349  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user2          |-­‐-­‐-­‐  76350  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user3          |-­‐-­‐-­‐  76351  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user4          |-­‐-­‐-­‐  76352  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user5          |-­‐-­‐-­‐  76353  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user6          |-­‐-­‐-­‐  76354  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user7          |-­‐-­‐-­‐  76355  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user8          \-­‐-­‐-­‐  76356  james  aws  iam  delete-­‐user  -­‐-­‐user-­‐name  user9

Page 106: Masterclass Advanced Usage of the AWS CLI

Mapping one output to N AWS calls

• Use a for loop for functions • If you’re using xargs, use -­‐I  {}  • Use xargs  –P  N  parallel execution • Use “.[UserName]” instead of “.UserName” to get

newline separated output..  • This works because nothing is written to stdout on error

Page 107: Masterclass Advanced Usage of the AWS CLI

Wrapping Up

• Configuration• Waiters• Query

• Templates• Credential Providers• Amazon S3 Streaming

Page 108: Masterclass Advanced Usage of the AWS CLI

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

Page 109: Masterclass Advanced Usage of the AWS CLI

MasterclassAdvanced Usage of the AWS CLI

Danilo PocciaAWS Technical Evangelist

@danilopdanilop