Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to set up variables in Terraform using files with AWS

Faraz Karim

Input variables

Variables in Terraform allow you to alter the functioning of a module without having to make any changes to the source code, allowing you to share modules across different configurations in Terraform. To find out more about Terraform input variables click here.

To specify the value of these variables, you may need to use Command Line Instructions, though an easier and more efficient alternative is using files.

Defining and setting variables using a (.tfvars) file

Terraform variables files serve as an effective means to specify the value of several variables at a time. To construct such a file, you code the required variable name assignments inside the file and choose a filename ending with .tfvars. Such a file does not need to have actual assignments since any value stored in them will be overwritten when an accompanying file ( in this case) is run where the variables are being set.

The variables file must be in the same directory as the file setting the values of the variables.

Following is an example where we define three variables in our myvars.tfvars file and use the file to set the values of these variables:

stu_name = "to be set"
stu_id = "to be set "
stu_courses = []

Setting variable values without an accompanying file

Terraform also automatically loads variable definition files with the exact name terraform.tfvars or any file ending with .auto.tfvars. There is no need for an accompanying file to initialize the variable values for such files, and the variables are set to the values specified in the file.




Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring