LevelUpWith

Explore / HashiCorp Certified: Terraform Associate (004) Exam Prep / Practice questions

HashiCorp Certified: Terraform Associate (004) practice questions

10 questions from the bank behind this course's timed mock. Free to read and to answer, no account needed, and each has its answer and an explanation. They belong to HashiCorp Certified: Terraform Associate (004) Exam Prep.

Answer them one at a time →Read lesson 1 free

  1. Question 1

    A platform engineer is reviewing this configuration: resource "aws_s3_bucket" "logs" { bucket = "acme-app-logs" tags = { Environment = "dev" } } Another part of the same configuration needs to refer to this bucket. Which reference address is correct?

    1. aws_s3_bucket.acme-app-logs
    2. aws_s3_bucket.logs
    3. logs.aws_s3_bucket
    4. resource.logs
    Show the answer

    Answer: B. aws_s3_bucket.logs

    A resource is referred to as TYPE.NAME, using the local name "logs" that exists only inside the configuration. "acme-app-logs" is the bucket's real name in AWS, set by the bucket argument, and is not how the configuration refers to the block.

  2. Question 2

    A platform engineer maintains two things: a root module that deploys production, and a reusable networking module that several other teams call from their own configurations. Following HashiCorp's guidance, how should the AWS provider version be constrained in each?

    1. Root module: a `~>` constraint that sets both a lower and an upper bound. Reusable module: only a minimum version with `>=`.
    2. Both modules: an exact pin such as `= 5.94.1`, so every caller gets an identical provider.
    3. Root module: a `~>` constraint. Reusable module: `required_version = ">= 5.90.0"` instead of a provider constraint.
    4. Root module: only a minimum version with `>=`. Reusable module: a `~>` constraint that sets both a lower and an upper bound.
    Show the answer

    Answer: A. Root module: a `~>` constraint that sets both a lower and an upper bound. Reusable module: only a minimum version with `>=`.

    The root module should bound both ends with `~>` so a breaking major release is never pulled in, while a reusable module sets only a minimum so its callers keep their freedom to choose. The reverse arrangement leaves production open to a new major version and boxes in every caller; `required_version` constrains the Terraform CLI, not a provider.

  3. Question 3

    A platform engineer adds a pre-commit hook that runs terraform validate in an initialized directory. The hook reports "Success! The configuration is valid.", yet a later terraform plan fails because the provider rejects the configured credentials. What best explains this?

    1. terraform validate must be given a saved plan file with -out before it can check provider access
    2. terraform validate only checks canonical formatting, so any non-style error is left for terraform plan
    3. terraform validate checks only that the configuration is valid and consistent, and does not contact remote services such as provider APIs
    4. terraform validate used a cached copy of the state file, which no longer matched the real infrastructure
    Show the answer

    Answer: C. terraform validate checks only that the configuration is valid and consistent, and does not contact remote services such as provider APIs

    validate never calls out to remote state or provider APIs, so a configuration can pass it and still fail at plan or apply. Checking canonical formatting is the job of terraform fmt, not validate.

  4. Question 4

    On Monday an engineer runs `terraform plan -out=tfplan` and sends the plan for review. On Tuesday a teammate applies an unrelated change to the same workspace, which updates the state. On Wednesday the engineer runs `terraform apply tfplan`. What happens, and what should the engineer do?

    1. Terraform rejects the file with a `Saved plan is stale` error; the engineer should create a new plan and have it reviewed
    2. Terraform silently re-plans against the current state and applies the combined result without prompting
    3. Terraform applies Monday's actions and reverts the teammate's change so that state matches the saved plan
    4. Terraform shows the differences since Monday and asks the engineer to type `yes` to continue
    Show the answer

    Answer: A. Terraform rejects the file with a `Saved plan is stale` error; the engineer should create a new plan and have it reviewed

    A saved plan is only valid against the state it was made from, so once the state has changed Terraform refuses it as stale and the fix is a fresh plan. Terraform never re-plans in saved plan mode; it runs exactly what was saved or nothing at all.

  5. Question 5

    A platform engineer adds this block to look up a golden image built by a Packer pipeline: data "aws_ami" "web" { most_recent = true owners = ["self"] } In the instance resource they write ami = aws_ami.web.id. The configuration declares no other block of type aws_ami. What happens when they run terraform plan?

    1. Terraform reports a reference to an undeclared resource, because without the data. prefix it looks for a managed resource named aws_ami.web
    2. Terraform defers the lookup to apply and shows the ami argument as (known after apply) in the plan
    3. Terraform plans to create a new AMI named web, because a reference without a prefix implicitly declares a managed resource
    4. Terraform resolves the reference to the data block automatically, because no managed resource shares that type and name
    Show the answer

    Answer: A. Terraform reports a reference to an undeclared resource, because without the data. prefix it looks for a managed resource named aws_ami.web

    A reference with no prefix always means a managed resource, so aws_ami.web points at a resource block that does not exist and Terraform raises an undeclared resource error. Terraform never falls back to a data block of the same type and name; the data. prefix is what puts the two in separate namespaces.

  6. Question 6

    A platform engineer keeps organisation-wide tags in local.default_tags ({ Owner = "platform", Env = "dev" }) and wants each resource to add its own tags from var.resource_tags ({ Env = "prod", Name = "web" }). Where the same key appears in both maps, the resource-specific value must win. Which expression produces the correct tags map?

    1. merge(var.resource_tags, local.default_tags)
    2. concat(local.default_tags, var.resource_tags)
    3. merge(local.default_tags, var.resource_tags)
    4. lookup(local.default_tags, var.resource_tags, "prod")
    Show the answer

    Answer: C. merge(local.default_tags, var.resource_tags)

    merge lets the later argument win on duplicate keys, so putting var.resource_tags last gives Env = "prod" while keeping Owner and Name. Reversing the order would let the default Env = "dev" overwrite the resource-specific value.

  7. Question 7

    A platform engineer keeps several modules in one Git repository, `https://example.com/infra.git`. The DNS module lives in the `modules/dns` directory of that repository, and the team wants to consume the code tagged `v1.2.0`. Which `source` value installs only that module at that tag?

    1. git::https://example.com/infra.git/modules/dns?ref=v1.2.0
    2. git::https://example.com/infra.git?ref=v1.2.0&path=modules/dns
    3. git::https://example.com/infra.git//modules/dns with a separate `version = "1.2.0"` argument
    4. git::https://example.com/infra.git//modules/dns?ref=v1.2.0
    Show the answer

    Answer: D. git::https://example.com/infra.git//modules/dns?ref=v1.2.0

    A double slash separates the package (the repository) from the subdirectory inside it, and `?ref=` pins the Git revision. A single slash makes the path part of the repository URL, and `version` is only valid for registry sources.

  8. Question 8

    An engineer's working directory already has a registry module installed at 5.1.0 under the constraint version = "~> 5.1.0". The module author then publishes 5.1.4. Without changing any configuration, the engineer runs plain terraform init again and then terraform plan. Which module version does the plan use?

    1. 5.1.4, because terraform plan refreshes module sources before it calculates changes
    2. 5.1.4, because terraform init always selects the newest release that satisfies the constraint
    3. 5.1.0, because plain terraform init does not touch modules that are already installed
    4. 5.1.0, because .terraform.lock.hcl recorded that version when the module was first installed
    Show the answer

    Answer: C. 5.1.0, because plain terraform init does not touch modules that are already installed

    Plain terraform init only installs modules added since the last init and leaves installed ones alone, so 5.1.0 stays until you run terraform init -upgrade. The lock file answer names the right version for the wrong reason: .terraform.lock.hcl tracks only providers, never module versions.

  9. Question 9

    A platform engineer is refactoring a root configuration and has cut the block `resource "aws_instance" "frontend"` out of the root module and pasted it, unchanged, into `./modules/compute`, which is called as `module "compute"`. The running instance must not be replaced. Which block should be added to the root module?

    1. moved { from = aws_instance.frontend to = module.compute.aws_instance.frontend }
    2. removed { from = aws_instance.frontend lifecycle { destroy = false } }
    3. moved { from = "aws_instance.frontend" to = "module.compute.aws_instance.frontend" }
    4. moved { from = module.compute.aws_instance.frontend to = aws_instance.frontend }
    Show the answer

    Answer: A. moved { from = aws_instance.frontend to = module.compute.aws_instance.frontend }

    `from` is the previous address held in state and `to` is the new address in configuration, both written as unquoted addresses, so the existing object is re-labelled under the module. The quoted version is the nearest miss: the direction is right, but `from` and `to` take addresses, not strings.

  10. Question 10

    A platform team's Jenkins pipeline must package each Terraform configuration itself and start runs in HCP Terraform programmatically. Security policy says the workspace must not be connected to any Git repository. Which approach fits this requirement?

    1. Create a CLI workspace with `terraform workspace new ci` and let Jenkins select it before each run
    2. Switch the workspace to local execution mode, so that Jenkins runs Terraform itself and HCP Terraform starts the runs
    3. Use the API-driven workflow: the pipeline uploads the configuration as a .tar.gz configuration version and creates runs through the API
    4. Use the UI/VCS-driven workflow, so that webhooks queue a run whenever commits are merged to the tracked branch
    Show the answer

    Answer: C. Use the API-driven workflow: the pipeline uploads the configuration as a .tar.gz configuration version and creates runs through the API

    The API-driven workflow is built for custom tooling: it uploads a configuration version and creates runs on a workspace that has no VCS link. The UI/VCS-driven workflow is the nearest alternative, but it requires exactly the repository connection the policy forbids.

The full timed mock

57 questions in 60 minutes, with a pass mark of 70%. Each paper is drawn from a bank of questions written for the mock alone, and these 10 are left out of it. Full access opens it, with every level of the course.

See the course →See the plans

LevelUpWith is independent of the organisation that sets this exam. These questions were written for this course; they are not taken from the exam.