LoginSignup
9
7

More than 5 years have passed since last update.

AWS CLIでAWS Account IDを取得する。

Last updated at Posted at 2016-04-11

はじめに

今回のAWS CLIアップデートでSTSにget-caller-identityというコマンドが追加されました。

リリースノート
https://aws.amazon.com/releasenotes/1133794697823335

STSはSecurity Token Serviceの略でhttps://sts.amazonaws.comに単一エンドポイントを持つグローバルサービスです。
AWSのAccountUserIDArnが取得できます。

コマンド
$ aws sts get-caller-identity

それでは実際にコマンドを試してみます。

1.バージョン確認

とりあえず、現在インストールされているCLIのバージョンを確認します。

バージョン確認
$ aws --version
aws-cli/1.10.16 Python/2.7.10 Darwin/15.4.0 botocore/1.4.7

get-caller-identityは今回(1.10.18)から追加されたコマンドのため今のバージョンでは利用できません。
試しに、このままのバージョン(1.10.16)でコマンドを実行してみます。

GetCallerIdentity
$ aws sts get-caller-identity
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: argument operation: Invalid choice, valid choices are:

assume-role                              | assume-role-with-saml
assume-role-with-web-identity            | decode-authorization-message
get-federation-token                     | get-session-token
help

案の定そのようなコマンドは無いと言われてしまいました。

2.アップグレード

それでは今回のバージョンへアップグレードします。

pipインストール
$  sudo pip install --upgrade awscli --ignore-installed six

バージョンを確認します。

バージョン確認
$ aws --version
aws-cli/1.10.18 Python/2.7.10 Darwin/15.4.0 botocore/1.4.9

アップデートが完了しました。

3.コマンド実行

では、コマンドを実行します。

GetCallerIdentity
$ aws sts get-caller-identity
{
    "Account": "123456789012",
    "UserId": "AIDA*****************",
    "Arn": "arn:aws:iam::123456789012:user/kohei"
}

無事、AWS Account IDが取得できているのが確認できます。

ちなみに今回取得できる値について簡単に説明しておきます。

  • Account:AWSアカウントIDは、Amazonリソースネーム(ARN)を構築するのに使用する12桁の数字。
  • UserID:現在のユーザーの一意のID。
  • Arn:Amazonリソースネームの略で、AWSリソースを一意に識別する値。

おわりに

このコマンドはグローバルエンドポイントを見に行って情報を取得してくれるためリージョンの指定などをすることなく実行できます。
これによって現在作業中のプロファイル情報がわかるので複数アカウント切り替えたりする環境ではとても役に立ちそうです。

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7