HashiCorp certification · Exam Terraform-Associate-003

Terraform-Associate-003 Mock Exam

HashiCorp Certified: Terraform Associate (003) (HCTA0-003)

Find out if you're ready for Terraform-Associate-003. Take the free timed mock exam and get your score in minutes, or study the free Terraform-Associate-003 questions below with answers and explanations.

20 questions · 33 minutes · free, no sign-up

Free Terraform-Associate-003 questions

1–10 of 58 free
Question 1 Choose 1
How would you output returned values from a child module in the Terraform CLI output?
Show answerHide answer

Correct answer: C Declare the output in both the root and child module

Explanation

Explanation:
Terraform modules encapsulate their details. Just because a value exists within a module does not mean it is automatically available to the root module or the CLI. You must explicitly expose it.

Here is the required two-step process:
Step 1: Declare the output in the child module (Option B is part of the solution) Inside the child module's configuration (e.g., in a file like modules/network/outputs.tf), you must define an output block for any value you wish to expose.
Example (modules/network/outputs.tf):
hcl
# This makes the VPC ID available to the calling module.
output "vpc_id" {
value = aws_vpc.main.id
description = "The ID of the created VPC"
}
Purpose:
This makes the value available at the boundary of the child module. Without this step, the value is completely hidden and inaccessible from the outside.
Step 2: Declare the output in the root configuration (Option A is part of the solution) In your root module (e.g., in main.tf or outputs.tf), you first call the child module using a module block. Then, to display the value in the CLI, you must create a new output block in the root that references the output you exposed in Step 1.
Example (root module's outputs.tf):
hcl
# This captures the output from the child module and makes it a root-level output.
output "child_vpc_id" {
value = module.network.vpc_id # References the child module's output
description = "The VPC ID output from the network child module"
}
Only after completing both steps will the value be displayed in the Terraform CLI after running terraform apply or terraform output.

Why the Other Options Are Incorrect
A. Declare the output in the root configuration
Incorrect (on its own). You cannot declare an output in the root for a value that has not been exposed by the child module. The root module has no inherent visibility into the child module's internal resources. The syntax module.NAME.OUTPUT would fail because the OUTPUT would not be defined.
B. Declare the output in the child module
Incorrect (on its own). Declaring an output in the child module only makes it available for the calling module (the root) to use. It does not automatically print the value to the CLI. The value must be "passed through" by being referenced in a root-level output block to be visible in the command line.
D. None of the above
Incorrect. This is false because the correct and required method is a combination of options A and B, which is explicitly provided as option C.

References
This question tests a fundamental concept of module composition in Terraform, which is covered in the HashiCorp Terraform Associate 003 exam objectives under the domain:
4.0 Read, generate, and modify configuration
4.3 Use collections and structural types in configuration.
(Implicitly) Understand module outputs.
Question 2 Choose 1
Which command must you first run before performing further Terraform operations in a working directory?
Show answerHide answer

Correct answer: D terraform init

Explanation

Explanation:
Before you can use any other Terraform commands (plan, apply, destroy, etc.), you must initialize your working directory with:
terraform init
This command sets up everything Terraform needs to work properly in that specific directory.

⚙️ What terraform init Does:
1.Initializes the working directory
Creates the necessary local files and directories (like .terraform/).
2.Downloads provider plugins
Terraform reads your configuration files (.tf) to determine which providers (e.g., AWS, Azure, Google Cloud) are required, then downloads them automatically.
3.Configures the backend (if defined)
If you’re using a remote backend (like S3, GCS, or Terraform Cloud), terraform init sets it up and verifies connectivity.
4.Prepares for further operations
After successful initialization, you can safely run:
terraform plan
terraform apply terraform destroy
etc.

💡 Typical Terraform Workflow:
1️⃣ terraform init
Initialize and set up the working directory
2️⃣ terraform plan
Preview what Terraform will change
3️⃣ terraform apply
Apply changes to create/update infrastructure
4️⃣ terraform destroy
Tear down managed infrastructure (if needed)
You must run terraform init first — otherwise, Terraform will show an error like:
“Error: Terraform has not been initialized. Run ‘terraform init’ to initialize the working directory.”

❌ Other Options (Incorrect):
A. terraform import
Used to bring existing resources into Terraform state, but only after initialization.
B. terraform workspace
Manages multiple workspaces (environments), but you must initialize before using it.
C. terraform plan
Generates an execution plan, but fails if the directory is not initialized.
D. terraform init
✅ Correct. Always the first command to run.

📚 Reference:
Terraform Docs – init Command:
“The terraform init command is used to initialize a working directory containing Terraform configuration files.”

🧩 Summary:
Which command must you first run before performing further Terraform operations?
Question 3 Choose 1
What is the Terraform style convention for indenting a nesting level compared to the one above it?
Show answerHide answer

Correct answer: B With two spaces

Explanation

Explanation:
Terraform’s official style convention recommends using two spaces for each level of indentation. This applies to all nested blocks such as resource, module, variable, output, and provider.

🔹 Why B is correct:
Two-space indentation is the standard enforced by terraform fmt, the built-in Terraform formatter.
It ensures consistency, readability, and alignment with HashiCorp’s style guide.
Any deviation (e.g., tabs or more spaces) will be reformatted automatically by terraform fmt.
📘 Reference:
“Indent two spaces for each nesting level.” — Terraform Style Conventions

❌ Why other options are incorrect:
A. With a tab — Incorrect
Tabs are not part of Terraform’s formatting standard.
Using tabs will be replaced with two spaces when terraform fmt is run.
Tabs can cause inconsistent rendering across editors and platforms.
📘 Reference:
“Use spaces rather than tabs for indentation.” — Terraform Style Guide
C. With four spaces — Incorrect
Four spaces is not the standard and will be reduced to two by terraform fmt.
It adds unnecessary width and breaks consistency with Terraform’s formatting rules.
D. With three spaces — Incorrect
Three spaces is non-standard and inconsistent.
Terraform will reformat it to two spaces automatically.

🧠 Operational insight:
terraform fmt is the authoritative tool for enforcing style.
Teams using Git hooks or CI pipelines often run terraform fmt -check to enforce two-space indentation.
This convention improves readability and reduces merge conflicts in collaborative environments.

✅ Summary:
B is correct:
Two spaces per nesting level is the official and enforced standard.
A, C, D are incorrect:
They deviate from the style guide and will be auto-corrected by terraform fmt.
Question 4 Choose 1
When does Terraform create the .terraform.lock.hc1 file?
Show answerHide answer

Correct answer: C After your first terraform init

Explanation

Explanation:
The .terraform.lock.hc1 file is the Terraform dependency lock file. Its purpose is to record the exact versions of providers that were selected during the terraform init process, thereby locking those dependencies for future operations.

The Role of terraform init
The terraform init command is the first command that must be run in a new Terraform configuration. Its primary jobs are:
Initializing the backend.
Downloading and installing the necessary provider plugins.

Creating the dependency lock file.
Here is the sequence of events:
You run terraform init.
Terraform reads your configuration and identifies the required providers (e.g., aws, azurerm, null).
It consults the Terraform Registry to find a provider version that matches the version constraints in your configuration.
It downloads the selected provider plugin(s) into the .terraform directory.
It writes the exact selected version for each provider into the .terraform.lock.hc1 file.

Purpose of the Lock File
The lock file ensures consistent environments across your team and CI/CD pipelines. When another user (or your CI system) runs terraform init in the same directory, it will read the lock file and install the exact same provider versions that are recorded there, even if a newer, compatible version has since been released.
This prevents subtle bugs and drifts in infrastructure management that can occur if different team members are inadvertently using different minor or patch versions of a provider.

Why the Other Options Are Incorrect
A. After your first terraform plan
Incorrect. The terraform plan command requires the providers to already be installed. It cannot generate a plan without them. By the time you run plan, the init command has already been run, and the lock file has already been created.
B. After your first terraform apply
Incorrect. Similar to plan, the apply command is a subsequent step that relies on the work done by init. The lock file is created to lock dependencies before any changes are planned or applied to your infrastructure.
D. When you enable state locking
Incorrect. This is a confusion of two distinct concepts. State Locking is a mechanism to prevent concurrent operations on the same state file (e.g., using a DynamoDB table with an S3 backend). It is configured in the backend and operates during apply or destroy.
The Dependency Lock File (.terraform.lock.hc1) is about pinning provider versions and is created and managed by the init command. It has nothing to do with state locking.

References
This question tests knowledge of Terraform's core workflow and dependency management, which is covered in the HashiCorp Terraform Associate 003 exam objectives under the domain:
2.0 Manage Terraform Lifecycle
2.1 Given a scenario: choose when to use terraform init.
Question 5 Choose 1
Which method for sharing Terraform modules fulfills the following criteria:
Keeps the module configurations confidential within your organization.
Supports Terraform's semantic version constraints.
Provides a browsable directory of your modules.
Show answerHide answer

Correct answer: D HCP Terraform/Terraform Cloud private registry

Explanation

Explanation:
The Terraform Cloud private registry is the only method that satisfies all three criteria:

1.Keeps modules confidential —
✅ Modules are scoped to your organization and not publicly accessible.
2.Supports semantic versioning —
✅ You can publish modules with version tags (e.g., v1.0.0, v2.1.3) and use version constraints like ~> 1.0.
3.Provides a browsable directory —
✅ Terraform Cloud offers a web UI where users can browse available modules, view documentation, and usage examples.

📘 Reference:
“HCP Terraform's private registry works similarly to the public Terraform Registry and helps you share Terraform providers and modules across your organization. It includes support for versioning and a searchable list of available providers and modules.” — Terraform Cloud Private Registry Overview

❌ Why other options are incorrect:
A. Git repository containing your modules—
❌ Git can store modules privately, but:
It lacks semantic version enforcement unless manually tagged.
No browsable UI or searchable module directory.
Requires custom tooling or scripts to manage versions and discovery.
B. Public Terraform module registry —
❌ Public registry is not confidential.
Anyone can browse and use your modules.
Suitable for open-source sharing, not internal enterprise use.
C. A subfolder within your workspace —
❌ This is a local organizational method.
No versioning support.
No browsable directory or reuse across teams.

✅ Summary:
D is correct:
It’s the only method that meets all three criteria: confidentiality, semantic versioning, and browsable UI.
A, B, C are incorrect:
Each fails at least one requirement.
Question 6 Choose 1
What is one disadvantage of using dynamic blocks in Terraform?
Show answerHide answer

Correct answer: D They make configuration harder to read and understand

Explanation

Explanation:
Dynamic blocks in Terraform are powerful for generating repeatable nested blocks using iteration, but they come with a key disadvantage: they reduce readability and clarity, especially for teams unfamiliar with the syntax or logic.

🔹 Why D is correct:
Dynamic blocks use for_each and content constructs, which can obscure the structure of the configuration.
They make it harder to visually parse what the block is doing compared to static, literal blocks.
This can lead to confusion during code reviews or onboarding.

📘 Reference:
“Dynamic blocks can make configuration harder to understand, especially for those unfamiliar with the syntax.” — Terraform Dynamic Blocks | HashiCorp Docs

❌ Why other options are incorrect:
A. Dynamic blocks can construct repeatable nested blocks —
❌ Incorrect This is a benefit, not a disadvantage. Dynamic blocks are designed specifically for this purpose.
B. Terraform will run more slowly —
❌ Incorrect There is no performance penalty associated with dynamic blocks. Terraform compiles them into the same plan structure as static blocks.
C. They cannot be used to loop through a list of values —
❌ Incorrect Dynamic blocks do support looping via for_each, making them ideal for iterating over lists or maps.

📘 Reference:
“The dynamic block construct supports for_each to iterate over complex structures.” — Terraform Dynamic Blocks | HashiCorp Docs
Question 7 Choose 1
What is the provider for the resource shown in the Exhibit?
resource "aws_vpc" "main" {
name = "test"
}
Show answerHide answer

Correct answer: D aws

Explanation

Explanation:
The question is asking for the provider of the resource block shown. In Terraform, a provider is a plugin that enables interaction with an API, such as the APIs for AWS, Azure, Google Cloud, etc.

Let's analyze the resource block structure:
hcl
resource "aws_vpc" "main" {
name = "test"
}

A resource block in Terraform has the following general syntax:
resource "" "" { ... }
resource: This is the keyword that declares a resource block.
"": This is a string that specifies the type of resource. The first part of this string (before the underscore _) almost always identifies the provider.
"": This is a unique identifier you give to the resource within your Terraform configuration. It is used to reference this resource elsewhere in the same module.

Applying this to the given block:
Resource Type: aws_vpc
The part before the underscore is aws. This explicitly tells Terraform that this resource is managed by the aws provider.
Local Name: main
This is the name you would use to reference this VPC within your configuration (e.g., aws_vpc.main.id).

Why the other options are incorrect:
A. VPC:
This is the specific resource type within the AWS provider, not the provider itself. The full resource type is aws_vpc.
B. test:
This is the value assigned to a tag argument (likely name = "test"), which is simply a configuration setting for the VPC resource. It has no relation to the provider.
C. main:
This is the local name of the resource, used for internal referencing within the Terraform configuration. It is not the provider.

Reference:
HashiCorp Documentation: Resource Blocks
"A resource block declares a resource of a given type ("aws_instance", "aws_iam_user", etc.) with a given local name ("web", "user"). The name is used to refer to this resource from elsewhere in the same Terraform module, but has no significance outside of the scope of a module." HashiCorp Documentation: Providers
"Providers are a logical abstraction of an upstream API. They are responsible for understanding API interactions and exposing resources." The resource type prefix (e.g., aws_) directly maps to the required provider.
Question 8 Choose 1
Running terraform fmt without any flags in a directory with Terraform configuration files check the formatting of those files without changing their contents.
Show answerHide answer

Correct answer: B False

Explanation

Explanation:
The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. The key to this question is understanding the behavior of the command when used without any flags.
By default, terraform fmt does not just check the formatting; it actually rewrites the files in-place to correct the formatting. It will modify the contents of your .tf and .tfvars files to adhere to the standard style conventions.
If you only want to check if the files are properly formatted without making changes, you must use the -check flag.
For example:
terraform fmt -check will return a non-zero exit code if any files need formatting, but will not change them.
terraform fmt (without flags) will automatically format and overwrite the files.
Therefore, the statement in the question is incorrect because running the command without flags will change the file contents to fix the formatting.

In summary:
terraform fmt = Formats and overwrites files.
terraform fmt -check = Only checks formatting, does not change files.

Reference:
HashiCorp Documentation: Command: fmt
"The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability."
"By default, fmt scans the current directory for configuration files. If you provide a directory for the dir argument, then fmt will scan that directory instead. If you provide a single file as the argument, then fmt will process just that file. If you give the argument - then fmt will read from standard input (STDIN)."
Question 9 Choose 1
What value does the Terraform Cloud private registry provide over the public Terraform Module Registry?
Show answerHide answer

Correct answer: B The ability to restrict modules to members of Terraform Cloud or Enterprise organizations

Explanation

Explanation:
The Terraform Cloud (now HCP Terraform) private registry enables organizations to host private Terraform modules and providers, restricting access exclusively to members of the specific Terraform Cloud or Enterprise organization. This access control is the primary value over the public Terraform Registry, which is openly accessible to all Terraform users at registry.terraform.io. The private registry ensures that proprietary or sensitive infrastructure configurations remain secure, supporting enterprise needs for governance, compliance, and collaboration within a controlled environment. Option B correctly identifies this restricted access as the key advantage.
The public Terraform Registry allows anyone to publish and use modules, fostering open-source sharing but lacking mechanisms to limit access. In contrast, the private registry, integrated into Terraform Cloud or Enterprise, creates a secure, organization-specific catalog where only authenticated members can view, use, or manage modules. This is essential for enterprises protecting sensitive infrastructure code, such as custom AWS resource configurations or compliance-driven setups. For example, a private module for deploying a secure VPC can be restricted to the organization’s DevOps team, preventing external exposure.
The private registry also integrates with Terraform Cloud’s features, like role-based access control (RBAC) and workspace management, enhancing security and ensuring only authorized users interact with modules. This supports consistent, reusable configurations across teams while maintaining strict access boundaries. Unlike the public registry, which prioritizes community access, the private registry’s restricted scope is its defining benefit, making it ideal for internal workflows and sensitive deployments.

Why other options are incorrect:
A. The ability to share modules publicly with any user of Terraform:
This is incorrect because the private registry is designed to restrict access, not enable public sharing. Public sharing is a feature of the public Terraform Registry, which allows global access without authentication.
C. The ability to tag modules by version or release:
This is incorrect because versioning is a standard feature in both the public and private registries. Modules in either registry can be tagged (e.g., 1.2.3) for consistent referencing, so this is not a unique advantage of the private registry.
D. The ability to share modules with public Terraform users and members of Terraform Cloud Organizations:
This is incorrect because the private registry does not support sharing with public Terraform users. It is exclusively for organization members, unlike the public registry’s open-access model.
By centralizing module management, the private registry reduces duplication and ensures compliance with organizational standards. For instance, a company can maintain a private module for standardized Kubernetes clusters, accessible only to its engineers. This controlled access distinguishes the private registry from the public one, which lacks such restrictions.

References:
HCP Terraform Private Registry Overview
Using Providers and Modules from the HCP Terraform Private Registry
Terraform Registry Documentation
Question 10 Choose 1
Which configuration consistency errors does terraform validate report?
Show answerHide answer

Correct answer: C Declaring a resource identifier more than once

Explanation

Explanation:
The terraform validate command is designed to check the syntactic correctness and internal consistency of your Terraform configuration files within a module. It validates the configuration's structure and basic logic before any plans or applies are run.

Let's evaluate each option:
A. Terraform module isn't the latest version:
Incorrect. terraform validate does not check for module version updates. This is the role of terraform init -upgrade or manual review of your source code and the Terraform Registry.
B. Differences between local and remote state:
Incorrect. terraform validate does not interact with the state file at all. It works solely on the configuration files. Detecting state differences is the primary function of terraform plan.
C. Declaring a resource identifier more than once: Correct.
This is a classic example of a configuration consistency error. If you declare two different resources with the same type and local name (e.g., two resource "aws_instance" "web" blocks), terraform validate will catch this duplicate declaration and report an error.
D. A mix of spaces and tabs in configuration files:
Incorrect. While a mix of spaces and tabs is bad practice, it is not a syntactic error. Terraform's parser is generally forgiving of style. The terraform fmt command is specifically designed to fix these kinds of style inconsistencies, but validate will not fail because of them.

In essence, terraform validate checks for errors that make the configuration un-parseable or logically invalid according to Terraform's own rules, such as:
Invalid HCL syntax (unclosed blocks, missing commas).
Reference to an undeclared variable or resource.
Incorrect number of arguments for a resource.
Duplicate resource or variable declarations.
An invalid expression.

Reference:
HashiCorp Documentation: Command: validate
"The terraform validate command validates the configuration files in a directory, referring only to the configuration and not to any remote services such as remote state, provider APIs, etc."
Full mock exam

Sit the Terraform-Associate-003 exam before exam day

Full-length mocks with the same number of questions and the same time limit as the real Terraform-Associate-003 exam. Every attempt draws a new set of questions from the full bank, so you can keep going until you pass comfortably.

Get full Terraform-Associate-003 mock exam
  • 65questions per mock
  • 105 mintime limit
  • 194questions in the bank
  • Newmock on every attempt