[AWS][ECS]TerraformでのSSM parameter store/Secrets Managerの運用方法
環境は上記のブログより。今回はAppとNginxコンテナを分離して、環境変数はSSM parameter store/Secrets Managerで管理している想定。
- .github/workflows/deploy-stg.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
name: Deploy-stg on: workflow_dispatch: inputs: name: description: 'Name' required: true permissions: id-token: write contents: read env: AWS_ROLE_NAME: githubactions-oidc AWS_ROLE_SESSION_NAME: githubactions-oidc AWS_REGION: ap-northeast-1 SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} SLACK_USERNAME: deploy-stg SLACK_CHANNEL: hoge SLACK_ICON: https://avatars.githubusercontent.com/u/44036562?s=280&v=4 jobs: deploy-ecs: name: deploy-ecs runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ env.AWS_ROLE_NAME }} role-session-name: ${{ env.AWS_ROLE_SESSION_NAME }} aws-region: ${{ env.AWS_REGION }} - name: Download file from S3 .env run: | aws s3 cp s3://hoge/stg/env ./.env --quiet - name: Login ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Build, tag, and push image to ECR id: build-image run: | docker build -f docker/stg/app/Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG . docker build -f docker/stg/nginx/Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY_NGINX:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG docker push $ECR_REGISTRY/$ECR_REPOSITORY_NGINX:$IMAGE_TAG echo "::set-output name=image-app::$ECR_REGISTRY/$ECR_REPOSITORY_APP:$IMAGE_TAG" echo "::set-output name=image-nginx::$ECR_REGISTRY/$ECR_REPOSITORY_NGINX:$IMAGE_TAG" env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ github.sha }} ECR_REPOSITORY_APP: stg-hoge-app ECR_REPOSITORY_NGINX: stg-hoge-nginx - name: New image ID ECS task definition app id: task-def-app uses: aws-actions/amazon-ecs-render-task-definition@v1 with: task-definition: ${{ env.ECS_TASK_DEFINITION }} container-name: ${{ env.CONTAINER_NAME_APP }} image: ${{ steps.build-image.outputs.image-app }} env: ECS_TASK_DEFINITION: docker/stg/task-definitions/github-actions.json CONTAINER_NAME_APP: stg-hoge-app - name: New image ID ECS task definition nginx id: task-def-nginx uses: aws-actions/amazon-ecs-render-task-definition@v1 with: task-definition: ${{ steps.task-def-app.outputs.task-definition }} container-name: ${{ env.CONTAINER_NAME_NGINX }} image: ${{ steps.build-image.outputs.image-nginx }} env: CONTAINER_NAME_NGINX: stg-hoge-nginx - name: Deploy ECS task definition uses: aws-actions/amazon-ecs-deploy-task-definition@v1 with: task-definition: ${{ steps.task-def-nginx.outputs.task-definition }} service: ${{ env.ECS_SERVICE }} cluster: ${{ env.ECS_CLUSTER }} wait-for-service-stability: true env: ECS_SERVICE: stg-hoge-service ECS_CLUSTER: stg-hoge - name: Slack Success if: ${{ success() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: Deploy stg Success SLACK_COLOR: good - name: Slack Failure if: ${{ failure() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: Deploy stg Failure SLACK_COLOR: danger - name: Run ECS task migration id: run-task uses: yyoshiki41/ecs-run-task-action@v0.0.8 with: task-definition: ${{ teps.task-def-nginx.outputs.task-definition }} task-definition-family: stg-hoge cluster: ${{ env.ECS_CLUSTER }} subnets: '["subnet-xxxxxx","subnet-xxxxxxx"]' security-groups: '["sg-xxxxxx","sg-xxxxxx"]' container-name: ${{ env.CONTAINER_NAME }} command: '["bundle","exec","rake","db:migrate","RAILS_ENV=staging"]' env: ECS_CLUSTER: stg-hoge ECS_SERVICE: hoge-service CONTAINER_NAME: stg-hoge-app - name: Slack Success if: ${{ success() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: Deploy stg Success SLACK_COLOR: good - name: Slack Failure if: ${{ failure() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: Deploy stg Failure SLACK_COLOR: danger |
- github-actions.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
{ "family": "stg_hoge", "taskRoleArn": "arn:aws:iam::xxxxxxx:role/ecsTaskExecutionRole", "executionRoleArn": "arn:aws:iam::xxxxxxx:role/ecsTaskExecutionRole", "networkMode": "awsvpc", "requiresCompatibilities": ["FARGATE"], "cpu": "1024", "memory": "2048", "containerDefinitions": [ { "name": "stg-hoge-app", "image": "xxxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/stg-app", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/stg-app", "awslogs-region": "ap-northeast-1", "awslogs-stream-prefix": "ecs" } }, "essential": true, "cpu": 512, "memory": 1024, "memoryReservation": 1024, "portMappings": [ { "hostPort": 3000, "containerPort": 3000, "protocol": "tcp" } ], "command": ["yarn", "dev"], "secrets": [ { "name": "DATABASE_URL", "valueFrom": "arn:aws:ssm:ap-northeast-1:xxxx:parameter/stg_db_url" }, ] }, { "name": "stg-hoge-nginx", "image": "xxxxx.dkr.ecr.ap-northeast-1.amazonaws.com/stg-nginx", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "/ecs/stg-nginx", "awslogs-region": "ap-northeast-1", "awslogs-stream-prefix": "ecs" } }, "essential": true, "cpu": 512, "memory": 1024, "memoryReservation": 1024, "portMappings": [ { "hostPort": 80, "containerPort": 80, "protocol": "tcp" } ], "command": ["/usr/sbin/nginx", "-g", "daemon off;"] } ] } |
コンテナを分離している場合はechoでのoutputsやidでの管理が重要。タスク定義は元のJSONファイルから最後のNginxでoutputしている。
- 並列化の場合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
name: Stg-Deploy-ecs-app on: workflow_dispatch: inputs: name: description: 'Name' required: true permissions: id-token: write contents: read env: SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} SLACK_USERNAME: deploy-stg-app SLACK_CHANNEL: deploy SLACK_ICON: https://avatars.githubusercontent.com/u/44036562?s=280&v=4 AWS_ROLE_NAME: githubactions-oidc AWS_ROLE_SESSION_NAME: githubactions-oidc AWS_REGION: ap-northeast-1 ECR_REPOSITORY_APP: stg-app ECR_REPOSITORY_NGINX: stg-nginx jobs: build-app: name: Build App Image runs-on: ubuntu-latest outputs: image: ${{ steps.app-image.outputs.image }} steps: - name: Checkout uses: actions/checkout@v3 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ env.AWS_ROLE_NAME }} role-session-name: ${{ env.AWS_ROLE_SESSION_NAME }} aws-region: ${{ env.AWS_REGION }} - name: Download file from S3 .env run: | aws s3 cp s3://hoge.com/app/stg-env ./.env --quiet - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '16.x' - name: Run npm install and build run: | npm install npm install --prefix ./resources/frontend/btob npm run build-deploy - name: Login ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Build and Push App Image id: app-image run: | docker build -f docker/stg/app/Dockerfile -t $ECR_REGISTRY/stg-app:$IMAGE_TAG . docker push $ECR_REGISTRY/stg-app:$IMAGE_TAG echo "::set-output name=image::$ECR_REPOSITORY:$IMAGE_TAG" env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ env.ECR_REPOSITORY_APP}} IMAGE_TAG: ${{ github.sha }} build-nginx: name: Build Nginx Image runs-on: ubuntu-latest outputs: image: ${{ steps.nginx-image.outputs.image }} steps: - name: Checkout uses: actions/checkout@v3 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ env.AWS_ROLE_NAME }} role-session-name: ${{ env.AWS_ROLE_SESSION_NAME }} aws-region: ${{ env.AWS_REGION }} - name: Login ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Build and Push Nginx Image id: nginx-image run: | docker build -f docker/stg/nginx/Dockerfile -t $ECR_REGISTRY/stg-nginx:$IMAGE_TAG . docker push $ECR_REGISTRY/stg-nginx:$IMAGE_TAG echo "::set-output name=image::$ECR_REPOSITORY:$IMAGE_TAG" env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ env.ECR_REPOSITORY_NGINX}} IMAGE_TAG: ${{ github.sha }} deploy-app: name: Deploy App Service needs: - build-app - build-nginx runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::${{ secrets.AWS_ACCOUNT_ID }}:role/${{ env.AWS_ROLE_NAME }} role-session-name: ${{ env.AWS_ROLE_SESSION_NAME }} aws-region: ${{ env.AWS_REGION }} - name: Login ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v1 - name: Render Task Definition id: task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 with: task-definition: docker/stg/task-definitions/github-actions-app.json container-name: stg-app image: ${{ steps.login-ecr.outputs.registry }}/${{ needs.build-app.outputs.image }} - name: Add Nginx Container id: nginx-task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 with: task-definition: ${{ steps.task-def.outputs.task-definition }} container-name: stg-nginx image: ${{ steps.login-ecr.outputs.registry }}/${{ needs.build-nginx.outputs.image }} - name: Deploy ECS Service id: deploy-ecs uses: aws-actions/amazon-ecs-deploy-task-definition@v2 with: task-definition: ${{ steps.nginx-task-def.outputs.task-definition }} cluster: stg-cluster service: stg-app-service wait-for-service-stability: true - name: Notify Success if: ${{ success() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: App Deployment Success SLACK_COLOR: good SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} - name: Notify Failure if: ${{ failure() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: App Deployment Failure SLACK_COLOR: danger SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} - name: Run ECS Task Migration id: run-task uses: yyoshiki41/ecs-run-task-action@v0.0.8 with: task-definition: ${{ steps.nginx-task-def.outputs.task-definition }} task-definition-family: stg-app cluster: ${{ env.ECS_CLUSTER }} subnets: '["subnet-xxxxx","subnet-xxxx"]' security-groups: '["sg-xxxxxx","sg-xxxxx"]' container-name: ${{ env.CONTAINER_NAME }} command: '["php", "artisan", "migrate", "--force"]' env: ECS_CLUSTER: stg-cluster CONTAINER_NAME: stg-app - name: Notify Success if: ${{ success() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: Migration stg Success SLACK_COLOR: good - name: Notify Failure if: ${{ failure() }} uses: rtCamp/action-slack-notify@v2 env: SLACK_TITLE: Migration stg Failure SLACK_COLOR: danger |
並列化したほうがデプロイ速度が約10分高速化されるので必須。
Was this helpful?
0 / 0
1989年生まれのFindy/SRE。ホスティングから大規模なアドテクなどのインフラエンジニアとして携わる。現在はサービスの信頼性向上、DevOps、可用性、レイテンシ、パフォーマンス、モニタリング、オブザーバビリティ、緊急対応、AWSでのインフラ構築、Docker開発環境の提供、IaC、新技術の検証、リファクタリング、セキュリティ強化、分析基盤の運用などを担当している。個人事業主では数社サーバー保守とベンチャー企業のSREインフラコンサルティングやMENTA/TechBullで未経験者にインフラのコーチング/コミュニティマネージャーとして立ち上げと運営をしている。また、過去「脆弱性スキャナVuls」のOSS活動もしており、自称エバンジェリスト/技術広報/テクニカルサポート/コントリビュータでもある。