0
0
Terraformcloud~5 mins

Terraform.tfvars file - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use Terraform to create cloud resources, you often need to provide values like names, sizes, or regions. The terraform.tfvars file helps you keep these values separate from your main setup so you can change them easily without touching the main code.
When you want to set specific values for your Terraform variables without changing the main configuration files.
When you have different environments like development and production and want to use different settings for each.
When you want to share your Terraform code but keep sensitive or environment-specific values separate.
When you want to quickly change resource names or sizes without editing multiple files.
When you want to keep your Terraform setup clean and organized by separating variable values.
Config File - terraform.tfvars
terraform.tfvars
region = "us-east-1"
instance_type = "t3.micro"
app_name = "my-app"
instance_count = 2

This file sets values for variables used in your Terraform configuration.

region: The cloud region where resources will be created.

instance_type: The size/type of the virtual machine.

app_name: A name to identify your application.

instance_count: How many instances to create.

Terraform automatically loads this file to apply these values during deployment.

Commands
This command prepares your Terraform working directory by downloading necessary plugins and setting up the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will do based on your configuration and the values in terraform.tfvars, without making any changes yet.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.my-app will be created + resource "aws_instance" "my-app" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t3.micro" + tags = { + "Name" = "my-app" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create or update your cloud resources using the values from terraform.tfvars without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.my-app: Creating... aws_instance.my-app: Still creating... [10s elapsed] aws_instance.my-app: Creation complete after 20s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips the confirmation prompt to apply changes immediately
This command shows the values of outputs defined in your Terraform configuration after deployment, confirming what was created.
Terminal
terraform output
Expected OutputExpected
instance_ip = "54.210.123.45"
Key Concept

If you remember nothing else from this pattern, remember: terraform.tfvars lets you set and change variable values separately from your main Terraform code for easy and safe configuration.

Common Mistakes
Not naming the file terraform.tfvars or using a different name without specifying it.
Terraform automatically loads terraform.tfvars but ignores other names unless explicitly told, so your values won't be applied.
Always name your variable values file terraform.tfvars or pass a different file with -var-file flag.
Putting syntax errors like missing quotes or wrong equals signs in terraform.tfvars.
Terraform will fail to read the file and stop with an error, blocking deployment.
Use correct syntax: key = "value" for strings, key = number for numbers, and check carefully.
Editing terraform.tfvars but forgetting to run terraform plan or apply again.
Changes won't take effect until you re-run Terraform commands to update your infrastructure.
After changing terraform.tfvars, always run terraform plan to see changes and terraform apply to apply them.
Summary
terraform.tfvars file holds variable values separately from main Terraform code.
terraform init sets up Terraform environment and plugins.
terraform plan shows what changes will happen using the variable values.
terraform apply creates or updates resources using those values.
terraform output displays information about created resources.