0
0

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.

Drone CI: 別の IAM ロールで AWS にアップロード・デプロイする

Last updated at Posted at 2021-04-23

Drone Server を EC2 で動かした場合にインスタンスロールに直接権限を持たせるのは避け、パイプライン内で必要に応じて別の IAM ロールに Switch Role する。

IAM ロールの作成

Drone Server から Switch Role する IAM ロールの Trust Relationship に、インスタンスロールを追加する。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::xxxxxxxxxxxx:role/xxxx"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

インスタンスロールには AssumeRole 権限を付与しておく。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::*:role/*",
            "Effect": "Allow"
        }
    ]
}

Switch Role

プラグインを使用する場合

nodefortytwo/drone-aws-role-auth プラグインで AssumeRole をおこない、取得した認証情報をファイルに保存する。ロール権限が必要なステップで、そのファイルを読み込む。

steps:
  # AssumeRole
  - name: awsrole
    image: nodefortytwo/drone-aws-role-auth
    settings:
      role: arn:aws:iam::xxxxxxxxxxxx:role/xxxx
      file: .awsrole_env

  # AWS へアップロード・デプロイするステップ
  - name: ...
    image: amazon/aws-cli
    commands:
      - . ./.awsrole_env
      - aws s3 cp ...

プラグインを使用しない場合

以下のように自前でやっても良い。

steps:
  - name: ...
    image: amazon/aws-cli
    commands:
      - |
        AWS_IAM_ROLE=arn:aws:iam::xxxxxxxxxxxx:role/xxxx
        CREDS=$(aws sts assume-role --duration-seconds 900 \
          --role-arn "$${AWS_IAM_ROLE}" \
          --role-session-name="${DRONE_REPO_OWNER}-${DRONE_REPO_NAME}")
        AWS_ACCESS_KEY_ID=$(echo "$${CREDS}" | jq -r '.Credentials.AccessKeyId')
        AWS_SECRET_ACCESS_KEY=$(echo "$${CREDS}" | jq -r '.Credentials.SecretAccessKey')
        AWS_SESSION_TOKEN=$(echo "$${CREDS}" | jq -r '.Credentials.SessionToken')
        export AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
      - aws s3 cp ...
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?