Setting a Variable Using an Environment Variable

We will learn to set a variable in Terraform using an environment variable.

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:

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
Terminal 1
Terminal

Click to Connect...

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:

Setting a variable using environment variables

📝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.

Setting a Variable on the Command Line
Setting a Variable Using a File
Mark as Completed
Report an Issue