Multiple Variable

We can defined any number of variable in same way. let see it with example so create variable.tf file with below content.

variable "username" {}

variable "age" {}

create hello-variable.tf file with below content.

output "printname" {
  value = "Hello, ${var.username}, your age is ${var.age}"
}

here are declaring two variable username and age in variable.tf. and printing them in hello-variable.tf

let's run terraform plan command and see the output

output:

┌──(gaurav㉿learning-ocean)-[~/youtube-course/hello-variable]
└─$ terraform plan
var.age
  Enter a value: 23

var.username
  Enter a value: Gaurav Sharma

Changes to Outputs:
  + printname = "Hello, Gaurav Sharma, your age is 23"

You can apply this plan to save these new output values to the Terraform state, without changing any
real infrastructure.
───────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly
these actions if you run "terraform apply" now.

we can pass the multiple variable from command line using below syntex.

$terraform plan -var "<VARIABLE_1_NAME>=<VARIABLE_1_VALUE>" -var "<VARIABLE_2_NAME>=<VARIABLE_2_VALUE>"

\

let's see it in actions.


┌──(gaurav㉿learning-ocean)-[~/youtube-course/hello-variable]
└─$ terraform plan -var "username=Learning-Ocean" -var "age=23"
Changes to Outputs:
  + printname = "Hello, Learning-Ocean, your age is 23"

You can apply this plan to save these new output values to the Terraform state, without changing any
real infrastructure.

───────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly
these actions if you run "terraform apply" now.

Demo Video