Moving containerDefinition command to locals

Hello, I am working with Terraform to deploy an ECS Service with some containers. Code is so long so I was trying to move all logic to locals.tf.

Right now, my code at container definition is like this:

module "prometheus" {
  source = "../aws-modules/ecs-service"

  [... more code ...]

  volumes               = local.prometheus_volumes
  cpu                   = var.prometheus_cpu
  memory                = var.prometheus_memory
  container_definitions = <<DEFINITION
[
  {
    "image": "${var.prometheus_container_image}",
    "name": "${lower(local.prometheus_name)}",
    "networkMode": "${var.prometheus_network_mode}",
    "portMappings": [
      {
        "containerPort": ${var.prometheus_container_port},
        "hostPort": ${var.prometheus_container_port},
        "protocol": "tcp"
      }
    ],
    "command": [
      "/bin/prometheus",
      "--config.file=/etc/prometheus/prometheus.yml",
      "--web.enable-lifecycle",
      "--storage.tsdb.path=/prom_data"
    ],
    [... more code ...]
  }
]
DEFINITION

This is working as expected, but I am trying to leave it like this:

module "prometheus" {
  source = "../aws-modules/ecs-service"

  [... more code ...]

  volumes               = local.prometheus_volumes
  cpu                   = var.prometheus_cpu
  memory                = var.prometheus_memory
  container_definitions = <<DEFINITION
[
  {
    "image": "${var.prometheus_container_image}",
    "name": "${lower(local.prometheus_name)}",
    "networkMode": "${var.prometheus_network_mode}",
    "portMappings": [
      {
        "containerPort": ${var.prometheus_container_port},
        "hostPort": ${var.prometheus_container_port},
        "protocol": "tcp"
      }
    ],
    "command": "${local.prometheus_container_command}",
     [... more code ...]
  }
]
DEFINITION

And declaring this on locals.tf:

prometheus_container_command = <<DEFINITION
    [
          "/bin/prometheus",
          "--config.file=/etc/prometheus/prometheus.yml",
          "--web.enable-lifecycle",
          "--storage.tsdb.path=/prom_data"
     ]
DEFINITION

I have done similar things on containerDefinitions, but never with an array, so I am facing this error (if I remove the newline, then the errors point to “/” from “/bin/prometheus”)

Summary
Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character '\n' in string literal

  on ../aws-modules/ecs-service/main.tf line 147, in resource "aws_ecs_task_definition" "ecs_task_definition":
 147:   container_definitions    = var.container_definitions

if I delete the “[”:

Summary
Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: invalid character 't' after array element

  on ../aws-modules/ecs-service/main.tf line 147, in resource "aws_ecs_task_definition" "ecs_task_definition":
 147:   container_definitions    = var.container_definitions

and I don’t know what more I can try (I have tried deleting “[”, without “DEFINITION”, etc…)

Some of you have tried to declare an array and use it on ECS containerDefinition?
Thanks a lot.

Hello ! I got answer on other site, so I am going to post it here (if I don’t breach any rule) in case someone also needs this.

Thanks!