5
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.

Fargate+Secrets Managerで環境変数管理

Last updated at Posted at 2021-07-24

前提

AWS CLIが入っていること

create secret

create secretをコマンドラインから行います。
createすると以下のように返されるはずです。

aws secretsmanager create-secret --region ap-northeast-1 --name DATABASE_URL     --description "AWS Database URL"     --secret-string xxxx.yyyyyy.ap-northeast-1.rds.amazonaws.com
{
    "ARN": "arn:aws:secretsmanager:ap-northeast-1:xxxxxx:secret:DATABASE_URL-xxxxxx",
    "Name": "DATABASE_URL",
    "VersionId": "7c6cd5b1-e4ae-415f-zzzzzzz-xxxxxxxx"
}

AWS Secrets Managerに行くと、登録されているのが確認できます。

Screenshot from 2021-07-25 00-33-37.png

Secrets Manager読み取り権限の付与

読み取りのポリシーを追加します。(自分の場合は ecstaskexecutionrole)
Attach inline policyから追加しました。たぶんここもスマートな方法があるのかと思いますが、とりあえずこれで、、、

Screen Shot 2021-07-25 at 16.54.25.png

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "secretsmanager:GetSecretValue",
            "Resource": "arn:aws:secretsmanager:ap-northeast-1:803801015105:secret:DATABASE_URL-xxxxxx"
        }
    ]
}

task definitionの整理

これをfargateのtask definitionのjsonファイルに貼り付けます。

task-definition.json
# 必要なコンテナの定義に追加
"secrets": [
  {
    "name": "DATABASE_URL",
    "valueFrom": "arn:aws:secretsmanager:ap-northeast-1:803801015105:secret:DATABASE_URL-xxxxxx"
  }
]

参考

5
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
5
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?