I'm attempting to use Heredoc syntax as a value for a string variable as per the following
variable "docker_config" {
type = "string"
default = <<EOF
{ "auths": { "https://index.docker.io/v1/": { "auth": "****" } } }
EOF
}
This doesn't result in Terraform producing an error, however when the variable is later used in a remote-exec command "echo ${var.docker_config} > /home/ubuntu/.docker/config.json", the value is empty.
Is this the correct way to use Heredoc syntax in a variable?
You can do a heredoc in a variable, a heredoc in a local variable, or you can construct a map and use jsonencode
to turn it into a string. You can use any of these later as well.
✗ cat main.tf
variable "test" {
description = "Testing heredoc"
default = <<EOF
"max-size": "8m",
"min-size": "1m",
"count": "8",
"type": "string",
EOF
}
locals {
docker_config = <<EOF
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "****"
}
}
}
EOF
even_better = {
auths = {
"https://index.docker.io/v1/" = {
auth = "****"
}
}
}
}
output "test_var" {
value = var.test
}
output "test_local" {
value = local.docker_config
}
output "even_better" {
value = jsonencode(local.even_better)
}
$ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
even_better = {"auths":{"https://index.docker.io/v1/":{"auth":"****"}}}
test_local = {
"auths": {
"https://index.docker.io/v1/": {
"auth": "****"
}
}
}
test_var = {
"auths": {
"https://index.docker.io/v1/": {
"auth": "****"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With