【レポート】 AWS CLI: 2017 and Beyond #reinvent #dev307

2017.11.30

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

中山(順)です

本記事は、以下のセッションのレポートとなります。ご査収ください。



速報性を重視したため一部内容が不正確な部分があるかもしれません。気が付き次第訂正します。
また、レポート記事ではありますが、公式ドキュメントへのリンクを張るなどして一部補足しています。

概要

AWS CLIにおける2017年の改善点をデモを交えて紹介するという内容でした。
正直、すんげー地味でしたw(良い内容だったんですけどねw)
デモも豊富で、英語が苦手な私にもわかりやすい内容でした。

  • title: DEV307 - AWS CLI: 2017 and Beyond
  • Speaker: Kyle Knapp (Software Development Engineer , Amazon Web Services)

Are you using the AWS CLI to manage your AWS services and want to do more? In this session learn how to utilize the latest features of the AWS CLI to improve your current workflows in interacting with and managing your AWS resources from the command line. It is recommended that audience members have familiarity with the AWS CLI as this talk will focus on its newer, more advanced features. Come hear from the core AWS CLI development team on how to leverage these features for 2017 and beyond!

セッション内容

2017年の主な改善点は以下の通りです。

  • Debugging
  • Providing Credentials
  • S3

Debugging

AWS CLIでは、内部でbotocoreを使っており、普段使う際に意識することはありません。
しかし、言い換えるとそれはブラックボックスということであり、トラブルシューティングが難しくなる可能性があります。

--debugオプションを付けて問題の原因を調査するという選択肢もありますが、コマンドを実行するたびに大量のメッセージが表示されますし、使い勝手が良いとは言い難いというのが正直なところです。

内部的には以下のようにコマンド実行時の引数でAPIをCALLし、レスポンスをPARSEして結果を返しています。

状況によっては、HTTPのリクエストやレスポンスも確認したいということがあろうかと思います。

そんな時、historyコマンドを利用することでトラブルシューティングがしやすくなります。

historyコマンドは、1.13.0で追加された機能となります。
historyコマンドは、以下のように予め有効化しておくことで利用できるようになります。

aws configure set cli_history enabled

履歴の一覧は以下のように取得できます。

aws history list
8325af37-cc81-47a8-a91e-93a62117c3b5  2017-11-30 02:54:21 AM  s3api get-bucket-location                         0
59dcd9b9-5dc8-474e-9e46-c1e1d1aac41b  2017-11-30 02:46:15 AM  s3api get-bucket-location                         255
b9107710-e3c5-4789-944c-bb4b996f8c32  2017-11-30 02:45:09 AM  s3api get-bucket-location                         0
b7d580db-c2f4-435d-970c-6da0fc856a95  2017-11-30 02:44:56 AM  s3api get-bucket-location                         255
ae10973e-9364-48d9-8613-f01a388a2737  2017-11-30 02:44:46 AM  s3api get-bucket-location                         0
e93a637c-4466-4062-8b12-5b4eaa777abc  2017-11-30 02:44:21 AM  s3api get-bucket-location                         255
4549e291-5138-4f8a-a307-042ce46d1a58  2017-11-30 02:42:52 AM  s3 ls                                             0

以下のコマンドで、直近に実行したコマンドの結果を確認できます。
HTTPのリクエストやレスポンスもわかりやすく表示されてとてもいい感じです。

aws history show
AWS CLI command entered
at time: 2017-11-30 03:33:29.280
with AWS CLI version: aws-cli/1.14.0 Python/2.7.12 Linux/4.9.62-21.56.amzn1.x86_64 botocore/1.8.4
with arguments: [u's3api', u'get-bucket-location', u'--bucket', u'aws-codestar-us-west-2-XXXXXXXXXXXX']

[0] API call made
at time: 2017-11-30 03:33:29.351
to service: s3
using operation: GetBucketLocation
with parameters: {
    "Bucket": "aws-codestar-us-west-2-XXXXXXXXXXXX"
}

[0] HTTP request sent
at time: 2017-11-30 03:33:29.354
to URL: https://s3.amazonaws.com/aws-codestar-us-west-2-XXXXXXXXXXXX?location
with method: GET
with headers: {
    "Authorization": "AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXXXXXXXXXX/20171130/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, Signature=f4a36813d475ceba555xxxxxxxxxxxxbf0dd3814b1e12bdce3bcf0dbfedb97967",
    "User-Agent": "aws-cli/1.14.0 Python/2.7.12 Linux/4.9.62-21.56.amzn1.x86_64 botocore/1.8.4",
    "X-Amz-Content-SHA256": "e3b0c44298fc1c149afxxxxxxxxxxxxxxxae41e4649b934ca495991b7852b855",
    "X-Amz-Date": "20171130T033329Z",
    "X-Amz-Security-Token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
with body: There is no associated body

[0] HTTP response received
at time: 2017-11-30 03:33:30.026
with status code: 200
with headers: {
    "content-type": "application/xml",
    "date": "Thu, 30 Nov 2017 03:33:30 GMT",
    "server": "AmazonS3",
    "transfer-encoding": "chunked",
    "x-amz-id-2": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "x-amz-request-id": "8DB6BB12F3981F29"
}
with body: <?xml version="1.0" ?>
<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">us-west-2</LocationConstraint>


[0] HTTP response parsed
at time: 2017-11-30 03:33:30.028
parsed to: {
    "ResponseMetadata": {
        "HTTPHeaders": {
            "content-type": "application/xml",
            "date": "Thu, 30 Nov 2017 03:33:30 GMT",
            "server": "AmazonS3",
            "transfer-encoding": "chunked",
            "x-amz-id-2": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=",
            "x-amz-request-id": "8DB6BB12F3981F29"
        },
        "HTTPStatusCode": 200,
        "HostId": "keXgCQu+i6kernDYp9VcKzwxxxxxxxxxxxxxxxxxxxxxxxxxyCgExEs7aCCHJ738XbIT4Kd70RE=",
        "RequestId": "8DB6BB12F3981F29"
    }
}

AWS CLI command exited
at time: 2017-11-30 03:33:30.032
with return code: 0

一覧で表示した履歴のIDを指定して過去のコマンドの履歴を確認することも可能です。
(以下は例です)

aws history show 8325af37-xxxx-xxxx-xxxx-93a62117c3b5

historyコマンドの詳細は弊社西澤がまとめておりましたので、こちらもご覧ください。

AWS CLIにhistory機能が追加されました #reinvent

Providing Credentials

ここでは、認証情報を取得する方法をいくつか紹介されていました。

Custom Credential Provider

外部のプロセスから認証情報を取得する方法についての紹介。 ~/.aws/configに呼び出すプロセスを記述できるもののようです。

Sourcing Credentials From External Processes

SAML

SAMLで認証連携できる環境においてAWS CLIで認証情報を取得するデモも行われました。 デモでは、oktaで認証できる環境が設定済みという前提で、awsprocesscredsというツールを用いていました。

awslabs/awsprocesscreds

ツールを実行すると、oktaのユーザーパスワードの入力を要求され、入力するとアクセスキー、シークレットアクセスキー、アクセストークンが返ってくるという感じでした。
ツールがaws sts assume-role-with-samlをいい感じに実行してくれているんだと思います。

プロファイルがどのロールの権限を引き受けるかは、GitHubのREADME.mdに~/.aws/configに設定方法が書いてあるので参考にしてみてください。

assume-role

マルチアカウント環境におけるassume-roleについての紹介およびデモ。
プロファイルに操作対象の認証情報ではなく、IAMロールとAssume-roleするための認証情報が設定されたプロファイルを指定することで、プロファイルを切り替えるだけでIAM Roleの切り替えを簡単に行えるというものです。

~/.aws/credentials:
[development]
aws_access_key_id=foo
aws_secret_access_key=bar
~/.aws/config
[profile crossaccount]
role_arn=arn:aws:iam:...
source_profile=development

また、assume-roleは多段でも使えるようです。
(ちょっと管理が大変そうな気はしますが)

S3

S3コマンドで帯域制御を行えるようになりました、という紹介。
(デモはなぜかmax_concurrent_requestsで実施w)

AWS CLIにおけるS3の設定については以下のドキュメントをご確認ください。

AWS CLI S3 Configuration

S3コマンドは引き続き強化していきそうな意気込みを感じました。

まとめ

非常に地味なセッションですが、より安全かつ効率的なオペレーションを行うために日々改善されているんだということがわかる、心温まる(?)セッションでした。
デモもたくさん用意していただいていて、比較的とっつきやすかったように思います。

これからも個人的にはAWS CLIに注目していきたいと思います。