From c74d76f4ab0a66299b01f9c3e27bb8da942e1a38 Mon Sep 17 00:00:00 2001 From: Chris Wright Date: Wed, 24 Jul 2024 12:24:39 +0100 Subject: [PATCH] Allow environment variables in app container definition * This allows adding environment variables using the app.json.tpl container defintion template * We can then reuse this container definition for tasks that won't be using an S3 environment variable file (eg. passing in environment variables from Terraform) --- container-definitions/app.json.tpl | 3 +++ ecs-cluster-infrastructure-service-scheduled-task.tf | 1 + ecs-cluster-infrastructure-service.tf | 1 + 3 files changed, 5 insertions(+) diff --git a/container-definitions/app.json.tpl b/container-definitions/app.json.tpl index bbbdd56..2ef80ff 100644 --- a/container-definitions/app.json.tpl +++ b/container-definitions/app.json.tpl @@ -29,6 +29,9 @@ } ], %{ endif } + %{ if environment != "[]" } + "environment": "${environment}, + %{ endif } %{ if environment_file_s3 != "" } "environmentFiles": [ { diff --git a/ecs-cluster-infrastructure-service-scheduled-task.tf b/ecs-cluster-infrastructure-service-scheduled-task.tf index e9490ec..595dc77 100644 --- a/ecs-cluster-infrastructure-service-scheduled-task.tf +++ b/ecs-cluster-infrastructure-service-scheduled-task.tf @@ -21,6 +21,7 @@ resource "aws_ecs_task_definition" "infrastructure_ecs_cluster_service_scheduled image = aws_ecr_repository.infrastructure_ecs_cluster_service[each.value["container_name"]].repository_url entrypoint = each.value["entrypoint"] != null ? jsonencode(each.value["entrypoint"]) : "[]" environment_file_s3 = "${aws_s3_bucket.infrastructure_ecs_cluster_service_environment_files[0].arn}/${each.value["container_name"]}.env" + environment = jsonencode([]) container_port = 0 extra_hosts = each.value["extra_hosts"] != null ? jsonencode([ for extra_host in each.value["extra_hosts"] : { diff --git a/ecs-cluster-infrastructure-service.tf b/ecs-cluster-infrastructure-service.tf index de7ec4d..744e29e 100644 --- a/ecs-cluster-infrastructure-service.tf +++ b/ecs-cluster-infrastructure-service.tf @@ -223,6 +223,7 @@ resource "aws_ecs_task_definition" "infrastructure_ecs_cluster_service" { image = aws_ecr_repository.infrastructure_ecs_cluster_service[each.key].repository_url entrypoint = each.value["container_entrypoint"] != null ? jsonencode(each.value["container_entrypoint"]) : "[]" environment_file_s3 = "${aws_s3_bucket.infrastructure_ecs_cluster_service_environment_files[0].arn}/${each.key}.env" + environment = jsonencode([]) container_port = each.value["container_port"] != null ? each.value["container_port"] : 0 extra_hosts = each.value["container_extra_hosts"] != null ? jsonencode([ for extra_host in each.value["container_extra_hosts"] : {