大网数据广告活动
服务器运维问答

TERRAFORM how do i have 1 ecs cluster with 2 or more ecs service/task definitions?

· 52CE 我爱测

Using Terraform, I have tried the hardest to find out how to create 1 ECS cluster and have multiple services running under it. SO basically i have 2 different containers i want to run with this 1 ECS cluster. How do i do this? I …

问题原文

原帖链接 · uberrebu · CC BY-SA 3.0

Using Terraform, I have tried the hardest to find out how to create 1 ECS cluster and have multiple services running under it. SO basically i have 2 different containers i want to run with this 1 ECS cluster.

How do i do this? I have seen many examples but usually 1 service with 1 ECS cluster

So from this link i got a template https://www.terraform.io/docs/providers/aws/d/ecs_task_definition.html

# Simply specify the family to find the latest ACTIVE revision in that family.
data "aws_ecs_task_definition" "mongo" {
  task_definition = "${aws_ecs_task_definition.mongo.family}"
}

resource "aws_ecs_cluster" "foo" {
  name = "foo"
}

resource "aws_ecs_task_definition" "mongo" {
  family = "mongodb"

  container_definitions = <<DEFINITION
[
  {
    "cpu": 128,
    "environment": [{
      "name": "SECRET",
      "value": "KEY"
    }],
    "essential": true,
    "image": "mongo:latest",
    "memory": 128,
    "memoryReservation": 64,
    "name": "mongodb"
  }
]
DEFINITION
}

resource "aws_ecs_service" "mongo" {
  name          = "mongo"
  cluster       = "${aws_ecs_cluster.foo.id}"
  desired_count = 2

  # Track the latest ACTIVE revision
  task_definition = "${aws_ecs_task_definition.mongo.family}:${max("${aws_ecs_task_definition.mongo.revision}", "${data.aws_ecs_task_definition.mongo.revision}")}"
}

So lets say i want to add a node app service to that how will it look? And have them both run on the same cluster

Thanks!

已采纳解答

原帖链接 · Andrey · CC BY-SA 3.0

You simply create another aws_ecs_service and aws_ecs_task_definition resources.

If you want it running on the same cluster, just specify the same cluster ID in the cluster param.

So it would look something like this

# Simply specify the family to find the latest ACTIVE revision in that family.
data "aws_ecs_task_definition" "mongo" {
  task_definition = "${aws_ecs_task_definition.mongo.family}"
}

data "aws_ecs_task_definition" "nginx" {
  task_definition = "${aws_ecs_task_definition.nginx.family}"
}

resource "aws_ecs_cluster" "foo" {
  name = "foo"
}


# ======================  TASKS ===================
resource "aws_ecs_task_definition" "mongo" {
  family = "mongodb"

  container_definitions = <<DEFINITION
[
  {
    "cpu": 128,
    "environment": [{
      "name": "SECRET",
      "value": "KEY"
    }],
    "essential": true,
    "image": "mongo:latest",
    "memory": 128,
    "memoryReservation": 64,
    "name": "mongodb"
  }
]
DEFINITION
}

resource "aws_ecs_task_definition" "nginx" {
  family = "nginx"

  container_definitions = <<DEFINITION
[
  {
    "cpu": 128,
    "environment": [{
      "name": "SECRET",
      "value": "KEY"
    }],
    "essential": true,
    "image": "nginx:latest",
    "memory": 128,
    "memoryReservation": 64,
    "name": "nginx"
  }
]
DEFINITION
}

# ======================  SERVICES ===================

resource "aws_ecs_service" "mongo" {
  name          = "mongo"
  cluster       = "${aws_ecs_cluster.foo.id}"
  desired_count = 2

  # Track the latest ACTIVE revision
  task_definition = "${aws_ecs_task_definition.mongo.family}:${max("${aws_ecs_task_definition.mongo.revision}", "${data.aws_ecs_task_definition.mongo.revision}")}"
}

resource "aws_ecs_service" "nginx" {
  name          = "nginx"
  cluster       = "${aws_ecs_cluster.foo.id}"
  desired_count = 2

  # Track the latest ACTIVE revision
  task_definition = "${aws_ecs_task_definition.nginx.family}:${max("${aws_ecs_task_definition.nginx.revision}", "${data.aws_ecs_task_definition.nginx.revision}")}"
}

There is another way of running multiple containers on ECS.

You can place both Nginx and MongoDB on the same instance and under the same Task/Service.

It would look like this

# Simply specify the family to find the latest ACTIVE revision in that family.
data "aws_ecs_task_definition" "webapp" {
  task_definition = "${aws_ecs_task_definition.webapp.family}"
}

resource "aws_ecs_cluster" "foo" {
  name = "foo"
}


# ======================  TASKS ===================

resource "aws_ecs_task_definition" "webapp" {
  family = "webapp"

  container_definitions = <<DEFINITION
[
  {
    "cpu": 128,
    "environment": [{
      "name": "SECRET",
      "value": "KEY"
    }],
    "essential": true,
    "image": "nginx:latest",
    "memory": 128,
    "memoryReservation": 64,
    "name": "nginx"
  },
  {
    "cpu": 128,
    "environment": [{
      "name": "SECRET",
      "value": "KEY"
    }],
    "essential": true,
    "image": "mongo:latest",
    "memory": 128,
    "memoryReservation": 64,
    "name": "mongodb"
  }
]
DEFINITION
}

# ======================  SERVICES ===================

resource "aws_ecs_service" "webapp" {
  name          = "webapp"
  cluster       = "${aws_ecs_cluster.foo.id}"
  desired_count = 1

  # Track the latest ACTIVE revision
  task_definition = "${aws_ecs_task_definition.webapp.family}:${max("${aws_ecs_task_definition.webapp.revision}", "${data.aws_ecs_task_definition.webapp.revision}")}"
}

The advantage of the first method is that you can scale and manage each container independently.

The advantage of the second method is that the containers will be placed on the same EC2 instance and can be linked but can not be scaled independently. When you set desired count to N, you will get N number of "webapp" (so both Nginx and MongoDB instances). With the first method you can have N number of Nginx and X number of MongoDB.

Hope this helps.

NOTE : Terraform code has not been tested.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。