How can I escape double quotes in a variable value?

Hello !

I’m trying to figure out how to escape double quotes in a variable of a terraform script.

Here is a more concrete example of what I’m trying to do that doesn’t work :

variable "authorized_ip_adress" {
  description = "IP addresses that will be allowed to use the API"
  default     = "\"10.0.0.1\", \"10.0.0.2\", \"10.0.0.3\""
}

and I need that when I call the variable, the value contains the double quotes.

I also tried with triple backslashes but it didn’t work either :

variable "authorized_ip_adress" {
  description = "IP addresses that will be allowed to use the API"
  default     = "\\\"10.0.0.1\\\", \\\"10.0.0.2\\\", \\\"10.0.0.3\\\""
}

Thank you in advance

I think it works as I’d expect:

fbte61@SEA37LT16653:/tmp/terraform$ cat main.tf
locals {
  authorized_ip_adress = "\"10.0.0.1\", \"10.0.0.2\", \"10.0.0.3\""
}

output "authorized_ip_adress" {
  value = local.authorized_ip_adress
}
fbte61@SEA37LT16653:/tmp/terraform$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

authorized_ip_adress = "10.0.0.1", "10.0.0.2", "10.0.0.3"
fbte61@SEA37LT16653:/tmp/terraform$

What is the output you want to have? Let’s work backwards from there :slight_smile:

Hello bentterp !

It works perfectly, that’s exactly what I wanted to do.

Thank you very much and have a nice day ! :grinning:

1 Like