2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CodeBuild上のDockerイメージにメタデータから取得したAWS認証情報を渡す方法

Posted at

CodeBuild上でビルドしたDockerコンテナからAWSの認証情報が上手く使えないみたいなので調べてみました。

そもそも http://169.254.169.254 が使えなくて、参考URLにあるように http://169.254.170.2 から取得します。更に AWS_CONTAINER_CREDENTIALS_RELATIVE_URI の環境変数を組み合わせると認証情報が取得できます。

$ curl http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
{
    "AccessKeyId": "ACCESS_KEY_ID",
    "Expiration": "EXPIRATION_DATE",
    "RoleArn": "TASK_ROLE_ARN",
    "SecretAccessKey": "SECRET_ACCESS_KEY",
    "Token": "SECURITY_TOKEN_STRING"
}

ちなみに AWS_CONTAINER_CREDENTIALS_RELATIVE_URI の変数には以下のような値が入っています。

$ echo $AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
/v2/credentials/xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx

上記踏まえた上で認証情報をDockerコンテナにAWSの認証情報渡す buildspec.yml の例です。

version: 0.2

phases:
  build:
    commands:
# 省略----------------------------
      - export NO_PROXY=169.254.170.2 # Proxyがある場合
      - AWS_CRED=`curl http://169.254.170.2${AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}`
      - AWS_ACCESS_KEY_ID=`echo ${AWS_CRED} | jq -r .AccessKeyId`
      - AWS_SECRET_ACCESS_KEY=`echo ${AWS_CRED} | jq -r .SecretAccessKey`
      - AWS_SESSION_TOKEN=`echo ${AWS_CRED} | jq -r .Token`
      - |
        docker run \
        -e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} \
        -e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} \
        -e AWS_SESSION_TOKEN=${AWS_SESSION_TOKEN} \
        ${IMAGE_NAME} ${COMMAND}

参考URL

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?