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

Elastic BeanstalkでSSM ParameterStoreからパラメータ取得しようとして詰まったこと

Posted at

経緯

  • EBのマルチコンテナ環境って結局裏でECS動いてるだけだから、大体ECSの機能使えるだろと思ってた
  • もともとEBの環境プロパティにDBの接続情報をぶち込んでいたが、平文であんまりよろしくないので、ParameterStoreに移そうと話が上がった。
  • ECSでParameterStoreを利用したケースはあり、EBのMultiContainerは実質ECSで動作するがわかっていたため、タスク定義に指定すればおわりやろとおもっていた。

Dockerrun.aws.jsonで指定した場合

  • EBのDockerrun.aws.jsonのContainerDifinissionのformatはECSとおなじとのことで、containerDefinitionsにsecretを追加して、EBのサービスRoleにParameterStoreを取得できるようポリシー設定した。
  • executionRoleArn はECSタスク実行用のRole、ここを参考にIAMポリシーを付与したRolearnを設定
  • 結果的に executionRoleArn はDockerrun.aws.jsonでは無効なプロパティ扱いされ、executionRoleArn を指定しろというECSのエラーが表示された。フォーマットが一緒のため検証時点ではエラーにならないが、適用されない模様。
  • ECSタスクの実行Roleは指定できず、最終的は別の方法をとった
Dockerrun.aws.json
{
    "AWSEBDockerrunVersion": 2,
    "executionRoleArn": "arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole"
    "containerDefinitions": [
        {
            "name": "api",
            "image": "xxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/eb-secret:latest",
            "essential": true,
            "memory": 128,
            "portMappings": [
                {
                    "hostPort": 80,
                    "containerPort": 5000
                }
            ],
            "environment": [],
            "secrets": [{
                "name": "environment_variable_name",
                "valueFrom": "arn:aws:ssm:region:aws_account_id:parameter/parameter_name"
    }]
        }
    ]
  }

entrypoint.shでコンテナの環境変数に展開する方法

― この方法ではコンテナ内にAWSCLI(とjq)を用意する必要がある
- 最終的にこの方法を利用した

entrypoint.sh
#!/bin/bash
export AWS_DEFAULT_REGION = ap-northeast-1
export AWS_DEFAULT_OUTPUT = json
export ENVIRONMENT_VARIABLE = $(aws ssm get-parameter --name parameter_name \
                                --with-decryption | jq -r '.Parameters[].Value')

要約

  • Elastic BeanstalkのMultiContainer環境ではコンテナ定義によってSSM ParameterStoreから取得したパラメータを環境変数に格納することはできないっぽい?
  • ParameterStoreからDBの接続情報を取得するためには、コンテナ起動時にentorypoint.shでawscliを利用して、環境変数に格納する
  • もしくは、アプリから直接SSM ParameterStoreに取りに行く必要がある
  • 複数台一気にアプリを立ち上げるような場合はParameterStoreのスループット上限を気にするように。標準は40tps、引き上げは可能
1
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
1
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?