Interview Prep/Terraform/Resources Variables and Expressions

Replacing a database should be forced when a config file changes, but the file is not an attribute of the resource. How?

IntermediateScenarioResources Variables and Expressions
Answer

Situation read → You need a trigger that doesn't map to a resource argument.

What I'd do →

  1. Track the hash in a terraform_data resource.
  2. Use replace_triggered_by to link it to the target.
resource "terraform_data" "config_version" {
  input = filesha256("${path.module}/app.conf")
}
resource "aws_instance" "app" {
  ami           = var.ami
  instance_type = "t3.small"
  lifecycle {
    replace_triggered_by = [terraform_data.config_version]
  }
}

Why → terraform_data is the built-in replacement for null_resource, and replace_triggered_by (1.2+) expresses the intent declaratively.

Interview tip → Use it sparingly; every forced replacement is downtime for a stateful thing.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions