How do I get the value of a variable whose name is based on another variable?

Jithin Kumar S

inside variables.tf

locals {
  env_name = lower(terraform.workspace)
}

inside tfvars

hc-auth-demo-port= 8003

main.tf

port= ${var.hc-auth-"${local.env_name}"-port}"

Getting error:

This character is not used within the language.

Based on an earlier answer, I've tried (which fixes that error, but doesn't solve my actual problem):

port = "${var.hc-auth}-${local.env_name}-port"

It is saying hc.auth is not declared:

An input variable with the name "hc-auth" has not been declared. This variable can be declared with a variable "hc-auth" {} block.

I've declared hc-auth-default-port in the variables, so how do I append all the 3 words to build that variable name and get its value?

this is the error msg --> this means it is looking for a variable hc.auth but in my case I wanted hc-auth-default-port interpolation

How do I get the value of a variable whose name is based on another variable?

Alain O'Dea

Terraform does not support building variable names from variables, but there is a workaround: using maps.

Provide a map of ports in terraform.tfvars (notice the use of snake_case, not skewer-case, as snake_case is the preferred code style for Terraform code):

hc_auth_ports = {
  demo = 8003
}

Declare a map(int) variable hc_auth_ports in main.tf or variables.tf:

variable "hc_auth_ports" {
  type = map(int)
}

Update main.tf to look up the port for the environment in the hc_auth_ports:

port = var.hc_auth_ports[local.env_name]

Now, you should get the outcome you want:

  1. declare the ports for each environment in tfvars
  2. able to choose the port for the current environment in main.tf

Original Answer to original question

The original question was effectively:

Why do I get error "This character is not used within the language." when trying to append variables in a string?

Here is the port assignment with the errors corrected:

port = "${var.hc-auth}-${local.env_name}-port"

This kind of expression is called a String Template.

Here is the original (broken) expression:

port = ${var.hc-auth-"${local.env_name}"-port}"

Here is why it doesn't work:

  1. missing double quote (") to start the string before ${var.hc_auth (likely what triggered the error message since ${ is illegal outside a String Template)
  2. missing closing brace (}) to close the variable interpolation of var.hc_auth
  3. incorrect double quote (") before ${local.env_name}
  4. incorrect closing brace (}) after -port

An interpolation takes the following form:

${hcl_expression}

It must be inside a string.

A ${ ... } sequence is an interpolation, which evaluates the expression given between the markers, converts the result to a string if necessary, and then inserts it into the final string:

"Hello, ${var.name}!"

In the above example, the named object var.name is accessed and its value inserted into the string, producing a result like "Hello, Juan!".

From Interpolation - String Templates - Configuration Syntax

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I manipulate a variable whose name conflicts with PDB commands?

How do I create a Variable in PL/SQL whose value persists?

How do I get a batch variable to influence another variable?

How do i change a property in state whose name is the value of a variable?

How do I create a variable that increments by 1 based on the value of another variable?

How can I use a variable's value to look up another variable whose name is said value

How do I conditionally change the value of a variable based on the value of another variable?

How do I get the raw value of a variable?

How do I get a value from a variable in another View Controller

How do i print Variable name instead of the value assigned to the variable?

How do I get get the value of a variable?

Bash: Echo a variable whose name is the value of another variable

How to call a value of a variable whose name is passed as text to another variable?

How do I store a variable in my controller whose value I get after DOM has loaded?

how do I get the variable name of a list

zsh: How to get value of variable based on a another variable?

How do I assign a variable in Bash whose name is expanded ($) from another variable?

In action script 3, how can I dynamically include a file with certain name, based on a value of another variable?

How do I store textbox name reference in a Variable to later assign the value of that textbox to another variable

XSLT: How do I get the value of a variable by concatenating a string and another variable?

How do i use variable's value as a dynamic variable name?

how to get a json variable Whose value contains a "-"

how to use the value of a variable as the name of another variable

How do I get a print of a variable value by executing another variable in sql server?

How do I get the value of a column in a Pandas DataFrame with the name based on another columns value on the same row?

how to print environment variable value in shell of a variable whose name is present in another variable value

How do I set the value of a variable based on another variable?

How do I add to a variable based on another variables value? (Python)

How do I change a value within a variable based on the value of another variable in R?