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 →
- Track the hash in a
terraform_dataresource. - Use
replace_triggered_byto 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.
More on Resources Variables and Expressions
You need three identical web servers. How do you declare them, and what is the catch with `count`?BeginnerYou manage subnets from a list of CIDRs. Someone removes the second CIDR and Terraform plans to recreate the third subnet. Why, and what do you change?IntermediateHow do you stop someone passing `environment = "production"` when only `dev`, `stage` and `prod` are valid?BeginnerA module takes a database password and exposes the connection string as an output. How do you stop them leaking into plan output?BeginnerA monitoring stack should only exist in prod. How do you create a resource conditionally?BeginnerYou have subnets defined as `{ web = {...}, db = {...} }` per VPC, nested two levels deep. How do you create them with `for_each`?Advanced