0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人的備忘録:ECS タスク定義を JSON と Terraform で書き比べた自分用メモ

Posted at

はじめに

ECS(Elastic Container Service)でアプリケーションをデプロイする際には、タスク定義(Task Definition)を作成する必要があります。

個人の備忘録程度の走り書きとなっておりますが、温かい目で見守っていただければ幸いです。

今回は、FastAPI アプリケーションを Fargate 上で動かすためのタスク定義を JSON 形式と Terraform 形式の2種類で整理しました。

書こうと思ったきっかけ

受講しているITスクールのハッカソンの開発の一環でECSとFargateを利用したアプリケーションのデプロイを行っており、作業中に得た知識を忘れないように備忘録としてまとめました。

内容(あくまでサンプル)

✅ JSON形式(CLI / API用)

AWS CLIやAPI経由で登録する際に使用できるJSON形式のタスク定義です。

{
  "family": "fastapi-task",
  "taskRoleArn": "arn:aws:iam::xxx:role/ecsTaskRole",
  "executionRoleArn": "arn:aws:iam::xxx:role/ecsTaskExecutionRole",
  "networkMode": "awsvpc",
  "requiresCompatibilities": ["FARGATE"],
  "cpu": "256",
  "memory": "512",
  "containerDefinitions": [
    {
      "name": "fastapi",
      "image": "xxx.dkr.ecr.ap-northeast-1.amazonaws.com/hackathon:latest",
      "essential": true,
      "cpu": 0,
      "portMappings": [
        {
          "containerPort": 8000,
          "hostPort": 8000,
          "protocol": "tcp"
        }
      ],
      "environment": [],
      "mountPoints": [],
      "volumesFrom": [],
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "/aws/ecs/reverse-proxy-cluster",
          "awslogs-region": "ap-northeast-1",
          "awslogs-stream-prefix": "ecs"
        }
      },
      "systemControls": []
    }
  ],
  "volumes": [],
  "placementConstraints": [],
  "tags": []
}

✅ Terraform形式

Terraformを使ってIaC(Infrastructure as Code)で管理するためのタスク定義リソースです。

resource "aws_ecs_task_definition" "fastapi" {
  family                   = "fastapi-task"
  task_role_arn            = "arn:aws:iam::xxx:role/ecsTaskRole"
  execution_role_arn       = "arn:aws:iam::xxx:role/ecsTaskExecutionRole"
  network_mode             = "awsvpc"
  requires_compatibilities = ["FARGATE"]
  cpu                      = "256"
  memory                   = "512"

  container_definitions = jsonencode([
    {
      name      = "fastapi"
      image     = "xxx.dkr.ecr.ap-northeast-1.amazonaws.com/hackathon:latest"
      cpu       = 0
      essential = true
      portMappings = [
        {
          containerPort = 8000
          hostPort      = 8000
          protocol      = "tcp"
        }
      ]
      environment       = []
      mountPoints       = []
      volumesFrom       = []
      systemControls    = []
      logConfiguration = {
        logDriver = "awslogs"
        options = {
          awslogs-group         = "/aws/ecs/reverse-proxy-cluster"
          awslogs-region        = "ap-northeast-1"
          awslogs-stream-prefix = "ecs"
        }
      }
    }
  ])

  tags = {}
}

まとめ

FastAPIをFargateで動かすにあたって必要なタスク定義のフォーマットを、CLI用とTerraform用の2パターンで整理しました。

インフラの自動化や再利用性を高めるために、Terraform形式での管理がおすすめです。

今後も似た構成を使う際に素早く再利用できるよう、今回の定義をベースにしていきます...!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?