What is variable.tf and terraform.tfvars?



Table of Content

  1. Terraform variable file - variable.tf
  2. Terraform tfvars file - terraform.tfvars
  3. Terraform multiple tfvars file
  4. Terraform setting variable using command line var


Pre-requisite

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

  1. You must install terraform (click here on how to install terraform)
  2. You must have either AWS or Google Cloud account (Click to here for AWS and Google Cloud terraform setup tutorial)

1. Types of Terraform Variables

Create a main.tf file

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2AYK7R6PQ"
 4   secret_key = "S1Yg1Qm2JNSej8EHdhPTiu5l5ZD36URsedf32kNT"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type =  var.instance_type
11
12   tags = {
13           Name = "Terraform EC2"
14   }
15} 

Now create variable.tf

1variable "instance_type" {
2   description = "Instance type t2.micro"
3   type        = string
4   default     = "t2.micro"
5}

Here is the sequence of terraform command which you need to run

1terraform init
1terraform plan
1terraform apply

And finally you can destroy the complete environment using -

1terraform destroy 

2. Terraform tfvars file - terraform.tfvars

Create a main.tf file

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2AYK7R6PQ"
 4   secret_key = "S1Yg1Qm2JNSej8EHdhPTiu5l5ZD36URsedf32kNT"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type =  var.instance_type
11
12   tags = {
13           Name = "Terraform EC2"
14   }
15} 

Now create variable.tf but without any default values

1variable "instance_type" {
2}

Finally create terraform.tfvars

1instance_type="t2.micro"

Here is the sequence of terraform command which you need to run

1terraform init
1terraform plan
1terraform apply

And finally you can destroy the complete environment using -

1terraform destroy 

3. Terraform multiple tfvars file

There can be situation where you need create multiple tfvars files based on the environment like stage, production.

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

  1. stage.tfvars
  2. production.tfvars

First Create your main.tf

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2AYK7R6PQ"
 4   secret_key = "S1Yg1Qm2JNSej8EHdhPTiu5l5ZD36URsedf32kNT"
 5}
 6resource "aws_instance" "ec2_example" {
 7
 8   ami           = "ami-0767046d1677be5a0"
 9   instance_type =  var.instance_type
10
11  tags = {
12           Name = var.environment_name
13   }
14
15} 

Second create your variable.tf

1variable "instance_type" {
2}
3
4variable "environment_name" {
5}

And third create your stage.tfvars for staging

1instance_type="t2.micro"
2
3environment_name ="stage" 

Apply the terraform configuration using following command -

1terraform init
1terraform plan -var-file="stage.tfvars"

And then

1terraform apply -var-file="stage.tfvars"

You can destroy the environment by running following command

1terraform destroy -var-file="stage.tfvars"

4. Terraform setting variable using command line var

Create a main.tf file

 1provider "aws" {
 2   region     = "eu-central-1"
 3   access_key = "AKIATQ37NXB2AYK7R6PQ"
 4   secret_key = "S1Yg1Qm2JNSej8EHdhPTiu5l5ZD36UsdfA99NyYT"
 5}
 6
 7resource "aws_instance" "ec2_example" {
 8
 9   ami           = "ami-0767046d1677be5a0"
10   instance_type =  var.instance_type
11
12   tags = {
13           Name = "Terraform EC2"
14   }
15} 
16
17variable "instance_type" {
18}

Now run the following terraform command

1terraform init
1terraform plan -var="instance_type=t2.micro"
1terraform apply -var="instance_type=t2.micro"

Finally you destroy the EC2 environment using following command -

1terraform destroy -var="instance_type=t2.micro"

Read More -

  1. Install terraform on Ubuntu 20.04, CentOS 8, MacOS, Windows 10, Fedora 33, Red hat 8 and Solaris 11
  2. How to setup Virtual machine on Google Cloud Platform using terraform
  3. Create EC2 Instance on AWS using terraform
  4. How to use Terraform Input Variables
  5. What is variable.tf and terraform.tfvars?
  6. How to use Terraform locals?
  7. How to use Terraform output values?
  8. Understanding terraform count, for_each and for loop?
  9. Cloud-nuke : How to nuke AWS resources and save additional AWS infrastructure cost?
  10. How to use Terraform Dynamic blocks?
  11. How to use Terraform resource meta arguments?
  12. How to use Terraform Data sources?
  13. What is terraform provisioner?
  14. Terraform how to do SSH in AWS EC2 instance?