Jwary
Jwary

Reputation: 167

How to concatenate two template files into one file and pass onto ECS container task definition in terraform

I have two template files. I want to merge these template files into one and pass them onto the ECS attribute container_definitions in the aws_ecs_task_definition resource.

Terraform Version => v0.14.9

nginx.tpl.json:

 [
    {
        "name": "nginx",
        "image": "public.ecr.aws/nginx/nginx:latest",
        "portMappings": [
            {
                "containerPort": 80,
                "hostPort": 80,
                "protocol": "tcp"
            }
        ]
    }
 ]

redis.json.tpl:

 [
    {
        "name": "redis",
        "image": "public.ecr.aws/peakon/redis:6.0.5",
        "portMappings": [
            {
                "containerPort": 6379,
                "hostPort": 6379,
                "protocol": "tcp"
            }
        ]
    }
 ]

When combined two template files manually like below it is working. But with Terraform concat or format getting errors.

 [
    {
        "name": "nginx",
        "image": "public.ecr.aws/nginx/nginx:latest",
        "portMappings": [
            {
                "containerPort": 80,
                "hostPort": 80,
                "protocol": "tcp"
            }
        ]
    },
    {
        "name": "redis",
        "image": "public.ecr.aws/peakon/redis:6.0.5",
        "portMappings": [
            {
                "containerPort": 6379,
                "hostPort": 6379,
                "protocol": "tcp"
            }
        ]
    }
 ]

data "template_file" "ecs_task" {
  template = format("%s%s",file("./ecs/templates/nginx.json.tpl"),
                            file("./ecs/templates/redis.json. tpl")
              )
} => Here I need to combine the two template files and then pass them onto the container_definitions to the below resource.
resource "aws_ecs_task_definition" "testapp" {
  family                = "testapp"
  network_mode          = "awsvpc"
  cpu                   = 256
  memory                = 512
  container_definitions = data.template_file.ecs_task.rendered # I'm getting the following error.
}

Error:invalid character '}' looking for the beginning of object key string

Can someone help me with this, please?

Upvotes: 2

Views: 4602

Answers (1)

Marcin
Marcin

Reputation: 238199

Update

Remove brackets from your files

    {
        "name": "nginx",
        "image": "public.ecr.aws/nginx/nginx:latest",
        "portMappings": [
            {
                "containerPort": 80,
                "hostPort": 80,
                "protocol": "tcp"
            }
        ]
    }

and


    {
        "name": "redis",
        "image": "public.ecr.aws/peakon/redis:6.0.5",
        "portMappings": [
            {
                "containerPort": 6379,
                "hostPort": 6379,
                "protocol": "tcp"
            }
        ]
    }

Then instead of "%s%s". Seems you are missing comma: "[%s,%s]".

Upvotes: 1

Related Questions