LoginSignup
6
4

More than 5 years have passed since last update.

AWS CodeBuildのビルド環境でアカウントIDを取得する

Posted at

目的

ビルド環境内でAWSのアカウントIDを動的に取得したい。

※なんで、公式の環境変数に無いのだろうか・・・。
https://docs.aws.amazon.com/ja_jp/codebuild/latest/userguide/build-env-ref-env-vars.html

やり方

buildspec.yml内で以下のコマンドを記載
ACCOUNT_ID=${CODEBUILD_BUILD_ARN} && IFS=':' && set -- $ACCOUNT_ID && ACCOUNT_ID=$5

以降は、${ACCOUNT_ID}で使いまわせる。

説明

ACCOUNT_ID=${CODEBUILD_BUILD_ARN}
「ACCOUNT_ID」にビルドの Amazon リソースネーム (ARN) (例:arn:aws:codebuild:region-ID:account-ID:build/codebuild-demo-project:b1e6661e-e4f2-4156-9ab9-82a19EXAMPLE)をセット。

IFS=':' && set -- $ACCOUNT_ID
上記の値を区切り文字「:」で分解して配列化して「ACCOUNT_ID」にセット。

ACCOUNT_ID=$5
配列の5つ目にあるアカウントIDを「ACCOUNT_ID」にセット。

6
4
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
6
4