[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している。
Was this helpful?
0 / 0
1989年生まれのFindy/SRE。ホスティングから大規模なアドテクなどのインフラエンジニアとして携わる。現在はサービスの信頼性向上、DevOps、可用性、レイテンシ、パフォーマンス、モニタリング、オブザーバビリティ、緊急対応、AWSでのインフラ構築、Docker開発環境の提供、IaC、新技術の検証、リファクタリング、セキュリティ強化、分析基盤の運用などを担当している。個人事業主では数社サーバー保守とベンチャー企業のSREインフラコンサルティングやMENTA/TechBullで未経験者にインフラのコーチング/コミュニティマネージャーとして立ち上げと運営をしている。また、過去「脆弱性スキャナVuls」のOSS活動もしており、自称エバンジェリスト/技術広報/テクニカルサポート/コントリビュータでもある。