Interview Prep/Terraform/Resources Variables and Expressions

User data scripts are long inline strings. How do you manage them?

BeginnerScenarioResources Variables and Expressions
Answer

Situation read → Unreadable heredocs inside HCL.

What I'd do → Move the script into a template file and render it.

resource "aws_instance" "app" {
  user_data = templatefile("${path.module}/templates/boot.sh.tftpl", {
    app_version = var.app_version
    region      = var.region
  })
  user_data_replace_on_change = true
}

Why → The script becomes lintable shell, and user_data_replace_on_change makes the change visible in the plan as a replacement.

Interview tip → Escape literal ${ as $${ in template files.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions