Managing Input Variables
1. Declaring Input Variables
| Variable Name | Type | Description | Default Value |
|---|---|---|---|
| instance_type | string |
EC2 instance type to launch | "t2.micro" |
| ami_id | string |
AMI ID for the EC2 instance | "ami-12345678" |
| count | number |
Number of instances to launch | 1 |
Example: Declaring Variables in Terraform
variable "instance_type" {
description = "EC2 instance type to launch"
type = string
default = "t2.micro"
}
variable "ami_id" {
description = "AMI ID for the EC2 instance"
type = string
default = "ami-12345678"
}
variable "count" {
description = "Number of instances to launch"
type = number
default = 1
}
2. Setting Variable Types
| Variable Type | Description | Example | Notes |
|---|---|---|---|
| string | Textual value | name = "John" |
Default type if not specified |
| number | Numeric value (integer or float) | port = 8080 |
Must be a valid number |
| bool | Boolean value (true or false) | enabled = true |
Valid values: true, false |
| list | Ordered collection of values | tags = ["web", "prod"] |
Values can be of any type |
| map | Collection of key-value pairs | labels = { env = "prod", team = "backend" } |
Keys must be strings, values can be any type |
| set | Ordered collection of unique values | allowed_ips = ["10.0.0.1", "10.0.0.2"] |
Introduced in Terraform 1.6 |
| tuple | Ordered collection of values with specific types | coordinates = [10.0, 20.0] |
Introduced in Terraform 1.6 |
| object | Collection of attributes with specific types | server = { name = "web", port = 80 } |
Keys are strings, values can be any type |
Example: Declaring a variable with type
variable "instance_type" {
type = string
description = "EC2 instance type"
default = "t3.micro"
}
Example: Using a list variable
variable "allowed_ports" {
type = list(number)
description = "List of allowed ports"
default = [22, 80, 443]
}
Example: Using a map variable
variable "environment_vars" {
type = map(string)
description = "Environment variables for the app"
default = {
NODE_ENV = "production"
LOG_LEVEL = "info"
}
}
3. Defining Default Values
| Variable | Default Value | Description |
|---|---|---|
| variable "instance_type" | "t2.micro" |
Instance type for EC2 launch template |
| variable "ami_id" | "ami-0c55b159cbfafe1d0" |
Amazon Machine Image ID for the instance |
| variable "vpc_cidr" | "10.0.0.0/16" |
Primary CIDR block for VPC |
Example: Using Default Values in a Variable Definition
variable "instance_type" {
description = "Type of EC2 instance to launch"
type = string
default = "t2.micro"
}
Example: Default Value in a Resource Block
resource "aws_instance" "web_server" {
ami = var.ami_id
instance_type = var.instance_type
vpc_security_group_ids = [
aws_security_group.web_sg.id,
]
}
4. Adding Variable Descriptions
| Property | Description | Example |
|---|---|---|
| description | Human-readable description of the variable | "Description of the variable" |
Example: Variable with Description
variable "instance_type" {
type = string
description = "The EC2 instance type to use"
default = "t3.micro"
}
Note: Descriptions are displayed in the Terraform console and documentation tools to help users understand the purpose of each variable.
5. Validating Variable Values
| Property | Description | Example |
|---|---|---|
| validation | Defines a validation rule for a variable | |
Example: Validating Instance Type
variable "instance_type" {
type = string
validation {
condition = contains(["t2.micro", "t2.small", "t2.medium"], var.instance_type)
error_message = "The instance_type must be one of t2.micro, t2.small, or t2.medium."
}
}
Example: Validating Numeric Range
variable "cpu_count" {
type = number
validation {
condition = var.cpu_count >= 1 && var.cpu_count <= 64
error_message = "The cpu_count must be between 1 and 64."
}
}
Note: Validation rules are evaluated during
terraform plan and terraform apply. If a validation fails, Terraform will display the error message and halt execution.
6. Marking Variables Sensitive
| Variable | Type | Sensitivity | Description |
|---|---|---|---|
| sensitive_var | string |
NEW | Variable marked as sensitive to prevent logging |
Note: Sensitive variables are not shown in logs or output unless explicitly requested.
7. Setting Variables via CLI
| Variable Source | Command Flag | Description |
|---|---|---|
| CLI Flags | -var |
Set a variable from the command line. Example: -var="instance_type=t3.micro" |
| CLI File | -var-file |
Load variables from a file (HCL or JSON format). Example: -var-file="prod.tfvars" |
| Environment Variables | TERRAFORM_ prefix |
Use environment variables with the TERRAFORM_ prefix. Example: TERRAFORM_instance_type=t3.micro |
Example: Setting Variables via CLI
terraform apply -var="instance_type=t3.micro" -var-file="prod.tfvars"
Note: CLI flags override variables set in files or environment variables. Use
-var for one-time overrides and -var-file for configuration files.
8. Using Variable Files
| File Type | Usage | Notes |
|---|---|---|
| .tfvars | terraform apply -var-file=prod.tfvars |
Standard variable file format for defining values |
| .tfvars.json | terraform apply -var-file=prod.json |
JSON format for variable files, useful for automation |
Example: Using a JSON variable file
{
"instance_type": "t3.medium",
"ami_id": "ami-0c55b159cbfafe1d0"
}
Note: Variable files are loaded in order of precedence, with
-var-file taking priority over default files like terraform.tfvars.
9. Using Environment Variables
| Variable | Source | Description |
|---|---|---|
| TF_VAR_<name> | Environment Variable | Set Terraform variables via environment variables prefixed with TF_VAR_ |
Example: Setting Environment Variables
export TF_VAR_instance_type="t3.micro"
export TF_VAR_ami_id="ami-12345678"
terraform plan
Note: Environment variables take precedence over
variables defined in .tfvars files or terraform.tfvars.
10. Referencing Variables
| Variable Type | Reference Syntax | Description |
|---|---|---|
| Local Variables | local.variable_name |
References a variable defined in the locals block |
| Input Variables | var.variable_name |
References an input variable defined in the variable block |
| Output Variables | output.output_name |
References an output variable defined in the output block |
| Module Variables | module.module_name.variable_name |
References a variable from a child module |
| Resource Attributes | resource_type.resource_name.attribute |
References an attribute from a resource |
| Data Attributes | data.data_type.data_name.attribute |
References an attribute from a data source |
Example: Referencing Variables in Expressions
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = local.instance_type
tags = {
Name = "${var.project_name}-web"
}
}
output "instance_id" {
value = aws_instance.web.id
}
Note: Variable references are resolved at runtime, not during parsing.
11. Setting Variable Precedence
| Source | Priority | Description |
|---|---|---|
| Environment Variables | 1 |
Set via TF_VAR_ prefix, highest priority |
| CLI Flags | 2 |
Passed with -var or -var-file |
| Variable Files | 3 |
Defined in .tfvars files, loaded with -var-file |
| Default Values | 4 |
Defined in variable blocks using default |
| Backend Configuration | 5 |
Values from remote state or backend configuration |
| Module Inputs | 6 |
Passed to modules from parent configurations |
Note: The
TF_VAR_ environment variables override any values set in variable files or default values.
12. Using Nullable Variables
| Property | Description | Default | Notes |
|---|---|---|---|
| nullable | Controls whether a variable can be set to null or undefined | false |
When true, the variable can accept null values. When false, Terraform will throw an error if a null value is provided. |