Assign a name of a variable based on another variable

I am looking into an existing pipeline using Gitlab and Terraform.
Basically what it comes down to -
I have a variable passed from the gitlab-ci.yml ( called TF_VAR_x - this is actually generated from a Gitlab CI_COMMIT_TAG variable ) . Is it possible to somehow assign the value of this to a variable name in tfvars? eg if I have something like this in terraform.tfvars -

is it possible to have y be the value of TF_VAR_x somehow?

myname = {
y = “alice”

Instead of trying to dynamically edit/alter the values being passed into your module within a .tfvars file you can pass input variable values into Terraform in multiple ways simultaneously.

In this instance, you can configure an input variable as follows:

variable "x" {
  type = string

Terraform actually looks for environment variables of the form TF_VAR_{variable name}
So, providing you have this set as an environment variable terraform will use it as the input value for x . Be aware though that environment variables have the lowest precedence of all of the input methods so if this variable value is set in a .tfvars file or on the command line then it will be overridden.

Another method would be to pass the value in as a command-line variable:

terraform apply -var="x=alice"
or with bash interpolation:
terraform apply -var="x=$TF_VAR_x"

Which can be mixed with other methods also:

terraform apply -var-file="alltheothervars.tfvars" -var="x=alice" 

Once you have the value passed in as an input variable you can then use it within your module in module expressions and the like.

Hope that helps

Happy Terraforming!

Thanks so much - following up on this , can I reference the value of x for a key within a map? I saw some suggestions on other forums and tried this in terraform.tfvars but it’s erroring out .

my_variables = {
        (var.x) = "mary"

Hi @v123,

What you shared there seems like it should be a valid snippet, but perhaps there’s something else in your configuration that’s causing a problem. It would help if you would share the entire error message from Terraform, exactly as Terraform printed it.

Thanks again - attaching the exact error . I am testing it with a simple setup on my local machine - I have never tried this config before but the intent it to try and assign the name of a key in a map to a variable passed in the input - which can be referenced later with a lookup . Not sure if what I am trying makes sense .

cat terraform.tfvars
filenames = “testing”

testmap = {
x = “i”
(var.filenames) = “abc”
variable “filenames” {
type = string
variable “testmap” {
type = map

~$ terraform plan

│ Error: Variables not allowed

│ on terraform.tfvars line 5:
│ 5: (var.filenames) = “abc”

│ Variables may not be used here.

│ Error: No value for required variable

│ on line 4:
│ 4: variable “testmap” {

│ The root module input variable “testmap” is not set, and has no default value. Use a -var or -var-file command line argument to provide a value for this variable.

Ah, I see: you are trying to use variable references in a .tfvars file.

That is always invalid, whatever syntax you use. .tfvars files are what assign the values to the root variables, so they need to be evaluated literally first before any references to the variables are possible.

If you want to produce a new value derived in some way from your input variables, the typical answer would be to define a local value inside your module, which can then refer to the input variables that are also declared in that module.

great thanks for clarifying that !