An AMI (Amazon Machine Image) is a template that contains the software configuration (operating system, application server, and applications) required to launch an EC2 instance.
Click to reveal answer
beginner
What does the Terraform 'data' block do when looking up an AMI?
The 'data' block in Terraform fetches information from existing resources, like finding the latest AMI ID based on filters, without creating new resources.
Click to reveal answer
intermediate
Why use filters in an AMI lookup data source?
Filters help narrow down the search to find the exact AMI you want, such as by name, owner, or tags, ensuring you get the right image for your EC2 instance.
Click to reveal answer
intermediate
Example: What does the attribute 'most_recent = true' do in an AMI lookup?
It tells Terraform to select the newest AMI that matches the filters, so you always get the latest version available.
Click to reveal answer
beginner
How do you reference the AMI ID found by the data source in Terraform?
You use the syntax: data.<data_source_name>.<resource_name>.id to get the AMI ID for use in other resources like EC2 instances.
Click to reveal answer
In Terraform, which block is used to look up an existing AMI?
Aprovider "aws"
Bresource "aws_ami"
Cdata "aws_ami"
Dvariable "aws_ami"
✗ Incorrect
The 'data' block is used to fetch existing information like AMI details without creating new resources.
What does setting 'owners = ["amazon"]' do in an AMI lookup?
ACreates a new AMI owned by Amazon
BLists all AMIs regardless of owner
CDeletes AMIs not owned by Amazon
DFilters AMIs owned by Amazon
✗ Incorrect
It filters the AMIs to only those owned by Amazon.
Which attribute ensures Terraform picks the latest AMI matching filters?
Alatest = true
Bmost_recent = true
Cnewest = true
Drecent = true
✗ Incorrect
'most_recent = true' tells Terraform to select the newest AMI.
How do you use the AMI ID found by the data source in an EC2 resource?
Aami = data.aws_ami.example.id
Bami = aws_ami.example.id
Cami = var.aws_ami_id
Dami = resource.aws_ami.example.id
✗ Incorrect
You reference the AMI ID using the data source name and resource name.
What is the purpose of the 'filter' block inside the AMI data source?
ATo specify criteria like name or tags to find the right AMI
BTo create a new AMI with specific tags
CTo delete AMIs that don't match criteria
DTo launch an EC2 instance
✗ Incorrect
Filters help find the AMI that matches specific criteria.
Explain how to use Terraform to find the latest Amazon Linux 2 AMI using a data source.
Think about filters, owner, and most_recent attributes.
You got /5 concepts.
Describe why using an AMI lookup data source is better than hardcoding an AMI ID in Terraform.
Consider what happens when AMIs get updated.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of using the aws_ami data source in Terraform?
easy
A. To find the latest Amazon Machine Image (AMI) based on filters
B. To create a new AMI from an existing instance
C. To delete unused AMIs automatically
D. To launch an EC2 instance directly
Solution
Step 1: Understand the role of aws_ami data source
The aws_ami data 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 A
Quick Check:
AMI lookup = find latest AMI [OK]
Hint: Remember: data source reads info, does not create resources [OK]
Common Mistakes:
Confusing data source with resource creation
Thinking it deletes AMIs
Assuming it launches instances directly
2. Which of the following is the correct syntax to lookup an AMI by name prefix and owner in Terraform?
easy
A. resource "aws_ami" "example" { filter { name = "name" values = ["my-ami*"] } owners = ["self"] }
B. data "aws_ami" "example" { filter { name = "name" values = ["my-ami*"] } owners = ["self"] }
C. data "aws_ami" "example" { filter { name = "owner" values = ["self"] } owners = ["my-ami*"] }
D. data "aws_ami" "example" { filter { name = "name" values = "my-ami*" } owners = "self" }
Solution
Step 1: Identify correct resource type and block structure
AMI lookup uses data "aws_ami", not resource. Filters require a block with name and values as a list.
Step 2: Check syntax for filters and owners
Filters use values = ["my-ami*"] (list), and owners is a list of strings like ["self"].
Final Answer:
data "aws_ami" "example" { filter { name = "name" values = ["my-ami*"] } owners = ["self"] } -> Option B
Quick Check:
Correct data source syntax = data "aws_ami" "example" { filter { name = "name" values = ["my-ami*"] } owners = ["self"] } [OK]
Hint: Data source uses 'data', filters need list values [OK]
Common Mistakes:
Using 'resource' instead of 'data'
Setting 'values' as string instead of list
Swapping 'name' and 'owners' fields
3. Given this Terraform snippet:
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?
medium
A. An error because most_recent is not valid here
B. A list of all Ubuntu AMI IDs owned by the user
C. The name of the AMI instead of the ID
D. The ID of the most recent Ubuntu 20.04 AMI owned by Canonical
Solution
Step 1: Understand the most_recent flag
Setting most_recent = true returns 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 D
Quick Check:
most_recent true = latest AMI ID [OK]
Hint: most_recent true returns single newest AMI ID [OK]
Common Mistakes:
Thinking it returns a list of AMIs
Confusing AMI name with ID
Assuming most_recent is invalid
4. You wrote this Terraform data source to find an AMI:
data "aws_ami" "example" {
filter {
name = "name"
values = "my-ami-*"
}
owners = ["self"]
}
Terraform gives an error. What is the problem?
medium
A. The owners field cannot be a list
B. The filter block is missing the most_recent flag
C. The values field must be a list, not a string
D. The name field should be owner
Solution
Step 1: Check the values field type
The values field expects a list of strings, but here it is given as a single string.
Step 2: Confirm correct syntax for values
It should be values = ["my-ami-*"] to be valid.
Final Answer:
The values field must be a list, not a string -> Option C
Quick Check:
values needs list brackets [OK]
Hint: Always use square brackets for filter values [OK]
Common Mistakes:
Using string instead of list for values
Misunderstanding owners field format
Adding unnecessary most_recent flag
5. You want to launch an EC2 instance using the latest Amazon Linux 2 AMI in your Terraform code. Which snippet correctly combines AMI lookup and instance resource?
hard
A. 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"
}
B. resource "aws_ami" "amazon_linux" {
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
owners = ["137112412989"]
}
resource "aws_instance" "web" {
ami = aws_ami.amazon_linux.id
instance_type = "t2.micro"
}
C. data "aws_ami" "amazon_linux" {
filter {
name = "owner"
values = ["137112412989"]
}
most_recent = true
}
resource "aws_instance" "web" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t2.micro"
}
D. 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"
}
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 uses data "aws_ami" with most_recent = true, proper filter with name = "name" and list for values, and correct owner ID.
Step 2: Check instance resource references the data source ID
The aws_instance resource uses ami = 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 A
Quick 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]
Hint: Use data source ID in instance AMI field [OK]
Common Mistakes:
Using resource instead of data for AMI lookup
Passing string instead of list in filter values
Incorrect filter name or owner placement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.