Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am really new to terraform so please forgive anything I say incorrectly. I come from a normal programming background (C/C++/C#/python etc)

In order to avoid duplication, I want to use a module output value to set a variable value in variables.tf using string interpolation. When I do this and then do 'terraform validate' I get an Error: Variables not allowed.

String interpolation worked fine when I used the output value in a different .tf file to set a resource parameter.
resource "google_storage_bucket_iam_binding" "b-viewer" {
  bucket  = google_storage_bucket.b.name
  role    = "roles/storage.objectViewer"
  members = ["abc${module.my_module.project_number}xyz"]
}


How can I achieve this?

What I have tried:

My terraform structure is:

root/variables.tf
root/module/<module name=""> # this is the module source

The module is called "my_module" and has an output e.g.

output "project_number" {
  value = google_project.project.number
}


In root/variables.tf I have:

variable "my_variable" {
   type = string
   default = "abc${module.my_module.project_number}xyz"
{


but this does not work, it throws and error because I have used the ${module.....} in the default value setting.

I also tried creating a local in variables.tf
locals {
  my_project = module.my_module.project_number
}

and then tried using that in the variable:
default = "abc${local.my_project}xyz"

but that did not work either.

I have tried to find answers to this, of course, but have failed and may be completely missing a point, so I apologise in advance if I am being stupid :/
Posted
Updated 9-Jul-23 23:36pm
v4

1 solution

I fixed this by putting the values in the locals block. I guess it didn't work with variables as variables have global scope and locals have module scope. So you can only use a value returned by a module in that module or the root module which created it. If it had global scope then it might be accessed without the value being set.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900