Setting a Variable Using an Environment Variable
We will learn to set a variable in Terraform using an environment variable.
We'll cover the following
Another way you can set the value of a variable is by using an environment variable. To do this, set
an environment variable in your terminal using the convention TF_VAR_<variable_identifier>
.
Click the terminal to run the terraform init
command:
Environment variables usage in Terraform#
Let’s set environment variables for each variable in the project we used in the last lesson. Follow the instructions below for the given terminal:
📝Note: Please do change
<your-bucketname>
with your own bucket name.
Once you have set the environment variables, run terraform apply
. You will see that now Terraform
will not ask you for a value for either variable and will use the values from your environment
variables.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.