Skip to content

Commit

Permalink
Merge pull request #3 from appzen-oss/multitaskdef
Browse files Browse the repository at this point in the history
Adding multi-taskdef support
  • Loading branch information
snemetz authored May 18, 2020
2 parents ee3e7e2 + f0df9da commit b38844f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
19 changes: 19 additions & 0 deletions files/container_definition_firelens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "${name}",
"image": "${image}",
"memory": ${memory},
"memoryReservation": ${memory_reservation},
"portMappings": ${port_mappings},
${command_override}
"environment": ${environment},
"mountPoints": ${mount_points},
"logConfiguration": {
"logDriver": "awsfirelens",
"options": {
"Name": "forward",
"Host": "${firelens_host}",
"Port": "${firelens_port}"
}
}
${additional_config}
}
26 changes: 26 additions & 0 deletions files/sidecar_container_definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "${name}",
"image": "${image}",
"memory": ${memory},
"memoryReservation": ${memory_reservation},
"portMappings": [],
"environment": ${environment},
"mountPoints": [],
"firelensConfiguration": {
"type": "fluentbit",
"options": {
"config-file-type": "file",
"enable-ecs-log-metadata": "true",
"config-file-value": "/fluent-bit/etc/json-annotate.conf"
}
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "${awslogs_group}",
"awslogs-region": "${awslogs_region}",
"awslogs-stream-prefix": "${awslogs_stream_prefix}"
}
}
${additional_config}
}
50 changes: 49 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,62 @@ data "template_file" "container_definition" {
}
}

# sidecar container_definition
data "template_file" "sidecar_container_definition" {
count = "${module.enabled.value}"
template = "${file("${path.module}/files/sidecar_container_definition.json")}"

vars {
name = "log_router"
image = "${var.docker_registry != "" ? "${var.docker_registry}/${var.sidecar_docker_image}" : var.sidecar_docker_image}"
memory = "${var.docker_memory}"
memory_reservation = "${var.sidecar_docker_memory_reservation}"
environment = "${jsonencode(var.sidecar_docker_environment)}"
awslogs_group = "${local.log_group_name}"
awslogs_region = "${var.region}"
awslogs_stream_prefix = "${module.label.environment}"
additional_config = "${var.sidecar_container_definition_additional == "" ? "" :
",${var.sidecar_container_definition_additional}"}"
}
}

# application_with_firelens_container_definition
data "template_file" "firelens_container_definition" {
count = "${module.enabled.value}"
template = "${file("${path.module}/files/container_definition_firelens.json")}"

# ADD: networkMode?, cpu
vars {
name = "${module.label.name}"
image = "${var.docker_registry != "" ? "${var.docker_registry}/${var.docker_image}" : var.docker_image}"
memory = "${var.docker_memory}"
memory_reservation = "${var.docker_memory_reservation}"

#app_port = "${var.app_port}"
port_mappings = "${replace(jsonencode(var.docker_port_mappings), "/\"([0-9]+)\"/", "$1")}"
command_override = "${length(var.docker_command) > 0 ? "\"command\": [\"${var.docker_command}\"]," : ""}"
environment = "${jsonencode(var.docker_environment)}"
mount_points = "${replace(jsonencode(var.docker_mount_points), "\"true\"", true)}"
firelens_host = "${var.firelens_host_url}"
firelens_port = "${var.firelens_port}"
additional_config = "${var.container_definition_additional == "" ? "" :
",${var.container_definition_additional}"}"
}
}

# FIX: resource cannot be found if it fails
# when passing in container_definition, if def bad, wrong format, invalid arg, etc.
# Look into support for sidecars, proxy, (AppMesh)

locals {
container_definitions = "${var.container_definition == "" && var.firelens_host_url == "" ? element(concat(data.template_file.container_definition.*.rendered, list("")), 0) : "[${data.template_file.firelens_container_definition.rendered},${data.template_file.sidecar_container_definition.rendered}]"}"
}

resource "aws_ecs_task_definition" "task" {
#count = "${module.enabled.value}"
count = "${module.enabled.value && var.task_definition_arn == "" ? 1 : 0}"
family = "${module.label.id}"
container_definitions = "${var.container_definition == "" ? element(concat(data.template_file.container_definition.*.rendered, list("")), 0) : var.container_definition}"
container_definitions = "${local.container_definitions}"
network_mode = "${var.network_mode}"
tags = "${module.label.tags}"
task_role_arn = "${var.task_role_arn == "" ? aws_iam_role.task.arn : var.task_role_arn}"
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ output "service_name" {

output "container_json" {
description = ""
value = "${element(concat(data.template_file.container_definition.*.rendered, list("")), 0)}"
value = "${local.container_definitions}"
}
32 changes: 32 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,35 @@ variable "container_definition_additional" {
description = "Additional parameters to add to container definition. This is a json substring"
default = ""
}

variable "firelens_port" {
description = "This is firelens port"
default = "24224"
}

variable "firelens_host_url" {
description = "This is firelens Host"
default = ""
}

variable "sidecar_container_definition_additional" {
description = "Additional parameters to add to container definition. This is a json substring"
default = ""
}

variable "sidecar_docker_image" {
description = "Docker image to use for task"
type = "string"
default = ""
}

variable "sidecar_docker_environment" {
description = "List of environment maps of format { \"name\" = \"var_name\", \"value\" = \"var_value\" }"
type = "list"
default = []
}

variable "sidecar_docker_memory_reservation" {
description = "Soft limit on memory use for task container (default 128)"
default = 128
}

0 comments on commit b38844f

Please sign in to comment.