Every AMI refresh recreates the fleet, and you want a pinned and a "latest" option. How do you look up AMIs?
BeginnerScenarioResources Variables and Expressions
Answer
Situation read → A most_recent data source changes results over time.
What I'd do →
- Use a data source with explicit filters, or an SSM public parameter.
- Offer an
ami_idvariable that overrides the lookup for prod.
data "aws_ssm_parameter" "al2023" {
name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64"
}
resource "aws_instance" "app" {
ami = coalesce(var.ami_id, data.aws_ssm_parameter.al2023.value)
}
Why → Dev follows latest, prod is pinned and promoted explicitly, so an AMI release never surprises a production plan.
Interview tip → Better still: bake AMIs with Packer and pass the ID in.
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