Elastic Beanstalk で Docker マルチステージビルドの動的環境変数を設定するにはどうすればよいですか?

所要時間3分
0

AWS Elastic Beanstalk で Docker マルチステージビルドの動的環境変数を設定したいと考えています。

簡単な説明

Dockerfile にハードコーディングされた変数/静的変数を設定できるのは、ビルドステージのみです。たとえば、ENV キーを使用して静的変数を設定します。これより前のステージで構築された Dockerfile 内の他のコンテナからは、Elastic Beanstalk 変数にアクセスできません。マルチステージビルド中に動的環境変数を設定するには、AWS Systems Manager AWS:: SSM::パラメータリソースまたは Elastic Beanstalk 変数を使用します。変数は 1 度しか設定する必要がないため、SSM パラメータを使用することがベストプラクティスです。

注: AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、「AWS CLI エラーのトラブルシューティング」を参照してください。また、AWS CLI の最新バージョンを使用していることを確認してください。

解決策

注: 以下の解決方法は、Alpine Linux に React アプリケーションがインストールされていることを前提としています。他のディストリビューションのパッケージマネージャーを使用してコンテナを構築し、必要なパッケージをインストールすることも可能です。

SSM パラメータを使用する

注: Amazon Elastic Compute Cloud (Amazon EC2) Linux インスタンスが SSM パラメータを取得できるようにするには、EC2 Linux インスタンスプロファイルに ssm:GetParameters 許可が必要となります。

1.    Systems Manager コンソールを使用して SSM パラメータを作成します。または、put-parameter の AWS CLI コマンドを実行します。

aws ssm put-parameter --name PRODURL_SSM --value http://myproddomain.com --type String

2.    以下の Dockerfile を使用してアプリケーションをデプロイします。Dockerfile は、一時コンテナまたはビルドコンテナ内の SSM パラメータ値を取得してから、ビルド中に変数をエクスポートします。

# Build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install react-scripts@3.4.1
COPY . ./
## Install the required packages for awscli, curl, and jq. ##
RUN apk -v --no-cache add \
        py-pip \
        curl \
        jq \
        && \
        pip install awscli --upgrade --user
## Export the region dynamically retrieving it from the instance metadata, and then retrieve the SSM parameter value using awscli. Then, place the parameter in a text file, and export the variable using the value from the text file. Finally, run the build. ##
RUN TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` && export REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region)&& /root/.local/bin/aws ssm get-parameters --names PRODURL_SSM --region $REGION | grep Value | cut -d '"' -f4 >> /tmp/SSMParameter.txt && export DYNAMIC_SSM_VAR=$(cat /tmp/SSMParameter.txt) && npm run build
# Production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
## Copy the text file that has the SSM parameter value from the build container to the production container to confirm the variable was retrieved successfully. ##COPY --from=build /tmp/SSMParameter.txt /tmp
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

3.    ビルドコンテナからコピーしたファイルをチェックして、デプロイメントが正常に行われたことを確認します。このファイルには、SSM パラメータ変数の値が含まれている必要があります。

例:

# docker exec -it <Container-Id> sh# cat /tmp/SSMParameter.txt
http://myproddomain.com

Elastic Beanstalk 変数を使用する

注: インスタンスプロファイルを環境に取り込むには、インスタンスプロファイルにelasticbeanstalk:DescribeConfigurationSettings AWS Identity and Access Management (IAM) アクセス許可が必要となります。

1.    **Elastic Beanstalk Command Line Interface (EB CLI)**で使用される config.yml ファイルをプロジェクトルートディレクトリにコピーします。

例:

cp .elasticbeanstalk/config.yml .

2.    EB CLI を使用して Elastic Beanstalk 変数を設定するには、以下のコマンドを実行して更新する、または既存の環境にデプロイします。

eb setenv PRODURL_EB=http://myproddomain.com

3.    新しい Elastic Beanstalk 環境を作成するには、プロジェクトルートディレクトリにある config.yml ファイルに新しい環境名を追加し、以下のコマンドを実行します。

eb create --envvars PRODURL_EB=http://myproddomain.com

4.    以下の Dockerfile を使用してアプリケーションをデプロイします。Dockerfile がビルドコンテナ内の Elastic Beanstalk 変数の値を取得し、ビルド中にその変数をエクスポートします。

# Build environmentFROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install react-scripts@3.4.1
COPY . ./
## Create an .elasticbeanstalk directory to place the config.yml file in, so that the eb cli can interact with the Elastic Beanstalk environment. ##
RUN mkdir .elasticbeanstalk
## Copy config.yml to /app/.elasticbeanstalk inside the build container as it will be used by eb cli ##
COPY config.yml /app/.elasticbeanstalk
## Install required packages for awsebcli ##
RUN apk -v --no-cache add \
        gcc \
        musl-dev \
        openssl \
        openssl-dev \
        make \
        py-pip \
        libffi-dev \
        python \
        python-dev \
        && \
        pip install awsebcli --upgrade --user
## Retrieve the Elastic Beanstalk variable using awsebcli and place it in a text file. Then, export the desired variable using the value from the text file, then run the build. ##
RUN /root/.local/bin/eb printenv | grep PRODURL_EB | awk '{print $3}' >> /tmp/EBVar.txt && export DYNAMIC_EB_VAR=$(cat /tmp/EBVar.txt) && npm run build
# Production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
## Copy the text file that has the Elastic Beanstalk variable value from the build container to the production container to confirm the variable was retrieved successfully ##
COPY --from=build /tmp/EBVar.txt /tmp
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

5.    ビルドコンテナからコピーしたファイルをチェックして、デプロイメントが正常に行われたことを確認します。このファイルには、Elastic Beanstalk 変数の値が含まれている必要があります。

# docker exec -it <Container-Id> sh# cat /tmp/EBVar.txt
http://myproddomain.com

関連情報

インスタンスアイデンティティドキュメント

get-parameters

AWS公式
AWS公式更新しました 6ヶ月前
コメントはありません

関連するコンテンツ