Pass Variable from Command Line

In last blogs we create two files. and with the below content. contains the below content.

variable username {} with below content.

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

then we run terraform plan then terraform will ask us variable value that is not defined.

Now we want to supply the variable value from the command line. so that we can run terraform plan command in non-interactive node.

for that we will use below.


terraform plan -var "<VARIABLE_NAME>=<VARIABLE_VALUE>"

let's run the below command and see the output

terraform plan -var "username=Learning-Ocean"


└─$ terraform plan -var "username=Learning-Ocean"
Changes to Outputs:
  + printname = "Hello, Learning-Ocean"
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