TA-002-P · Question #204
Every region in AWS has a different AMI ID for Linux and these are keep on changing. What is the best approach to create the EC2 instances that can deal with different AMI IDs based on regions?
The correct answer is A. Use data source aws_ami. The aws_ami data source dynamically queries AWS for the correct AMI ID at runtime, automatically handling regional differences and AMI updates.
Question
Every region in AWS has a different AMI ID for Linux and these are keep on changing. What is the best approach to create the EC2 instances that can deal with different AMI IDs based on regions?
Options
- AUse data source aws_ami.
- BCreate a map of region to ami id.
- CCreate different configuration file for different region.
- DNone of the above
How the community answered
(41 responses)- A76% (31)
- B2% (1)
- C15% (6)
- D7% (3)
Why each option
The `aws_ami` data source dynamically queries AWS for the correct AMI ID at runtime, automatically handling regional differences and AMI updates.
The `aws_ami` data source lets you define filter criteria (e.g., name, owner, architecture) so Terraform resolves the correct AMI ID for the current region at plan or apply time. This eliminates the need to hardcode or manually maintain AMI IDs, which change frequently as AWS publishes new images.
A static region-to-AMI map requires manual updates every time AWS releases new AMIs, making it maintenance-heavy and error-prone across regions.
Separate configuration files per region create significant duplication and do not handle AMI ID changes over time without manual intervention.
The `aws_ami` data source is the correct and recommended approach, so 'None of the above' is incorrect.
Concept tested: Dynamic AMI lookup using aws_ami data source
Source: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
Topics
Community Discussion
No community discussion yet for this question.