構成
CodeBuildからVPC内のリソースにアクセスするためには以下のような構成をとる場合が多いと思います。
プライベートサブネットを作成して、インターネット接続のためにNAT gatewayを配置:
この他にVPC外のCodeBuildからexecute-commandを経由してVPC内リソースにアクセスする方法もあります。
ECSのコンテナを立てる時間があるので、build時間が1~2分長くなりますが、Nat gatewayは0.062USD/hourくらいかかるので料金の節約になります。
設定例
ここでは例として、VPC内で./bin/rails db:migrate
をexecute-commandを利用して実行します。
- buildspec.yml
version: 0.2phases: install: commands: # session-manager-pluginを入れておく - curl https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb -o /tmp/session-manager-plugin.deb - dpkg -i /tmp/session-manager-plugin.deb pre_build: commands: - BASE_URI=xxxxxxxxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com - aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin $BASE_URI - REPOSITORY_URI=$BASE_URI/$ENV-example:latest build: commands: - docker build -t $REPOSITORY_URI . - docker push $REPOSITORY_URI post_build: commands: - ./db-migrate.sh
- db-migrate.sh
#!/bin/bashset -euxo pipefail# taskが残留しないように1時間で落ちるようにしておくcat <<EOF > /tmp/overrides.json { "containerOverrides": [ { "name": "app", "command": ["sleep", "1h"] } ] }EOFtask_arn=$(aws ecs run-task --cluster example-cluster --count 1 --launch-type FARGATE \ --enable-execute-command \ --network-configuration "awsvpcConfiguration={subnets=[subnet-xxxxxxxx,subnet-xxxxxxxx,subnet-xxxxxxxx],securityGroups=[sg-xxxxxxxxxxxxxxxxx],assignPublicIp=ENABLED}" \ --task-definition example \ --output json \ --overrides file:///tmp/overrides.json | jq -r .tasks[0].containers[0].taskArn)echo "wait for task running"until [ "$(aws ecs describe-tasks --cluster example-cluster --tasks $task_arn | jq -r .tasks[0].containers[0].lastStatus)" = "RUNNING" ]do echo -n "." sleep 30done# RUNNINGになっても、しばらく待たないと、execute-commandできないsleep 30# 実行aws ecs execute-command \ --cluster example-cluster \ --task $task_arn \ --interactive \ --command "./bin/rails db:migrate"# 停止aws ecs stop-task --cluster example-cluster --task $task_arn
関連記事
タグ
この記事を書いた人
yamasita
東京電機大学工学部→3年間某SIerにて銀行システムの開発→bpsに入社
yamasitaの書いた記事一覧へ
本記事の内容へのお問い合せはTwitterで@techrachoへMentionまたはDMにてご連絡頂くか、運営会社であるBPS株式会社のお問い合せフォームよりお問い合せ下さい。