Skip to content

Commit f5912b6

Browse files
author
Eugene Dementiev
committed
Allow additional linked containers
1 parent 4891c5e commit f5912b6

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

main.tf

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
resource "aws_ecs_task_definition" "task" {
22
family = "${var.cluster_name}-${var.service_name}"
3-
container_definitions = jsonencode([
3+
container_definitions = jsonencode([for s in concat([
44
{
55
name = var.service_name
66
image = var.image
77
cpu = var.cpu
88
essential = var.essential
99
memory = var.memory
1010
memoryReservation = var.memory_reservation
11-
portMappings = local.balanced ? coalescelist(var.port_mappings, local.defaultPortMappings) : []
1211
mountPoints = []
13-
logConfiguration = local.log_configuration
14-
environment = [for k in sort(keys(var.environment)) : { "name" : k, "value" : var.environment[k] }]
1512
volumesFrom = []
1613
}
17-
])
14+
], var.additional_container_definitions) : merge(s, {
15+
environment = [for k in sort(keys(var.environment)) : { "name" : k, "value" : var.environment[k] }]
16+
# leverage the new terraform syntax and override the awslogs-stream-prefix
17+
logConfiguration = merge(local.log_configuration,
18+
local.log_configuration["logDriver"] == "awslogs" && local.log_configuration["options"] != null ? {
19+
options = merge(local.log_configuration["options"], { "awslogs-stream-prefix" = s.name })
20+
} : {}
21+
)
22+
# load balancer the local.load_balancer_container_name
23+
portMappings = local.balanced && local.load_balancer_container_name == s.name ? coalescelist(var.port_mappings, local.defaultPortMappings) : []
24+
})])
1825

1926
task_role_arn = var.task_role_arn
2027

@@ -44,7 +51,7 @@ resource "aws_ecs_service" "service" {
4451

4552
content {
4653
target_group_arn = load_balancer.value
47-
container_name = var.service_name
54+
container_name = local.load_balancer_container_name
4855
container_port = var.container_port
4956
}
5057
}

variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ variable "container_port" {
5252
default = 0
5353
}
5454

55+
variable "load_balancer_container_name" {
56+
description = "Name of the container that will used by the load balancer. Can be used to put nginx proxy in front of the app. Defaults to the service name"
57+
type = string
58+
default = ""
59+
}
60+
61+
variable "additional_container_definitions" {
62+
type = list(any)
63+
default = []
64+
}
65+
5566
variable "task_role_arn" {
5667
type = string
5768
default = ""
@@ -85,7 +96,8 @@ variable "desired_count" {
8596
}
8697

8798
locals {
88-
balanced = var.container_port > 0
99+
balanced = var.container_port > 0
100+
load_balancer_container_name = coalesce(var.load_balancer_container_name, var.service_name)
89101

90102
target_group_names = coalescelist(distinct(compact(concat(var.target_group_names, list(var.target_group_name)))), list(var.cluster_name))
91103

0 commit comments

Comments
 (0)