1

let say I have this :-

variable "url" {
  value = "https://sqs.eu-central-1.amazonaws.com/11111111/my_queue.fifo"
}
provisioner "local-exec" {
command = <<-EOT
  sed -i "s/URL.*:.*/URL: $url/g" "./test.yaml"
EOT
}

while the ./test.yaml file would be:

kind: ConfigMap
apiVersion: v1
metadata:
  name: test
  namespace: default
data:
  URL: ""

I wonder how to get my URL: "" element replaced with an url variable from terraform ? I've tried many times but it returned me regex error. the escaping hell :cry

Bilal Bayasut
  • 121
  • 3
  • 9
  • I think all you need is `sed -i "s,URL.*:.*,URL: $url,g" "./test.yaml"` (assuming `,` is never present in the `$url`). Or, escape it to be used in `sed` replacement pattern. See [Escape a string for a sed replace pattern](https://stackoverflow.com/questions/407523/escape-a-string-for-a-sed-replace-pattern) – Wiktor Stribiżew Feb 11 '21 at 09:52

1 Answers1

1

The easiest way to set the urlin your test.yaml would be to use templatefile. In this case, your test.yaml would be:

kind: ConfigMap
apiVersion: v1
metadata:
  name: test
  namespace: default
data:
  URL: "${url}"

and the terraform code:

variable "url" {
  default = "https://sqs.eu-central-1.amazonaws.com/11111111/my_queue.fifo"
}

output "test" {  
  value = templatefile("./test.yaml", {url = var.url})
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • does this works on .yaml file instead of tf files only? , I've tried doing this but it gave me error ``` A reference to a resource type must be followed by at least one attribute access, specifying the resource name. ``` – Bilal Bayasut Feb 11 '21 at 19:30
  • @BilalBayasut Yes, it works with `yaml`. The example given in the answer is working one. – Marcin Feb 11 '21 at 21:39
  • @BilalBayasut No problem. Glad it worked out:-) – Marcin Feb 12 '21 at 09:43