HCL Language – 11 – Input Variables in Modules

Input Variables in Terraform Modules

In HashiCorp Configuration Language (HCL), input variables play a crucial role in making Terraform modules flexible and reusable. They allow you to customize module behavior by passing values from the calling configuration. In this guide, we’ll explore how input variables work within Terraform modules.

Defining Input Variables

Input variables in Terraform modules are defined within the module using the variable block in a file, often named variables.tf. These variables can have a name, type, and an optional description for documentation.


# my_module/variables.tf
variable "instance_count" {
  type        = number
  description = "The number of instances to create."
}

variable "region" {
  type        = string
  description = "The AWS region where instances will be launched."
}
    

In this example, the “my_module” module defines two input variables: instance_count of type number and region of type string.

Passing Input Variables

To use a Terraform module with input variables, you include it in your main configuration file and provide values for the variables within the module block.


module "example_module" {
  source          = "./my_module"
  instance_count  = 3
  region          = "us-west-2"
}
    

Here, the module block references the “example_module,” which uses the “my_module” module. It provides values for the input variables instance_count and region.

Variable Validation and Default Values

Input variables can include validation rules and default values to ensure that the provided values meet specific criteria. For example, you can set a default value or require that a variable is defined.


# my_module/variables.tf
variable "instance_count" {
  type        = number
  description = "The number of instances to create."
  default     = 2
}

variable "region" {
  type        = string
  description = "The AWS region where instances will be launched."
  validation {
    condition = can(regex("us-(east|west)-[0-9]", var.region))
    error_message = "Region must be in the format 'us-east-X' or 'us-west-X'."
  }
}
    

In this modified example, the instance_count variable has a default value of 2, while the region variable includes a validation rule that checks if the provided value matches the regex pattern for AWS regions.

Using Input Variables in Resources

Input variables are commonly used within resource blocks in the module’s configuration to make resource definitions dynamic. For example, you can use the instance_count variable to specify how many instances to create.


resource "aws_instance" "example" {
  count         = var.instance_count
  instance_type = "t2.micro"
  ami           = "ami-0c55b159cbfafe1f0"
  subnet_id     = aws_subnet.example.id
  availability_zone = "us-west-2a"
}
    

In this snippet, the count attribute for the aws_instance resource uses the instance_count input variable to determine how many instances to create.

Summary

Input variables are a fundamental aspect of Terraform modules in HCL, enabling flexibility and reusability in infrastructure code. By defining, passing, and validating input variables, you can create versatile modules that can be customized for various use cases while maintaining consistency and compliance with your infrastructure requirements.