Terraform: List variable inside of HEREDOC

Created on 19 Nov 2017  ยท  3Comments  ยท  Source: hashicorp/terraform

Trying to use a list variable inside of HEREDOC, but getting:

```

  • module.elasticsearch.aws_elasticsearch_domain.elasticsearch: At column 1, line 1: output of an HIL expression must be a string, or a single list (argument 2 is TypeList) in:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "elasticsearch:*",
            "Principal": "*",
            "Effect": "Allow",
            "Condition": {
                "IpAddress": {"aws:SourceIp": "${var.allowed_ips}"}
            }
        }
    ]
}

````

Here is the config:

````tf
variable "allowed_ips" {
type = "list"
description = "A list of allowed IPs that can connect to the Elasticsearch cluster"
}

access_policies = < {
"Version": "2012-10-17",
"Statement": [
{
"Action": "elasticsearch:",
"Principal": "
",
"Effect": "Allow",
"Condition": {
"IpAddress": {"aws:SourceIp": "${var.allowed_ips}"}
}
}
]
}
CONFIG
````

config question

Most helpful comment

@apparentlymart @nodesocket

I guess jsonencode takes only one parameter and the above is a typo.
ref: https://www.terraform.io/docs/configuration/interpolation.html#jsonencode-value-

Removing the "," worked for me.

_just a note, in case someone else has reached here after googling_

All 3 comments

Hi @nodesocket,

This error is returned because Terraform can't know automatically what is an appropriate way to serialize the given list as a string in order to include it. However, you can use an interpolation function to convert the list to a string in a suitable serialization. I'm not familiar with exactly what format is expected here, but for example you could either use a comma-separated string, like this:

access_policies = <<CONFIG
  ...
  "IpAddress": {"aws:SourceIp": "${join(",", var.allowed_ips)}"}
  ...
CONFIG

...or a JSON array, like this:

access_policies = <<CONFIG
  ...
  "IpAddress": {"aws:SourceIp": ${jsonencode(",", var.allowed_ips)}}
  ...
CONFIG

Note that in the latter case there are no enclosing quotes, because the result is itself a valid JSON expression.

@apparentlymart @nodesocket

I guess jsonencode takes only one parameter and the above is a typo.
ref: https://www.terraform.io/docs/configuration/interpolation.html#jsonencode-value-

Removing the "," worked for me.

_just a note, in case someone else has reached here after googling_

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings