Search code examples
terraformterraform-variables

How to define default value of one variable based on other variables in Terraform variable.tf file?


For example, in variable.tf file we have this code:

variable "variable1" {
    type    = string
    default = "ABC"
}

variable "variable2" {
    type    = string
    default = "DEF"
}

variable "variable3" {
    type    = string
    default = "$var.variable1-$var.variable2"
}

Expected output:

variable3 = ABC-DEF

Solution

  • Yes, I agree with @Montassar, you can use the local block to create a new expression from the existing resources or the variables. But it should combine the variables like this:

    locals {
      variable3 = "${var.variable1}-${var.variable2}"
    }
    

    And it will look like this:

    enter image description here