Courseiva
Understand Terraform's purposehardMultiple ChoiceObjective-mapped

TF-004 Data Source Practice Question

Exhibit

resource "aws_instance" "web" {
  ami = data.aws_ami.ubuntu.id
  instance_type = "t2.micro"
}

data "aws_ami" "ubuntu" {
  most_recent = true
  owners = ["099720109477"]

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }
}

Refer to the exhibit. What is the purpose of the data source?

⚠ Common exam trap

A common trap in the Terraform exam is confusing data sources with resources: data sources are read-only and fetch existing information, while resources provision new infrastructure. Here, the `aws_ami` data source fetches an existing AMI ID, not creating a new one.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

To fetch an existing AMI ID

In Terraform, a data source allows you to fetch or compute information from external sources that is not defined within the current configuration. The correct answer is B because the data source in the exhibit (e.g., `aws_ami`) is used to query and retrieve the ID of an existing AMI that matches specified filters, such as name, owner, or tag, without creating a new AMI. This enables dynamic referencing of pre-existing resources in your infrastructure.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • To create a new AMI

    Why it's wrong here

    The `data` block in Terraform is explicitly designed to query and retrieve information about existing infrastructure or external services, not to provision new resources. Creating a new Amazon Machine Image (AMI) would require a `resource` block, such as `aws_ami` or `aws_ami_from_instance`, which defines the desired state for a new infrastructure component. Therefore, using a data source to create an AMI is fundamentally incorrect, as data sources are read-only operations on existing entities.

  • To fetch an existing AMI ID

    Why this is correct

    The primary purpose of this `data "aws_ami"` block is to dynamically query the AWS API and retrieve the unique identifier (ID) of an existing Amazon Machine Image that matches the specified criteria. By filtering for `ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*`, `architecture = "x86_64"`, and `virtualization_type = "hvm"`, and then selecting the latest image, the data source efficiently fetches the most current AMI ID for Ubuntu 20.04, making it available for use in other resource configurations.

  • To define a variable

    Why it's wrong here

    A data source serves a distinct function from a variable definition in Terraform. Variables (`variable` blocks) are used to declare input parameters for a module or configuration, allowing users to provide values at apply time, making the configuration reusable and flexible. In contrast, a `data` block, like `data "aws_ami"`, is used to fetch dynamic information from a cloud provider or external service during the plan phase, making that retrieved data accessible within the configuration for resource definitions or other logic.

  • To output the AMI name

    Why it's wrong here

    While the attributes retrieved by a data source, including the AMI name, can certainly be referenced within an `output` block to display information to the user after Terraform applies changes, the data source itself is not an output. The core function of the `data "aws_ami"` block is to perform the lookup and retrieval of the AMI's details from AWS. An `output` block is a separate construct whose sole purpose is to expose specific values from the Terraform state or configuration, making them visible or usable by other systems.

About these practice questions

This TF-004 question is part of Courseiva's 428-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.