Trying to use a list variable inside of HEREDOC, but getting:
```
{
"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
````
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.
Most helpful comment
@apparentlymart @nodesocket
I guess
jsonencodetakes 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_