title: How to concatenate two template files into one file and pass onto ECS container task definition in terraform
body: <p>I have two template files. I want to merge these template files into one and pass them onto the ECS attribute <strong>container_definitions</strong> in the <strong>aws_ecs_task_definition</strong> resource.</p>
  <p>Terraform Version =&gt; v0.14.9</p>
  <p><strong>nginx.tpl.json:</strong></p>
  <pre><code> [
      {
          &quot;name&quot;: &quot;nginx&quot;,
          &quot;image&quot;: &quot;public.ecr.aws/nginx/nginx:latest&quot;,
          &quot;portMappings&quot;: [
              {
                  &quot;containerPort&quot;: 80,
                  &quot;hostPort&quot;: 80,
                  &quot;protocol&quot;: &quot;tcp&quot;
              }
          ]
      }
   ]
  </code></pre>
  <p><strong>redis.json.tpl:</strong></p>
  <pre><code> [
      {
          &quot;name&quot;: &quot;redis&quot;,
          &quot;image&quot;: &quot;public.ecr.aws/peakon/redis:6.0.5&quot;,
          &quot;portMappings&quot;: [
              {
                  &quot;containerPort&quot;: 6379,
                  &quot;hostPort&quot;: 6379,
                  &quot;protocol&quot;: &quot;tcp&quot;
              }
          ]
      }
   ]
  </code></pre>
  <p>When combined two template files manually like below it is working. But with Terraform <a href="https://www.terraform.io/docs/language/functions/concat.html" rel="nofollow noreferrer">concat</a> or <a href="https://www.terraform.io/docs/language/functions/format.html" rel="nofollow noreferrer">format</a> getting errors.</p>
  <pre><code> [
      {
          &quot;name&quot;: &quot;nginx&quot;,
          &quot;image&quot;: &quot;public.ecr.aws/nginx/nginx:latest&quot;,
          &quot;portMappings&quot;: [
              {
                  &quot;containerPort&quot;: 80,
                  &quot;hostPort&quot;: 80,
                  &quot;protocol&quot;: &quot;tcp&quot;
              }
          ]
      },
      {
          &quot;name&quot;: &quot;redis&quot;,
          &quot;image&quot;: &quot;public.ecr.aws/peakon/redis:6.0.5&quot;,
          &quot;portMappings&quot;: [
              {
                  &quot;containerPort&quot;: 6379,
                  &quot;hostPort&quot;: 6379,
                  &quot;protocol&quot;: &quot;tcp&quot;
              }
          ]
      }
   ]
  
  </code></pre>
  <pre><code>data &quot;template_file&quot; &quot;ecs_task&quot; {
    template = format(&quot;%s%s&quot;,file(&quot;./ecs/templates/nginx.json.tpl&quot;),
                              file(&quot;./ecs/templates/redis.json. tpl&quot;)
                )
  } =&gt; Here I need to combine the two template files and then pass them onto the container_definitions to the below resource.
  </code></pre>
  <pre><code>resource &quot;aws_ecs_task_definition&quot; &quot;testapp&quot; {
    family                = &quot;testapp&quot;
    network_mode          = &quot;awsvpc&quot;
    cpu                   = 256
    memory                = 512
    container_definitions = data.template_file.ecs_task.rendered # I'm getting the following error.
  }
  </code></pre>
  <p><strong>Error:</strong><code>invalid character '}' looking for the beginning of object key string</code></p>
  <p>Can someone help me with this, please?</p>