Skip to content

Commit 4de3187

Browse files
authored
Add support for multiple containers (#24)
* Add ability to pass multiple container definitions * Make container_name and container_image optional
1 parent 1808bd2 commit 4de3187

File tree

7 files changed

+99
-4
lines changed

7 files changed

+99
-4
lines changed

.terraform.lock.hcl

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ In order to run all checks at any point run the following command:
6767
| <a name="input_container_cpu"></a> [container\_cpu](#input\_container\_cpu) | (Optional) The number of cpu units to reserve for the container. This is optional for tasks using Fargate launch type and the total amount of container\_cpu of all containers in a task will need to be lower than the task-level cpu value | `number` | `1024` | no |
6868
| <a name="input_container_definition"></a> [container\_definition](#input\_container\_definition) | Container definition overrides which allows for extra keys or overriding existing keys. | `map(any)` | `{}` | no |
6969
| <a name="input_container_depends_on"></a> [container\_depends\_on](#input\_container\_depends\_on) | The dependencies defined for container startup and shutdown. A container can contain multiple dependencies. When a dependency is defined for container startup, for container shutdown it is reversed. The condition can be one of START, COMPLETE, SUCCESS or HEALTHY | <pre>list(object({<br> containerName = string<br> condition = string<br> }))</pre> | `[]` | no |
70-
| <a name="input_container_image"></a> [container\_image](#input\_container\_image) | The image used to start the container. Images in the Docker Hub registry available by default | `string` | n/a | yes |
70+
| <a name="input_container_image"></a> [container\_image](#input\_container\_image) | The image used to start the container. Images in the Docker Hub registry available by default | `string` | `null` | no |
7171
| <a name="input_container_memory"></a> [container\_memory](#input\_container\_memory) | (Optional) The amount of memory (in MiB) to allow the container to use. This is a hard limit, if the container attempts to exceed the container\_memory, the container is killed. This field is optional for Fargate launch type and the total amount of container\_memory of all containers in a task will need to be lower than the task memory value | `number` | `4096` | no |
7272
| <a name="input_container_memory_reservation"></a> [container\_memory\_reservation](#input\_container\_memory\_reservation) | (Optional) The amount of memory (in MiB) to reserve for the container. If container needs to exceed this threshold, it can do so up to the set container\_memory hard limit | `number` | `2048` | no |
73-
| <a name="input_container_name"></a> [container\_name](#input\_container\_name) | The name of the container. Up to 255 characters ([a-z], [A-Z], [0-9], -, \_ allowed) | `string` | n/a | yes |
73+
| <a name="input_container_name"></a> [container\_name](#input\_container\_name) | The name of the container. Up to 255 characters ([a-z], [A-Z], [0-9], -, \_ allowed) | `string` | `null` | no |
74+
| <a name="input_containers"></a> [containers](#input\_containers) | Container definitions to use for the task. If this is used, all other container options will be ignored. | `list(any)` | `[]` | no |
7475
| <a name="input_disable_networking"></a> [disable\_networking](#input\_disable\_networking) | When this parameter is true, networking is disabled within the container. | `bool` | `null` | no |
7576
| <a name="input_dns_search_domains"></a> [dns\_search\_domains](#input\_dns\_search\_domains) | Container DNS search domains. A list of DNS search domains that are presented to the container | `list(string)` | `[]` | no |
7677
| <a name="input_dns_servers"></a> [dns\_servers](#input\_dns\_servers) | Container DNS servers. This is a list of strings specifying the IP addresses of the DNS servers | `list(string)` | `[]` | no |

examples/multiple-containers/.terraform.lock.hcl

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module "container-definition-1" {
2+
source = "cloudposse/ecs-container-definition/aws"
3+
version = "0.58.1"
4+
container_name = "container-1"
5+
container_image = "ubuntu-1"
6+
}
7+
8+
module "container-definition-2" {
9+
source = "cloudposse/ecs-container-definition/aws"
10+
version = "0.58.1"
11+
container_name = "container-2"
12+
container_image = "ubuntu-2"
13+
}
14+
15+
module "td" {
16+
source = "../../"
17+
name_prefix = "multiple-containers"
18+
19+
containers = [
20+
module.container-definition-1.json_map_object,
21+
module.container-definition-2.json_map_object,
22+
]
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
terraform {
2+
required_version = ">= 0.13"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = ">= 4"
7+
}
8+
}
9+
}
10+
11+
provider "aws" {
12+
region = "us-east-1"
13+
skip_credentials_validation = true
14+
skip_requesting_account_id = true
15+
skip_metadata_api_check = true
16+
s3_use_path_style = true
17+
access_key = "mock_access_key"
18+
secret_key = "mock_secret_key"
19+
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module "container_definition" {
8080
# Task Definition
8181
resource "aws_ecs_task_definition" "td" {
8282
family = "${var.name_prefix}-td"
83-
container_definitions = "[${module.container_definition.json_map_encoded}]"
83+
container_definitions = length(var.containers) == 0 ? "[${module.container_definition.json_map_encoded}]" : jsonencode(var.containers)
8484
task_role_arn = var.task_role_arn == null ? aws_iam_role.ecs_task_execution_role.arn : var.task_role_arn
8585
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
8686
network_mode = "awsvpc"

variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ variable "tags" {
1616
#------------------------------------------------------------------------------
1717
variable "container_name" {
1818
type = string
19+
default = null
1920
description = "The name of the container. Up to 255 characters ([a-z], [A-Z], [0-9], -, _ allowed)"
2021
}
2122

2223
variable "container_image" {
2324
type = string
25+
default = null
2426
description = "The image used to start the container. Images in the Docker Hub registry available by default"
2527
}
2628

@@ -42,6 +44,12 @@ variable "container_definition" {
4244
default = {}
4345
}
4446

47+
variable "containers" {
48+
type = list(any)
49+
description = "Container definitions to use for the task. If this is used, all other container options will be ignored."
50+
default = []
51+
}
52+
4553
variable "port_mappings" {
4654
description = "The port mappings to configure for the container. This is a list of maps. Each map should contain \"containerPort\", \"hostPort\", and \"protocol\", where \"protocol\" is one of \"tcp\" or \"udp\". If using containers in a task with the awsvpc or host network mode, the hostPort can either be left blank or set to the same value as the containerPort"
4755
type = list(object({

0 commit comments

Comments
 (0)