HCL Language – 15 – HCL Interpolation

HCL Interpolation

HCL (HashiCorp Configuration Language) provides a powerful feature known as interpolation, allowing you to dynamically insert values into your configuration. Interpolation is a fundamental concept in HCL, enabling you to create flexible and reusable Terraform configurations. In this guide, we’ll explore the various forms of interpolation, how to use them, and practical examples.

String Interpolation

String interpolation in HCL allows you to embed variables and expressions within double-quoted strings. This is especially useful for constructing resource names, labels, and other dynamic string values.


resource "aws_instance" "example" {
  ami = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "web-${var.environment}-instance"
  }
}
    

In this example, the tag “Name” is dynamically generated by interpolating the value of the “var.environment” variable into the string.

List and Map Interpolation

HCL supports interpolation within lists and maps, allowing you to create dynamic lists of values or maps of key-value pairs.


variable "security_groups" {
  type = list(string)
  default = ["sg-web", "sg-db"]
}

resource "aws_security_group" "example" {
  count = length(var.security_groups)
  name = "sg-${element(var.security_groups, count.index)}"
  // Other configuration settings
}
    

Here, the “name” attribute of each security group resource is interpolated using the elements of the “security_groups” list.

Attribute Interpolation

Attribute interpolation allows you to reference attributes of other resources in your configuration. It’s a key part of resource relationships and dependencies.


resource "aws_instance" "web" {
  ami = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

resource "aws_instance" "db" {
  ami = "ami-0c55b159cbfafe1f0"
  instance_type = "m4.large"
}

resource "aws_security_group" "web_sg" {
  name = "web-sg"
  // Other configuration settings
}

resource "aws_security_group" "db_sg" {
  name = "db-sg"
  // Other configuration settings
}

resource "aws_security_group_rule" "web_to_db" {
  type = "ingress"
  from_port = 3306
  to_port = 3306
  protocol = "tcp"
  security_group_id = aws_security_group.web_sg.id
  source_security_group_id = aws_security_group.db_sg.id
}
    

In this example, the “security_group_id” and “source_security_group_id” attributes are interpolated to reference the “id” attributes of the “aws_security_group” resources.

Function Interpolation

HCL provides built-in functions for performing operations within interpolations. Functions like “lookup,” “element,” and “coalesce” allow you to manipulate data and make decisions dynamically.


variable "environment" {
  type = map(string)
  default = {
    dev = "development"
    prod = "production"
  }
}

resource "aws_instance" "example" {
  ami = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Environment = coalesce(lookup(var.environment, "nonexistent"), "unknown")
  }
}
    

In this case, the “coalesce” and “lookup” functions are used to handle potential errors or missing data in the variable interpolation.

Summary

HCL interpolation is a powerful feature that enables dynamic and flexible configuration in Terraform. With string, list, map, attribute, and function interpolation, you can create configurations that adapt to changing requirements and environments. Interpolation is a key concept in writing concise and maintainable Terraform code.