aws ecsをcodepipelineでデプロイするときに必要な定義類

デプロイする際の定義としては、最低限3つ必要。

  • appspec.yml
  • buildspec.yaml
  • taskdef.json

ベースのテンプレートはこのまま使用できる。

appspec.yml

version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: "<TASK_DEFINITION>"
        LoadBalancerInfo:
          ContainerName: "[コンテナの名称]"
          ContainerPort: [ポート番号]
        PlatformVersion: "1.4.0"

buildspec.yml

version: 0.2

env:
  variables:
      IMAGE_REPOSITORY_NAME: "[イメージのリポジトリの名称]"
      AWS_DEFAULT_REGION: "ap-northeast-1"
      AWS_ACCOUNT_ID: "[IAMアカウントID]"
      DOCKER_BUILDKIT: "1"
      Environment: "[環境名]"
      ContainerName: "[コンテナの名称]"
phases:
  pre_build:
    commands:
      ## ECRにログイン
       - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
      ## Dockerイメージのタグとして使用するため、Gitのコミットハッシュを取得
       - IMAGE_TAG=$CODEBUILD_RESOLVED_SOURCE_VERSION
      ## Push先リポジトリの指定
       - REPOSITORY_URI=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_REPOSITORY_NAME}
  build:
    commands:
      ## Dockerイメージのビルド
       - echo docker build -t $REPOSITORY_URI:$IMAGE_TAG .
       - docker build -t $REPOSITORY_URI:$IMAGE_TAG .
       - docker image ls

      ## DockerイメージのECRへのプッシュ
       - echo docker push $REPOSITORY_URI:$IMAGE_TAG
       - docker push $REPOSITORY_URI:$IMAGE_TAG

      ## ECS+CodeDeployにどのイメージを使用するか指示するためのファイルを作成
       - printf '{"Version":"1.0","ImageURI":"%s"}' $REPOSITORY_URI:$IMAGE_TAG > imageDetail.json
artifacts:
  files:
     - imageDetail.json
     - build/${Environment}/appspec.yml
     - build/${Environment}/taskdef.json

taskdef.json

{
    "ipcMode": null,
    "executionRoleArn": "[ロール]",
    "containerDefinitions": [
      {
        "dnsSearchDomains": null,
        "environmentFiles": null,
        "logConfiguration": {
          "logDriver": "awslogs",
          "secretOptions": null,
          "options": {
            "awslogs-group": "[cloudwatchのロググループ]",
            "awslogs-region": "ap-northeast-1",
            "awslogs-stream-prefix": "[出力の接頭辞]"
          }
        },
        "entryPoint": null,
        "portMappings": [
          {
            "hostPort": [ポート],
            "protocol": "tcp",
            "containerPort": [ポート]
          }
        ],
        "command": [],
        "linuxParameters": null,
        "cpu": 0,
        "environment": [
            {
                "name": "[パラメーター名]",
                "value": "[パラメーター値]"
            }
        ],
        "resourceRequirements": null,
        "ulimits": null,
        "dnsServers": null,
        "mountPoints": [],
        "secrets": [],
        "dockerSecurityOptions": null,
        "memory": null,
        "memoryReservation": null,
        "volumesFrom": [],
        "stopTimeout": null,
        "image": "<IMAGE_NAME>",
        "startTimeout": null,
        "firelensConfiguration": null,
        "dependsOn": null,
        "disableNetworking": null,
        "interactive": null,
        "healthCheck": null,
        "essential": true,
        "links": null,
        "hostname": null,
        "extraHosts": null,
        "pseudoTerminal": null,
        "user": null,
        "readonlyRootFilesystem": null,
        "dockerLabels": null,
        "systemControls": null,
        "privileged": null,
        "name": "[コンテナの名称]"
      }
    ],
    "placementConstraints": [],
    "memory": "[メモリ値]",
    "taskRoleArn": "[ロール]",
    "family": "[タスク定義の名称]",
    "pidMode": null,
    "requiresCompatibilities": [
      "FARGATE"
    ],
    "networkMode": "awsvpc",
    "runtimePlatform": {
      "operatingSystemFamily": "LINUX",
      "cpuArchitecture": null
    },
    "cpu": "[CPU値]",
    "inferenceAccelerators": null,
    "proxyConfiguration": null,
    "volumes": []
  }

hostPortcontainerPort 同じにする必要がある。

environment に環境変数を直接入力しているが、 environmentFiles でファイルを指定することもできる。

その際は、下記のようにする。

          "environmentFiles": [
            {
              "value": "[バケット名]/[環境ファイル名]",
              "type": "s3"
            }
          ],

参考

タスク定義パラメータ

memorycpu は下記を参照

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size