Some of the variables that we are going to use in our code could be very sensitive, for example credentials and access keys. There should not be gone into version control. Therefore, it is possible to fetch these variable values using an additional file named terraform.tfvars. When values are populating for variables, Terraform will fetch vaules for vairables from this file and pass them into the code.
For example, let's again provision a simple EC2 instance,
As usual, this is going to initialize the aws provider for Terraform. It requires access key, secret key and region taken in from variables. Now, access key and secret key are sensitive and therefore we should preferably take these from a terraform.tfvars.
In above file I have stilled ignored specifying the region, which can be taken from the normal vars.tf file.
Create a file named vars.tf,
According to above file, AWS_ACCESS_KEY and AWS_SECRET_KEY will be fetched from terraform.tfvars since we haven't provided any value in there and rest of the variables works as usual.