Setting a Variable Using a File

We will learn how to set variables in Terraform using a file.

Another way to set the value of variables is by using a file. We’ll create a new file in the project called terraform.tfvars. We are going to use the same Terraform code in the main.tf that we did before. Inside the terraform.tfvars file we’ll place the following:

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
/
terraform.tfvars
main.tf
Set up the variables using a file

📝Note: Please do change <your-bucketname> with your own chosen bucket name.

terraform.tfvars file#

terraform.tfvars is a special filename that Terraform looks at to discover values for variables. Terraform will look in this file and use any values given for a variable. To set the value of the variable you simply put the variable identifier, an equals sign, and the value you want to give it. We are setting the value of both bucket_name and bucket_suffix in our file. Now when we run the project, Terraform will use those values for the variables and not ask us for them.

Another file name#

The other way we could have named our file is by using anything ending in .auto.tfvars. Terraform examines files with that ending for set variables. It is also possible to define multiple files and put the value for different variables in each of them.

📝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 Using an Environment Variable
More Complex Variables
Mark as Completed
Report an Issue