Setting a Variable Using a File
We will learn how to set variables in Terraform using a file.
We'll cover the following
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:
/
- terraform.tfvars
📝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.