0
0
Terraformcloud~5 mins

Variable declaration syntax in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Variables in Terraform let you customize your infrastructure without changing the code. They help you reuse configurations by setting values outside the main files.
When you want to set different values for your infrastructure in different environments like development and production.
When you want to avoid hardcoding sensitive information like passwords or API keys in your configuration files.
When you want to share your Terraform code with others and let them provide their own values.
When you want to make your Terraform modules flexible and reusable with different inputs.
When you want to organize your configuration by separating variable definitions from resource definitions.
Config File - variables.tf
variables.tf
variable "region" {
  description = "The AWS region to deploy resources"
  type        = string
  default     = "us-east-1"
}

variable "instance_count" {
  description = "Number of EC2 instances to launch"
  type        = number
  default     = 2
}

variable "tags" {
  description = "Tags to apply to resources"
  type        = map(string)
  default     = {
    Environment = "dev"
    Project     = "example"
  }
}

This file declares three variables:

  • region: a string with a default AWS region.
  • instance_count: a number for how many instances to create.
  • tags: a map of strings for resource tags.

Each variable has a description, type, and default value to make the configuration clear and flexible.

Commands
Initializes the Terraform working directory and downloads necessary provider plugins.
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!
Checks the configuration files for syntax errors and validates variable declarations.
Terminal
terraform validate
Expected OutputExpected
Success! The configuration is valid.
Shows the execution plan using the declared variables and their default values.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist.
Key Concept

If you remember nothing else from this pattern, remember: variables let you customize Terraform configurations without changing the code.

Common Mistakes
Not specifying the variable type and relying only on default values.
Terraform may infer wrong types or cause errors when you provide unexpected input later.
Always declare the variable type explicitly to ensure correct input and validation.
Hardcoding values directly in resource blocks instead of using variables.
This makes the configuration less flexible and harder to reuse in different environments.
Use variables to pass values into resources, making your code reusable and easier to manage.
Forgetting to run 'terraform init' before other commands.
Terraform needs to initialize providers and modules before planning or applying changes.
Always run 'terraform init' first in a new or changed working directory.
Summary
Declare variables in a separate file with type, description, and default values.
Run 'terraform init' to prepare the working directory and download providers.
Use 'terraform validate' to check for syntax and variable declaration errors.
Use 'terraform plan' to see how variables affect the infrastructure before applying.