Course contentsShow
← Cloud and Infrastructure
Lesson 732 of 2,98914. Terraform FundamentalsFree lesson

String Literals and String Interpolation

Working with strings in HCL including quoted strings, heredocs, and ${} interpolation syntax.

String Literals and String Interpolation

What you'll learn: How to work with text values in Terraform's HCL, including basic strings, multi-line text, and embedding dynamic values inside strings.

The Core Concept

Strings are fundamental to Infrastructure as Code—you use them for resource names, tags, configuration values, and more. HCL gives you three powerful ways to work with strings:

Quoted Strings are your everyday text values wrapped in double quotes:

resource "aws_s3_bucket" "example" {
  bucket = "my-app-bucket"
}

Heredocs let you write multi-line strings without escape characters, perfect for scripts or configuration files. They start with << followed by a delimiter (often EOF):

user_data = <<EOF
#!/bin/bash
echo "Hello from the cloud"
apt-get update
EOF

String Interpolation is where the magic happens. Using ${} syntax, you can embed dynamic values—like resource attributes or variables—directly into your strings:

resource "aws_instance" "web" {
  ami           = "ami-12345"
  instance_type = "t2.micro"
  
  tags = {
    Name = "web-server-${aws_instance.web.id}"
  }
}

Think of interpolation like mail-merge: you write a template string with placeholders, and Terraform fills in the actual values when it runs. This lets you build descriptive names, construct URLs, or assemble configuration based on other resources.

You can even do simple operations inside the ${}:

description = "Created on ${timestamp()}"

Why This Matters

String interpolation is essential for creating relationships between resources—referencing one resource's output in another's configuration—which is the heart of building real infrastructure.

Key Takeaway: Use quoted strings for static text, heredocs for multi-line content, and ${} interpolation to dynamically embed resource attributes and expressions into your strings.