What is variable.tf and terraform.tfvars?

Terraform variable:-

Terraform variable.tf is a file where you can define variables for your Terraform configuration. This file can contain the variable definitions as well as the optional default value for the variable. Here is an example of variable.tf which has -

  1. Two variables with no default value - instance_type, github_repo

  2. One variable with default value - location

Terraform.tfvars:-

Terraform.tfvars is a file where you assign a value to the variables. I am just gonna use the previous variable.tf and assign the values to the variables -

Here are the benefits of using terraform.tfvars -

  1. You can have multiple terraform.tfvars based on your project setup for example -

    • DEV - terraform-dev.tfvars

    • QA - terraform-qa.tfvars

    • PROD - terraform-prod.tfvars

Pre-requisite

Before we start working with Terraform variables, here are the pre-requisites -

  1. You must install terraform

  2. You must have AWS Account

How to create variable.tf?

Let's take a very basic example to understand the concept of variable.tf in terraform. In this example, we are going to set up an EC2 instances on AWS

For setting up an EC2 Instance we will need the following information -

  1. Region - location

  2. Instance Type - instance_type

  3. Tags - tag

Let's create variable.tf file for region, instance type, and tags -

Here is the code for variable.tf

Create main.tf for provisioning EC2 instance -

How to create terraform.tfvars?

After creating the variable.tf, let's create .tfvars and in that file, we are going to assign values to the variable -

  1. location - "eu-central-1"

  2. instance_type - "t2.micro"

  3. tag - "EC2 Instnace for DEV"

How to create multiple .tfvars files for different environments?

There can be a situation where you need to create multiple tfvars files based on the environment like DEV, QA, PRODUCTION

So in such a scenario, you can create one tfvars file for each environment -

  1. terraform-dev.tfvars

  2. terraform-qa.tfvars

  3. terraform-prod.tfvars

DEV:-

QA:-

PROD:-

How do you pass a variable(.tfvars) to the command line to Terraform using --var-file?

Referencing the previous example, we can pass the variables terraform-dev.tfvars, terraform-qa.tfvars, terraform-prod.tfvars based on the environment we are working.

Here is how I am going to run the terraform init, terraform plan, and terraform apply command in DEV, QA, and PROD environment.

DEV - Keep in mind that you have to supply the correct .tfvars file based on the environment you are working on.

QA - Keep in mind that you have to supply the correct .tfvars file based on the environment you are working on.

PROD - Keep in mind that you have to supply the correct .tfvars file based on the environment you are working on.

Difference between terraform.tfvars vs variables.tf

Conclusion

I hope this article will help you to understand the variables.tf and terraform.tfvars in a more detailed way.