HCL Language – 3 – Variables in HCL

Variables in HCL

Variables are a fundamental component of HashiCorp Configuration Language (HCL). They allow you to parameterize your configurations, making them more flexible and reusable. In this guide, we’ll explore the use of variables in HCL, their declaration, interpolation, and best practices.

Variable Declaration

In HCL, variables are declared using the “variable” block. Each variable has a name and an optional description. Here’s an example of variable declaration:


variable "region" {
  description = "AWS region"
}
    

In this example, we declare a variable named “region” with a description indicating that it represents the AWS region.

Variable Types

HCL variables can be of various types, such as string, number, boolean, list, or map. The variable type is automatically inferred based on the assigned value. For instance:


variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}
    

In this example, we specify that “instance_type” is a string variable, and we provide a default value of “t2.micro.”

Variable Interpolation

One of the powerful features of HCL is variable interpolation. It allows you to use the values of variables within your configurations. Interpolation is achieved using the “${}” syntax.


resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
  count         = 2
}
    

In this example, we interpolate the value of the “instance_type” variable into the “instance_type” property of an AWS EC2 instance resource.

Variable Scope

HCL variables have scope within the configuration. They can be defined at the root level, within resource blocks, or even within modules. However, the scope of a variable determines where it can be used.


variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
}
    

In this case, the “instance_type” variable is declared at the root level and can be used within the resource block.

Best Practices for Variables

When working with variables in HCL, it’s essential to follow some best practices to maintain clear and maintainable configurations:

  • Use Descriptive Names: Choose meaningful names for your variables to improve code readability.
  • Provide Descriptions: Include descriptions for variables to document their purpose.
  • Set Defaults: Assign default values to variables to ensure your configurations work even when a value isn’t explicitly provided.
  • Keep Variables Close to Usage: Declare variables close to where they are used to maintain context and readability.
  • Avoid Changing Variable Types: Changing the type of a variable can lead to errors, so it’s best to keep the type consistent.
Conclusion

Variables are a crucial part of HCL that enables you to create dynamic and reusable infrastructure configurations. By understanding how to declare, assign types, and interpolate variables, you can write more flexible and adaptable code for tools like Terraform.