Courseiva
Implement and maintain statemediumMultiple ChoiceObjective-mapped

TF-004 Implement and maintain state Practice Question

A team manages infrastructure with Terraform and uses a remote backend in an S3 bucket. After a recent state migration, a developer runs 'terraform plan' and gets an error: 'Error: Error loading state: NoSuchKey: The specified key does not exist.' The developer confirms that the state file exists in the bucket. What is the most likely cause?

⚠ Common exam trap

HashiCorp often tests the distinction between access denied errors (403) and missing key errors (404), so the trap here is that candidates might confuse a permission issue with a path mismatch, especially when the state file is confirmed to exist in the bucket.

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

The backend configuration in the Terraform code does not match the actual state file path.

The error 'NoSuchKey: The specified key does not exist' indicates that Terraform is looking for the state file at a specific key (path) in the S3 bucket, but that key does not exist. Since the developer confirms the state file exists in the bucket, the most likely cause is a mismatch between the backend configuration in the Terraform code (e.g., the `key` argument) and the actual path where the state file is stored. This often happens after a state migration if the backend configuration was not updated to reflect the new location.

Answer analysis

Option-by-option breakdown

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

  • The backend configuration in the Terraform code does not match the actual state file path.

    Why this is correct

    This is the correct answer. A "NoSuchKey" error from an S3 backend explicitly indicates that Terraform attempted to retrieve a state file at a specific S3 key (path) but found no object there. This frequently occurs when the 'key' parameter in the 'backend "s3"' configuration, or the active Terraform workspace, does not precisely align with the actual location of the state file in the S3 bucket, causing Terraform to look for a non-existent object.

  • Terraform state locking is enabled but the lock was not released.

    Why it's wrong here

    This option is incorrect. When Terraform's state locking mechanism, typically implemented via DynamoDB for S3 backends, encounters an existing lock, it will explicitly report an "Error acquiring state lock" or a similar message indicating a lock conflict. A "NoSuchKey" error is fundamentally unrelated to state locking; it signifies that the state file itself could not be found at the specified path, not that access to it was blocked by a lock.

  • The S3 bucket policy does not allow reading the state file.

    Why it's wrong here

    This option is incorrect. If an S3 bucket policy or associated IAM permissions prevented Terraform from reading the state file, the resulting error would typically be "AccessDenied" or "Forbidden," clearly indicating a permissions issue. A "NoSuchKey" error specifically means the object was not found at the requested path, implying that the request itself was valid from a permissions standpoint, but the target resource simply wasn't present.

  • S3 bucket versioning is enabled, and the state file is a non-current version.

    Why it's wrong here

    This option is incorrect. When S3 bucket versioning is enabled, Terraform, by default, always attempts to read the *latest* version of the state file object at the specified key. A "NoSuchKey" error indicates that *no* object exists at the specified key whatsoever, regardless of versioning. If a previous version existed but the latest was deleted, Terraform would still report "NoSuchKey" because the current version is absent, not that it's trying to access an old version.

Quick reference

AWS S3 Storage Class Comparison

Storage ClassMin DurationRetrievalUse Case
S3 StandardNoneImmediateFrequently accessed data
S3 Standard-IA30 daysImmediateInfrequent access, rapid retrieval
S3 One Zone-IA30 daysImmediateNon-critical infrequent data
S3 Intelligent-TieringNoneImmediate–hoursUnknown or changing access patterns
S3 Glacier Instant90 daysMillisecondsArchive with instant retrieval
S3 Glacier Flexible90 daysMinutes–hoursArchive, flexible retrieval
S3 Glacier Deep Archive180 daysHoursLong-term compliance archive

About these practice questions

One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.