AMI lookup data source example in Terraform - Time & Space Complexity
We want to understand how the time to find an AMI image changes as we look up more images.
Specifically, how does the number of API calls grow when searching for AMIs?
Analyze the time complexity of the following operation sequence.
data "aws_ami" "example" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
owners = ["amazon"]
}
This code looks up the most recent Amazon Linux 2 AMI by filtering images owned by Amazon.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: AWS API call to search AMIs with filters
- How many times: Once per data source usage in Terraform plan/apply
The API call searches through available AMIs matching the filter. As the number of AMIs grows, the search internally checks more images.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 1 API call, internal search over 10 images |
| 100 | 1 API call, internal search over 100 images |
| 1000 | 1 API call, internal search over 1000 images |
Pattern observation: The number of API calls stays the same, but the internal search work grows with the number of images.
Time Complexity: O(n)
This means the time to find the AMI grows linearly with the number of images available.
[X] Wrong: "The lookup always takes the same time no matter how many AMIs exist."
[OK] Correct: The API call is one, but the search inside AWS checks all matching images, so more images mean more work.
Understanding how cloud API calls scale helps you design efficient infrastructure code and anticipate delays.
"What if we removed the filter and searched all AMIs? How would the time complexity change?"
Practice
aws_ami data source in Terraform?Solution
Step 1: Understand the role of
Theaws_amidata sourceaws_amidata source is used to query existing AMIs, not to create or delete them.Step 2: Identify its main use
It helps find the latest AMI by applying filters like name, owner, and tags.Final Answer:
To find the latest Amazon Machine Image (AMI) based on filters -> Option AQuick Check:
AMI lookup = find latest AMI [OK]
- Confusing data source with resource creation
- Thinking it deletes AMIs
- Assuming it launches instances directly
Solution
Step 1: Identify correct resource type and block structure
AMI lookup usesdata "aws_ami", notresource. Filters require a block withnameandvaluesas a list.Step 2: Check syntax for filters and owners
Filters usevalues = ["my-ami*"](list), andownersis a list of strings like["self"].Final Answer:
data "aws_ami" "example" { filter { name = "name" values = ["my-ami*"] } owners = ["self"] } -> Option BQuick Check:
Correct data source syntax = data "aws_ami" "example" { filter { name = "name" values = ["my-ami*"] } owners = ["self"] } [OK]
- Using 'resource' instead of 'data'
- Setting 'values' as string instead of list
- Swapping 'name' and 'owners' fields
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
owners = ["099720109477"]
}
output "ami_id" {
value = data.aws_ami.ubuntu.id
}What will the output
ami_id contain?Solution
Step 1: Understand the
Settingmost_recentflagmost_recent = truereturns the newest AMI matching the filter.Step 2: Analyze the filter and owner
The filter matches Ubuntu 20.04 AMIs by Canonical's owner ID, so the data source returns the latest matching AMI ID.Final Answer:
The ID of the most recent Ubuntu 20.04 AMI owned by Canonical -> Option DQuick Check:
most_recent true = latest AMI ID [OK]
- Thinking it returns a list of AMIs
- Confusing AMI name with ID
- Assuming most_recent is invalid
data "aws_ami" "example" {
filter {
name = "name"
values = "my-ami-*"
}
owners = ["self"]
}Terraform gives an error. What is the problem?
Solution
Step 1: Check the
Thevaluesfield typevaluesfield expects a list of strings, but here it is given as a single string.Step 2: Confirm correct syntax for
It should bevaluesvalues = ["my-ami-*"]to be valid.Final Answer:
Thevaluesfield must be a list, not a string -> Option CQuick Check:
values needs list brackets [OK]
- Using string instead of list for values
- Misunderstanding owners field format
- Adding unnecessary most_recent flag
Solution
Step 1: Verify correct use of data source for AMI lookup
data "aws_ami" "amazon_linux" { most_recent = true filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } owners = ["137112412989"] } resource "aws_instance" "web" { ami = data.aws_ami.amazon_linux.id instance_type = "t2.micro" } correctly usesdata "aws_ami"withmost_recent = true, proper filter withname = "name"and list forvalues, and correct owner ID.Step 2: Check instance resource references the data source ID
Theaws_instanceresource usesami = data.aws_ami.amazon_linux.id, which is correct to launch with the looked-up AMI.Final Answer:
data "aws_ami" "amazon_linux" { most_recent = true filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } owners = ["137112412989"] } resource "aws_instance" "web" { ami = data.aws_ami.amazon_linux.id instance_type = "t2.micro" } -> Option AQuick Check:
Data source + resource with correct references = data "aws_ami" "amazon_linux" { most_recent = true filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } owners = ["137112412989"] } resource "aws_instance" "web" { ami = data.aws_ami.amazon_linux.id instance_type = "t2.micro" } [OK]
- Using resource instead of data for AMI lookup
- Passing string instead of list in filter values
- Incorrect filter name or owner placement
Ready to prove it?
Pass the test and this pattern moves unknown
