8

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
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Venki
  • 225
  • 4
  • 14

4 Answers4

12

You can use local instead

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

and then instead of using var. use local. like this:

resource "example" "example" {

   example = local.variable3

}

ref : https://www.terraform.io/docs/configuration/locals.html

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Montassar Bouajina
  • 1,482
  • 1
  • 8
  • 20
7

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

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • yes thank you @Charles you are right about the format of the output. or also we can use variable3 = var.variable1+"-"+var.variable2 in HCL2 format. I edited the answer thank you – Montassar Bouajina Jul 21 '20 at 09:24
  • Hi @MontassarBouagina i tried your solution, it throws an below error: **An input variable with the name "variable3" has not been declared. This variable can be declared with a variable "variable3" {} block.** – Venki Jul 21 '20 at 11:27
  • 1
    @Venki Please see the solution in my answer, it's the right one and will work for you! – Charles Xu Jul 22 '20 at 01:10
  • 1
    @Venki i think you are using check if you are using var.variable3 any where in your code you should change it with local.variable3 – Montassar Bouajina Jul 22 '20 at 13:10
  • @MontassarBouagina Thanks, yup it is working, can we place Custom Validation Rules for the local block which we place in variables i.e [here](https://www.terraform.io/docs/configuration/variables.html)or how can we restrict the length to the variable3 – Venki Jul 23 '20 at 07:19
  • @Venki yes you ca you can use validation within a variable here's an exemple : variable "variable1" { type = string description = "" validation { condition = length(var.variable1) > 4 error_message = "The length should be > 4" } } and can you please validate the answer ty :) – Montassar Bouajina Jul 23 '20 at 09:29
  • @MontassarBouagina, ya for variable -validation is possible, what about local block? – Venki Jul 23 '20 at 10:00
  • 1
    @Venki no i don't think so any way you don't need a validation on a local since it's gonna be depending on the 2 orther variables so just validate you 2 variable and you ok to go – Montassar Bouajina Jul 23 '20 at 11:03
  • @Venki ty but why u didn't validate my answer ? – Montassar Bouajina Jul 23 '20 at 13:25
1

You can't do this. Docs clearly states:

The default argument requires a literal value and cannot reference other objects in the configuration.

But you could probably use locals for variable3.

Marcin
  • 215,873
  • 14
  • 235
  • 294
0

To my knowledge what you want is not doable with default.

However you can create variable3 and just not assign it a default value and then in your call set variable3 = var.variable1-var.variable2

Not sure this solves your problem but to my knowledge the way you want to do it, won't work.

Also I would recommend upgrading to v0.12.

Berimbolinho
  • 465
  • 1
  • 6
  • 16