AWS CLIコマンドを実行します。
手順に必要な設定値を変数に格納をします。
AWS認証ファイルを指定します。
環境変数の設定: export AWS_SHARED_CREDENTIALS_FILE="${HOME}/environment/tmp-handson-cli-sts-session-token/handson-cli-sts-session-token.ini"
環境変数の設定:
export AWS_SHARED_CREDENTIALS_FILE="${HOME}/environment/tmp-handson-cli-sts-session-token/handson-cli-sts-session-token.ini"
プロファイルを指定します。
環境変数の設定: export AWS_PROFILE='handson-cli-sts-session-token'
export AWS_PROFILE='handson-cli-sts-session-token'
各変数に正しい設定値が格納されていることを確認します。
変数の確認: cat << END # 0. AWS_SHARED_CREDENTIALS_FILE:"${HOME}/environment/tmp-handson-cli-sts-session-token/handson-cli-sts-session-token.ini" AWS_SHARED_CREDENTIALS_FILE="${AWS_SHARED_CREDENTIALS_FILE}" # AWS_PROFILE:"handson-cli-sts-session-token" AWS_PROFILE="${AWS_PROFILE}" END
変数の確認:
cat << END # 0. AWS_SHARED_CREDENTIALS_FILE:"${HOME}/environment/tmp-handson-cli-sts-session-token/handson-cli-sts-session-token.ini" AWS_SHARED_CREDENTIALS_FILE="${AWS_SHARED_CREDENTIALS_FILE}" # AWS_PROFILE:"handson-cli-sts-session-token" AWS_PROFILE="${AWS_PROFILE}" END
下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。
有効期限内
AWSコマンドを実行します。(S3の例)
コマンド: aws s3 ls 結果(例): 2021-12-20 09:00:00 example-bucket
コマンド:
aws s3 ls
結果(例):
2021-12-20 09:00:00 example-bucket
AWSコマンドを実行します。(EC2の例)
環境変数の設定: export AWS_DEFAULT_REGION='ap-northeast-1' コマンド: aws ec2 describe-vpcs 結果(例): { "Vpcs": [ { "CidrBlock": "10.0.0.0/16", "DhcpOptionsId": "dopt-bec282da", "State": "available", "VpcId": "vpc-03b6793be492e9eaa", "OwnerId": "XXXXXXXXXXXX", "InstanceTenancy": "default", "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-0379592cf9e84bd4b", "CidrBlock": "10.0.0.0/16", "CidrBlockState": { "State": "associated" } } ], "IsDefault": false, "Tags": [ { "Key": "aws:cloudformation:stack-id", "Value": "arn:aws:cloudformation:ap-northeast-1:XXXXXXXXXXXX:stack/handson-cloud9/de3e6880-2758-11ec-bc32-06689a7cb845" }, { "Key": "aws:cloudformation:stack-name", "Value": "handson-cloud9" }, { "Key": "aws:cloudformation:logical-id", "Value": "Vpc0StackNameSuffix" }, { "Key": "Name", "Value": "handson-cloud9-vpc" } ] } ] }
export AWS_DEFAULT_REGION='ap-northeast-1'
aws ec2 describe-vpcs
{ "Vpcs": [ { "CidrBlock": "10.0.0.0/16", "DhcpOptionsId": "dopt-bec282da", "State": "available", "VpcId": "vpc-03b6793be492e9eaa", "OwnerId": "XXXXXXXXXXXX", "InstanceTenancy": "default", "CidrBlockAssociationSet": [ { "AssociationId": "vpc-cidr-assoc-0379592cf9e84bd4b", "CidrBlock": "10.0.0.0/16", "CidrBlockState": { "State": "associated" } } ], "IsDefault": false, "Tags": [ { "Key": "aws:cloudformation:stack-id", "Value": "arn:aws:cloudformation:ap-northeast-1:XXXXXXXXXXXX:stack/handson-cloud9/de3e6880-2758-11ec-bc32-06689a7cb845" }, { "Key": "aws:cloudformation:stack-name", "Value": "handson-cloud9" }, { "Key": "aws:cloudformation:logical-id", "Value": "Vpc0StackNameSuffix" }, { "Key": "Name", "Value": "handson-cloud9-vpc" } ] } ] }
AWSコマンドを実行します。(IAMの例)
コマンド: aws iam list-users 結果(例): An error occurred (InvalidClientTokenId) when calling the ListUsers operation: The security token included in the request is invalid 注釈 リクエストに MFA 情報が含まれていない場合は、IAM API オペレーションを呼び出せません。 ( https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_credentials_temp_request.html )
aws iam list-users
An error occurred (InvalidClientTokenId) when calling the ListUsers operation: The security token included in the request is invalid
注釈
リクエストに MFA 情報が含まれていない場合は、IAM API オペレーションを呼び出せません。 ( https://docs.aws.amazon.com/ja_jp/IAM/latest/UserGuide/id_credentials_temp_request.html )
有効期限終了後
AWSコマンドを実行します
コマンド: aws s3 ls 結果(例): An error occurred (ExpiredToken) when calling the ListBuckets operation: The provided token has expired.
An error occurred (ExpiredToken) when calling the ListBuckets operation: The provided token has expired.
本手順の主処理は、以下の完了条件を満たしたときに成功したものとします。
完了条件1: AWS CLIコマンドの実行結果を確認できている。
「AWS CLIコマンドの実行結果を確認できている。」ことを確認します。
主処理で取得できていればOKです。
環境変数を削除して、プロファイルの権限を無効にします。
環境変数の設定: export -n AWS_SHARED_CREDENTIALS_FILE 環境変数の設定: export -n AWS_PROFILE
export -n AWS_SHARED_CREDENTIALS_FILE
export -n AWS_PROFILE