Search code examples
terraformamazon-ecs

How can I specify environment variable on ecs task definition via terraform?


I have below configuration in terraform to deploy ecs task definition. The task can be created and all configuration is good except environment variable. I have specified environment inside container_definitions but the task still doesn't have any environment variables under definition.

How can I specify environment variables in this script?

resource "aws_ecs_task_definition" "data-check" {
  family = "${var.stage}-data-check"

  execution_role_arn = aws_iam_role.ecs_data_check_role.arn

  container_definitions    = <<TASK_DEFINITION
  [
    {
      "name"      : "data-check-task",
      "image"     : "${local.image_name}",
      "essential" : true,
      "environment" : [{
        "name" : "this is name"
      }],
      "logConfiguration" : {
        "logDriver" : "awslogs",
        "options" : {
          "awslogs-region" : "${var.aws_region}",
          "awslogs-group" : "/ecs/${local.ecs_name}",
          "awslogs-stream-prefix" : "ecs"
        }
      }
    }
  ]
TASK_DEFINITION

  cpu                      = 1024
  memory                   = 2048
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
}

Solution

  • You missed something, the right way to declare env vars is:

        "environment": [
            {"name": "VARNAME", "value": "VARVAL"}
        ],