LoginSignup
0
1

terraform ECSのコード例

Posted at

前提

main.tfの1ファイルで作成する場合の簡易版メモ。

terraform ECSコード

main.tf
provider "aws" {
  region = "ap-northeast-1"  # AWSリージョンを適切な値に変更
}

resource "aws_ecs_cluster" "my_cluster" {
  name = "my-ecs-cluster"  # ECSクラスターの名前
}

resource "aws_ecs_task_definition" "my_task" {
  family                   = "my-ecs-app"  # タスク定義のファミリー名
  network_mode             = "awsvpc"      # VPCネットワークモードを指定
  requires_compatibilities = ["FARGATE"]   # Fargateタスクとして実行

  execution_role_arn = aws_iam_role.ecs_execution_role.arn

  container_definitions = jsonencode([{
    name  = "my-container"
    image = "nginx:latest"  # コンテナイメージを指定

    portMappings = [{
      containerPort = 80  # コンテナのポートを設定
      hostPort      = 80  # ホストのポートを設定
    }]
  }])
}

resource "aws_iam_role" "ecs_execution_role" {
  name = "ecs-execution-role"
  
  assume_role_policy = jsonencode({
    Version = "2012-10-17",
    Statement = [{
      Action = "sts:AssumeRole",
      Effect = "Allow",
      Principal = {
        Service = "ecs-tasks.amazonaws.com"
      }
    }]
  })
}

resource "aws_iam_policy_attachment" "ecs_execution_policy_attachment" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
  role       = aws_iam_role.ecs_execution_role.name
}

resource "aws_ecs_service" "my_service" {
  name            = "my-ecs-service"
  cluster         = aws_ecs_cluster.my_cluster.id
  task_definition = aws_ecs_task_definition.my_task.arn
  launch_type     = "FARGATE"  # Fargateを使用
  network_configuration {
    subnets = ["subnet-12345678"]  # サブネットIDを指定
    security_groups = [aws_security_group.my_security_group.id]
  }
  depends_on = [aws_ecs_task_definition.my_task]
}

resource "aws_security_group" "my_security_group" {
  name        = "my-security-group"
  description = "My Security Group for ECS"
  
  // セキュリティグループの設定を追加
}

コードの例:

ECSクラスターを作成し、Fargateタスクを定義しています。

ECSタスクはNginxコンテナを使用し、指定されたVPC内のサブネットとセキュリティグループに配置されます。

terraform ECSコード 詳しめに解説

main.tf
# AWSプロバイダーの設定
provider "aws" {
  region = "ap-northeast-1"  # 使用するAWSリージョンを指定
}

# ECSクラスターの作成
resource "aws_ecs_cluster" "my_cluster" {
  name = "my-ecs-cluster"  # ECSクラスターの名前
}

# ECSタスク定義の作成
resource "aws_ecs_task_definition" "my_task" {
  family                   = "my-ecs-app"  # タスク定義のファミリー名
  network_mode             = "awsvpc"      # VPCネットワークモードを指定
  requires_compatibilities = ["FARGATE"]   # Fargateタスクとして実行

  # タスク実行ロールのARNを指定
  execution_role_arn = aws_iam_role.ecs_execution_role.arn

  # タスクのコンテナ定義
  container_definitions = jsonencode([{
    name  = "my-container"
    image = "nginx:latest"  # 使用するコンテナイメージ

    portMappings = [{
      containerPort = 80  # コンテナ内のポート
      hostPort      = 80  # ホストのポート
    }]
  }])
}

# タスク実行ロールの作成
resource "aws_iam_role" "ecs_execution_role" {
  name = "ecs-execution-role"
  
  # タスク実行ロールの信頼ポリシーを指定 
  assume_role_policy = jsonencode({
    Version = "2012-10-17",
    Statement = [{
      Action = "sts:AssumeRole",
      Effect = "Allow",
      Principal = {
        Service = "ecs-tasks.amazonaws.com"
      }
    }]
  })
}

# タスク実行ロールにAmazon ECSタスク実行ポリシーをアタッチ
resource "aws_iam_policy_attachment" "ecs_execution_policy_attachment" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
  role       = aws_iam_role.ecs_execution_role.name
}

# ECSサービスの作成
resource "aws_ecs_service" "my_service" {
  name            = "my-ecs-service"
  cluster         = aws_ecs_cluster.my_cluster.id
  task_definition = aws_ecs_task_definition.my_task.arn
  launch_type     = "FARGATE"  # Fargateを使用

  # タスクが配置されるVPCとセキュリティグループを指定
  network_configuration {
    subnets = ["subnet-12345678"]  # サブネットIDを指定
    security_groups = [aws_security_group.my_security_group.id]
  }
  depends_on = [aws_ecs_task_definition.my_task]
}

# セキュリティグループの作成
resource "aws_security_group" "my_security_group" {
  name        = "my-security-group"
  description = "My Security Group for ECS"
  
  # セキュリティグループの設定を追加
}

既存のセキュリティグループアタッチ方法

main.tf
# 既存のセキュリティグループIDを指定(例: sg-0123456789abcdef0)
variable "existing_security_group_id" {
  description = "既存のセキュリティグループID"
  type        = string
  default     = "sg-0123456789abcdef0"  # 実際のセキュリティグループIDに置き換え
}

# ...

# ECSサービスの作成
resource "aws_ecs_service" "my_service" {
  name            = "my-ecs-service"
  cluster         = aws_ecs_cluster.my_cluster.id
  task_definition = aws_ecs_task_definition.my_task.arn
  launch_type     = "FARGATE"  # Fargateを使用

  # 既存のセキュリティグループIDを指定してセキュリティグループをアタッチ
  network_configuration {
    subnets = ["subnet-12345678"]  # サブネットIDを指定
    security_groups = [var.existing_security_group_id]  # 既存のセキュリティグループIDを使用
  }
  depends_on = [aws_ecs_task_definition.my_task]
}
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