Skip to content

Commit a7377e2

Browse files
jasonwalshjasonwalsh
authored andcommitted
feat: Add support for multiple containers
Closes #9
1 parent 03eefb7 commit a7377e2

File tree

12 files changed

+467
-101
lines changed

12 files changed

+467
-101
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module "mongodb" {
2+
source = "../.."
3+
4+
family = "mongodb"
5+
image = "mongo:3.6"
6+
memory = 512
7+
name = "mongodb"
8+
9+
portMappings = [
10+
{
11+
containerPort = 27017
12+
protocol = "TCP"
13+
},
14+
]
15+
16+
register_task_definition = false
17+
}
18+
19+
module "redis" {
20+
source = "../.."
21+
22+
family = "redis"
23+
image = "redis:alpine"
24+
memory = 512
25+
name = "redis"
26+
27+
portMappings = [
28+
{
29+
containerPort = 6379
30+
protocol = "TCP"
31+
},
32+
]
33+
34+
register_task_definition = false
35+
}
36+
37+
module "merged" {
38+
source = "../../modules/merge"
39+
40+
container_definitions = [
41+
"${module.mongodb.container_definitions}",
42+
"${module.redis.container_definitions}",
43+
]
44+
}
45+
46+
resource "aws_ecs_task_definition" "ecs_task_definition" {
47+
container_definitions = "${module.merged.container_definitions}"
48+
family = "app"
49+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "container_definitions" {
2+
value = "${module.merged.container_definitions}"
3+
}

main.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,17 @@ locals {
118118
digit = "/\"(-[[:digit:]]|[[:digit:]]+)\"/"
119119
}
120120

121-
container_definitions = "${replace(data.template_file.container_definitions.rendered, "/\"(null)\"/", "$1")}"
121+
container_definition = "${
122+
var.register_task_definition ?
123+
format("[%s]", data.template_file.container_definition.rendered) :
124+
format("%s", data.template_file.container_definition.rendered)
125+
}"
126+
127+
container_definitions = "${replace(local.container_definition, "/\"(null)\"/", "$1")}"
122128
}
123129

124-
data "template_file" "container_definitions" {
125-
template = "${file("${path.module}/templates/container-definitions.json.tmpl")}"
130+
data "template_file" "container_definition" {
131+
template = "${file("${path.module}/templates/container-definition.json.tpl")}"
126132

127133
vars = {
128134
command = "${local.command == "[]" ? "null" : local.command}"

modules/merge/README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
## Contents
2+
3+
- [Purpose](#purpose)
4+
- [Usage](#usage)
5+
- [Inputs](#inputs)
6+
- [Outputs](#outputs)
7+
8+
## Purpose
9+
10+
AWS ECS task definitions allow for multiple containers to be defined. For example, the following task definition contains two container definitions:
11+
12+
```json
13+
{
14+
"containerDefinitions": [
15+
{
16+
"name": "wordpress",
17+
"links": [
18+
"mysql"
19+
],
20+
"image": "wordpress",
21+
"essential": true,
22+
"portMappings": [
23+
{
24+
"containerPort": 80,
25+
"hostPort": 80
26+
}
27+
],
28+
"memory": 500,
29+
"cpu": 10
30+
},
31+
{
32+
"environment": [
33+
{
34+
"name": "MYSQL_ROOT_PASSWORD",
35+
"value": "password"
36+
}
37+
],
38+
"name": "mysql",
39+
"image": "mysql",
40+
"cpu": 10,
41+
"memory": 500,
42+
"essential": true
43+
}
44+
],
45+
"family": "hello_world"
46+
}
47+
```
48+
49+
Due to some known limitations with the [HashiCorp Configuration Language](https://github.com/hashicorp/hcl) (HCL), the `merge` module allows for combining multiple container definitions. To see an example of the `merge` module in use, see the [usage](#usage) section.
50+
51+
## Usage
52+
53+
The task definition defined in the purpose section can be created using a combination of the [`terraform-aws-ecs-task-definition`](https://github.com/mongodb/terraform-aws-ecs-task-definition) module and the `merge` module like so:
54+
55+
```hcl
56+
module "wordpress" {
57+
source = "mongodb/ecs-task-definition/aws"
58+
59+
name = "wordpress"
60+
61+
links = [
62+
"mysql",
63+
]
64+
65+
image = "wordpress"
66+
essential = true
67+
68+
portMappings = [
69+
{
70+
containerPort = 80
71+
hostPort = 80
72+
},
73+
]
74+
75+
memory = 500
76+
cpu = 10
77+
78+
register_task_definition = false
79+
}
80+
81+
module "mysql" {
82+
source = "mongodb/ecs-task-definition/aws"
83+
84+
environment = [
85+
{
86+
name = "MYSQL_ROOT_PASSWORD"
87+
value = "password"
88+
},
89+
]
90+
91+
name = "mysql"
92+
image = "mysql"
93+
cpu = 10
94+
memory = 500
95+
essential = true
96+
97+
register_task_definition = false
98+
}
99+
100+
module "merged" {
101+
source = "mongodb/ecs-task-definition/aws//modules/merge"
102+
103+
container_definitions = [
104+
"${module.wordpress.container_definitions}",
105+
"${module.mysql.container_definitions}",
106+
]
107+
}
108+
109+
resource "aws_ecs_task_definition" "hello_world" {
110+
container_definitions = "${module.merged.container_definitions}"
111+
family = "hello_world"
112+
}
113+
```
114+
115+
**Note:** The `register_task_definition` flag for both task definitions is required; otherwise a task definition containing a single container definition is registered created for both the `wordpress` and `mysql` services.
116+
117+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
118+
## Inputs
119+
120+
| Name | Description | Type | Default | Required |
121+
|------|-------------|:----:|:-----:|:-----:|
122+
| container\_definitions | A list of container definitions in JSON format that describe the different containers that make up your task | list | `[]` | no |
123+
124+
## Outputs
125+
126+
| Name | Description |
127+
|------|-------------|
128+
| container\_definitions | A list of container definitions in JSON format that describe the different containers that make up your task |
129+
130+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/merge/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "container_definitions" {
2+
description = "A list of container definitions in JSON format that describe the different containers that make up your task"
3+
value = "${format("[%s]", join(",", var.container_definitions))}"
4+
}

modules/merge/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "container_definitions" {
2+
default = []
3+
description = "A list of container definitions in JSON format that describe the different containers that make up your task"
4+
type = "list"
5+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"command": ${command},
3+
"cpu": ${cpu},
4+
"disableNetworking": ${disableNetworking},
5+
"dnsSearchDomains": ${dnsSearchDomains},
6+
"dnsServers": ${dnsServers},
7+
"dockerLabels": ${dockerLabels},
8+
"dockerSecurityOptions": ${dockerSecurityOptions},
9+
"entryPoint": ${entryPoint},
10+
"environment": ${environment},
11+
"essential": ${essential},
12+
"extraHosts": ${extraHosts},
13+
"healthCheck": ${healthCheck},
14+
"hostname": "${hostname}",
15+
"image": "${image}",
16+
"interactive": ${interactive},
17+
"links": ${links},
18+
"linuxParameters": ${linuxParameters},
19+
"logConfiguration": ${logConfiguration},
20+
"memory": ${memory},
21+
"memoryReservation": ${memoryReservation},
22+
"mountPoints": ${mountPoints},
23+
"name": "${name}",
24+
"portMappings": ${portMappings},
25+
"privileged": ${privileged},
26+
"pseudoTerminal": ${pseudoTerminal},
27+
"readonlyRootFilesystem": ${readonlyRootFilesystem},
28+
"repositoryCredentials": ${repositoryCredentials},
29+
"resourceRequirements": ${resourceRequirements},
30+
"secrets": ${secrets},
31+
"systemControls": ${systemControls},
32+
"ulimits": ${ulimits},
33+
"user": "${user}",
34+
"volumesFrom": ${volumesFrom},
35+
"workingDirectory": "${workingDirectory}"
36+
}

templates/container-definitions.json.tmpl

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/ecs_task_definition_test.go

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)