3
votes

I am trying to pass a map variable into a template_file, and am being thrown this error:

vars (varsname): '' expected type 'string', got unconvertible type 'map[string]interface {}'

    data "template_file" "app" {

        template = "${file("./app_template.tpl")}"

        vars {
                container = "${var.container-configuration}"
        }
    }

variables.tf

    variable "container-configuration" {
        description = "Configuration for container"
        type        = "map"
        default     = {
                    image          = "blahblah.dkr.ecr.us-east-2.amazonaws.com/connect"
                    container-port = "3000"
                    host-port      = "3000"
                    cpu            = "1024"
                    memory         = "2048"
                    log-group      = "test"
                    log-region     = "us-east-2a"
                  }
    }

Is there a way to pass the map into the template file for interpolation? I haven't found anything clear in the documentation.

2

2 Answers

4
votes

Terraform v0.12 introduced the templatefile function, which absorbs the main use-cases of the template_file data source, and accepts values of any type:

templatefile("${path.module}/app_template.tpl", {
  container = var.container-configuration
})

Terraform v0.11 and earlier do not have any means to render a template with non-string values. The limitation exists due to the nature of the protocol used to represent map values in the configuration: it is only capable of representing maps of string until the new protocol that was introduced in Terraform v0.12.

0
votes

Passing maps is not yet supported, see template_file documentation.

From that article:

Variables must all be primitives. Direct references to lists or maps will cause a validation error.

It means you need to pass variables one by one individually.