Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heredoc syntax for variable values

Tags:

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?

like image 989
trajan Avatar asked Apr 30 '19 03:04

trajan


1 Answers

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": "****"
    }
  }
}
like image 176
SomeGuyOnAComputer Avatar answered Nov 03 '22 01:11

SomeGuyOnAComputer