deep dive: aws command line interface

95
©2015, Amazon Web Services, Inc. or its affiliates. All rights reserved Deep Dive: AWS Command Line Interface James Saryerwinnie, Software Development Engineer, AWS

Upload: amazon-web-services

Post on 06-Aug-2015

521 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Deep Dive: AWS Command Line Interface

©2015, Amazon Web Services, Inc. or its affiliates. All rights reserved

Deep Dive: AWS Command Line Interface

James Saryerwinnie, Software Development Engineer, AWS

Page 2: Deep Dive: AWS Command Line Interface

Crash course Foundation Advanced scenarios

Intro to the AWS CLI Exploring Key Functionality Looking at Advanced CLI Features

Page 3: Deep Dive: AWS Command Line Interface

Crash courseIntro to the AWS CLI

Page 4: Deep Dive: AWS Command Line Interface

AWS Command Line Interface

Unified tool to manage your AWS services

Page 5: Deep Dive: AWS Command Line Interface

MSI (Windows)

Bundled (cross platform)

pip (cross platform)

Page 6: Deep Dive: AWS Command Line Interface

aws configure

Page 7: Deep Dive: AWS Command Line Interface

$ aws ec2 describe-instances

Service (command) Operation (subcommand)

Page 8: Deep Dive: AWS Command Line Interface

$ aws iam list-access-keys

Service (command) Operation (subcommand)

Page 9: Deep Dive: AWS Command Line Interface

json text

PLACES Seattle WA

PLACES Las Vegas NV

table

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

{"Places": [

{ "City": "Seattle", "State": "WA"

}, {

"City": "Las Vegas","State": "NV"

} ]

}

Page 10: Deep Dive: AWS Command Line Interface

FoundationExploring Key Functionality

Page 11: Deep Dive: AWS Command Line Interface

Configuration

Page 12: Deep Dive: AWS Command Line Interface

aws configure

Page 13: Deep Dive: AWS Command Line Interface

aws configure

AWS access key ID [**ABCD]: AWS secret access key [****************EFGH]: Default region name [us-west-2]: Default output format [None]:

Page 14: Deep Dive: AWS Command Line Interface

aws configure <subcommand>

Page 15: Deep Dive: AWS Command Line Interface

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 16: Deep Dive: AWS Command Line Interface

aws configure get region

Page 17: Deep Dive: AWS Command Line Interface

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

Page 18: Deep Dive: AWS Command Line Interface

A profile is a group of configuration values

Page 19: Deep Dive: AWS Command Line Interface

aws configure --profile prod

Page 20: Deep Dive: AWS Command Line Interface

Configuration files

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

• Supported by all AWS SDKs

• Only contains credentials

• Used only by the CLI

• Can contain credentials

Page 21: Deep Dive: AWS Command Line Interface

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

Page 22: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.aws_access_key_id foo

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

Page 23: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.aws_access_key_id foo

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

[prod] aws_access_key_id = foo

Page 24: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.aws_secret_access_key bar

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

[prod] aws_access_key_id = foo

Page 25: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.aws_secret_access_key bar

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

[prod] aws_access_key_id = foo aws_secret_access_key = bar

Page 26: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.region uswest2

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

[prod] aws_access_key_id = foo aws_secret_access_key = bar

Page 27: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.region uswest2

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

[prod] aws_access_key_id = foo aws_secret_access_key = bar

[profile prod] region = us-west-2

Page 28: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.output text

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

[prod] aws_access_key_id = foo aws_secret_access_key = bar

[profile prod] region = us-west-2

Page 29: Deep Dive: AWS Command Line Interface

aws configure set profile.prod.output text

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

[prod] aws_access_key_id = foo aws_secret_access_key = bar

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

Page 30: Deep Dive: AWS Command Line Interface

create-new-user.sh

#!/bin/bash# Create a new user and create a new profile.aws iam create-user --user-name reinvent-usercredentials=$(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.summit.aws_access_key_id "$access_key_id"aws configure set profile.summit.secret_access_key "$secret_access_key"

Page 31: Deep Dive: AWS Command Line Interface

create-new-user.sh

#!/bin/bash# Create a new user and create a new profile.aws iam create-user --user-name reinvent-usercredentials=$(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.summit.aws_access_key_id "$access_key_id"aws configure set profile.summit.secret_access_key "$secret_access_key"

Page 32: Deep Dive: AWS Command Line Interface

create-new-user.sh

#!/bin/bash# Create a new user and create a new profile.aws iam create-user --user-name reinvent-usercredentials=$(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.summit.aws_access_key_id "$access_key_id"aws configure set profile.summit.secret_access_key "$secret_access_key"

Page 33: Deep Dive: AWS Command Line Interface

create-new-user.sh

#!/bin/bash# Create a new user and create a new profile.aws iam create-user --user-name reinvent-usercredentials=$(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.summit.aws_access_key_id "$access_key_id"aws configure set profile.summit.secret_access_key "$secret_access_key"

Page 34: Deep Dive: AWS Command Line Interface

create-new-user.sh

#!/bin/bash# Create a new user and create a new profile.aws iam create-user --user-name reinvent-usercredentials=$(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.summit.aws_access_key_id "$access_key_id"aws configure set profile.summit.secret_access_key "$secret_access_key"

Page 35: Deep Dive: AWS Command Line Interface

create-new-user.sh

#!/bin/bash# Create a new user and create a new profile.aws iam create-user --user-name reinvent-usercredentials=$(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.summit.aws_access_key_id "$access_key_id"aws configure set profile.summit.secret_access_key "$secret_access_key"

Page 36: Deep Dive: AWS Command Line Interface

Use the aws configure

suite of subcommands

Page 37: Deep Dive: AWS Command Line Interface

Query

Page 38: Deep Dive: AWS Command Line Interface

-query (string)

A JMESPath query to use in filtering the response data.

Page 39: Deep Dive: AWS Command Line Interface
Page 40: Deep Dive: AWS Command Line Interface

aws ec2 ...

parse params

HTTP request

parse response

display response

format response

Pagination

parse response

parse response body

apply --query

Page 41: Deep Dive: AWS Command Line Interface

<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 42: Deep Dive: AWS Command Line Interface

{"Users": [{

"Arn": "arn:aws:iam::...:user/james","UserId": "userid","CreateDate": "2013-03-09T23:36:32Z","Path": "/","UserName": "james"

}]

}

Page 43: Deep Dive: AWS Command Line Interface

{"Users": [{

"Arn": "arn:aws:iam::...:user/james","UserId": "userid","CreateDate": "2013-03-09T23:36:32Z","Path": "/","UserName": "james"

}]

}

--query Users[*].[UserName,Path,UserId]

Page 44: Deep Dive: AWS Command Line Interface

[[

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

]

--query Users[*].[UserName,Path,UserId]

Page 45: Deep Dive: AWS Command Line Interface

-----------------------------------------| ListUsers |+-----------------------+----+----------+| james | / | userid |+-----------------------+----+----------+

Page 46: Deep Dive: AWS Command Line Interface
Page 47: Deep Dive: AWS Command Line Interface

http://jmespath.orgA Query Language for JSON

Page 48: Deep Dive: AWS Command Line Interface

Waiters

Page 49: Deep Dive: AWS Command Line Interface

Amazon EC2 instance state transitions

Page 50: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

#!/bin/bashinstance_id=$(aws ec2 run-instances --image-id ami-12345 \

--query 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 1instance_state=$(aws ec2 describe-instances --instance-ids $instance_id \

--query 'Reservations[].Instances[].State.Name')done

Page 51: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

#!/bin/bashinstance_id=$(aws ec2 run-instances --image-id ami-12345 \

--query 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 1instance_state=$(aws ec2 describe-instances --instance-ids $instance_id \

--query 'Reservations[].Instances[].State.Name')done

Page 52: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

#!/bin/bashinstance_id=$(aws ec2 run-instances --image-id ami-12345 \

--query 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 1instance_state=$(aws ec2 describe-instances --instance-ids $instance_id \

--query 'Reservations[].Instances[].State.Name')done

Page 53: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

#!/bin/bashinstance_id=$(aws ec2 run-instances --image-id ami-12345 \

--query 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 1instance_state=$(aws ec2 describe-instances --instance-ids $instance_id \

--query 'Reservations[].Instances[].State.Name')done

Page 54: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

#!/bin/bashinstance_id=$(aws ec2 run-instances --image-id ami-12345 \

--query 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 1instance_state=$(aws ec2 describe-instances --instance-ids $instance_id \

--query 'Reservations[].Instances[].State.Name')done

Page 55: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

#!/bin/bashinstance_id=$(aws ec2 run-instances --image-id ami-12345 \

--query 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 1instance_state=$(aws ec2 describe-instances --instance-ids $instance_id \

--query 'Reservations[].Instances[].State.Name')done

Page 56: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

instance_id=$(aws ec2 run-instances --image-id ami-12345 \--query Instances[].InstanceId \--output text)

aws ec2 wait instance-running --instance-ids $instance_id

Page 57: Deep Dive: AWS Command Line Interface

ec2-instance-running.sh

instance_id=$(aws ec2 run-instances --image-id ami-12345 \--query Instances[].InstanceId \--output text)

aws ec2 wait instance-running --instance-ids $instance_id

subcommand

describe-instances options

waiter name

Page 58: Deep Dive: AWS Command Line Interface

Advanced scenariosLooking at Advanced AWS CLI Features

Page 59: Deep Dive: AWS Command Line Interface

Templates

Page 60: Deep Dive: AWS Command Line Interface

The AWS CLI is data driven

Page 61: Deep Dive: AWS Command Line Interface

"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 62: Deep Dive: AWS Command Line Interface

"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 63: Deep Dive: AWS Command Line Interface

"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"],

}

arguments.json

Page 64: Deep Dive: AWS Command Line Interface

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

Page 65: Deep Dive: AWS Command Line Interface

What else can we do?

Page 66: Deep Dive: AWS Command Line Interface

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

Page 67: Deep Dive: AWS Command Line Interface

Demo

Creating and using JSON templates

Page 68: Deep Dive: AWS Command Line Interface

Credential providers

Page 69: Deep Dive: AWS Command Line Interface

export AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon EC2Instance Metadata

Credential Locator

Page 70: Deep Dive: AWS Command Line Interface

export AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon EC2Instance Metadata

Credential Locator

Page 71: Deep Dive: AWS Command Line Interface

export AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon EC2Instance Metadata

Credential Locator

Page 72: Deep Dive: AWS Command Line Interface

export AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon EC2Instance Metadata

Credential Locator

Page 73: Deep Dive: AWS Command Line Interface

export AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon EC2Instance Metadata

Credential Locator

Page 74: Deep Dive: AWS Command Line Interface

export AWS_ACCESS_KEY_ID=...

~/.aws/credentials

~/.aws/config

Amazon EC2Instance Metadata

Credential Locator

AssumeRole

Page 75: Deep Dive: AWS Command Line Interface

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

Page 76: Deep Dive: AWS Command Line Interface
Page 77: Deep Dive: AWS Command Line Interface
Page 78: Deep Dive: AWS Command Line Interface
Page 79: Deep Dive: AWS Command Line Interface
Page 80: Deep Dive: AWS Command Line Interface
Page 81: Deep Dive: AWS Command Line Interface

aws configure set profile.prodrole.source_profile dev

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

configure-role.sh

Page 82: Deep Dive: AWS Command Line Interface

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

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

Page 83: Deep Dive: AWS Command Line Interface

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

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

Page 84: Deep Dive: AWS Command Line Interface

Demo

Using roles with the AWS CLI

Page 85: Deep Dive: AWS Command Line Interface

Amazon S3 commands

Page 86: Deep Dive: AWS Command Line Interface

aws s3 cp local s3://bucket/keyaws s3 sync . s3://bucket/dir

Page 87: Deep Dive: AWS Command Line Interface

largefile-part-1

largefile-part-2

largefile-part-3

largefile-part-4

delete-filesmallfile

Threads

aws s3 sync . s3://bucket/dir

Page 88: Deep Dive: AWS Command Line Interface

multipart_chunksize

multipart_threshold

max_queue_size

max_concurrent_requests

Page 89: Deep Dive: AWS Command Line Interface

multipart_chunksize

multipart_threshold

max_queue_size

max_concurrent_requests

$ aws configure set default.s3.max_concurrent_requests 20$ aws configure set default.s3.max_queue_size 10000$ aws configure set default.s3.multipart_threshold 64MB$ aws configure set default.s3.multipart_chunksize 16MB

Page 90: Deep Dive: AWS Command Line Interface

http://docs.aws.amazon.com/cli/latest/topic/s3-config.html

$ aws help s3-config

Page 91: Deep Dive: AWS Command Line Interface

Summary

Page 92: Deep Dive: AWS Command Line Interface

Wrapping up

• Configuration

• Waiters

• Query

• Templates

• Credential Providers

• Amazon S3 Commands

Page 93: Deep Dive: AWS Command Line Interface

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/

Page 94: Deep Dive: AWS Command Line Interface

Your feedback is important to AWSPlease complete the session evaluation. Tell us what you think!

Page 95: Deep Dive: AWS Command Line Interface

NEW YORK