Bird
Raised Fist0
Terraformcloud~10 mins

State disaster recovery in Terraform - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the backend type for Terraform state storage.

Terraform
terraform {
  backend "[1]" {}
}
Drag options to blanks, or click blank then click option'
As3
Blocal
Chttp
Dgcs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'local' backend which stores state on the local machine, not suitable for disaster recovery.
Choosing 'http' which is less common and requires additional setup.
2fill in blank
medium

Complete the code to enable versioning on the S3 bucket for state recovery.

Terraform
resource "aws_s3_bucket" "terraform_state" {
  bucket = "my-terraform-state"
  versioning {
    status = "[1]"
  }
}
Drag options to blanks, or click blank then click option'
ADisabled
BEnabled
CSuspended
DActive
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Disabled' or 'Suspended' which do not enable versioning.
Using 'Active' which is not a valid status value.
3fill in blank
hard

Fix the error in the backend configuration to enable encryption of the state file.

Terraform
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "state.tfstate"
    region         = "us-east-1"
    encrypt        = [1]
  }
}
Drag options to blanks, or click blank then click option'
Atrue
B"yes"
C"true"
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using quoted strings like "true" or "yes" which are invalid for boolean attributes.
Using capitalized True which is not valid in Terraform.
4fill in blank
hard

Fill both blanks to configure DynamoDB for state locking and specify the bucket name.

Terraform
terraform {
  backend "s3" {
    bucket         = "[1]"
    key            = "terraform.tfstate"
    region         = "us-west-2"
    dynamodb_table = "[2]"
    encrypt        = true
  }
}
Drag options to blanks, or click blank then click option'
Amy-terraform-state
Bterraform-lock-table
Cstate-lock
Dterraform-state-bucket
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the bucket name with the DynamoDB table name.
Using incorrect or missing names causing backend initialization errors.
5fill in blank
hard

Fill all three blanks to create an S3 bucket with versioning and a DynamoDB table for locking.

Terraform
resource "aws_s3_bucket" "terraform_state" {
  bucket = "[1]"
  versioning {
    status = "[2]"
  }
}

resource "aws_dynamodb_table" "terraform_lock" {
  name         = "[3]"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"
  attribute {
    name = "LockID"
    type = "S"
  }
}
Drag options to blanks, or click blank then click option'
Amy-terraform-state
BEnabled
Cterraform-lock-table
DDisabled
Attempts:
3 left
💡 Hint
Common Mistakes
Setting versioning status to 'Disabled' which disables versioning.
Using inconsistent names between resources and backend configuration.

Practice

(1/5)
1. What is the main purpose of using remote state storage in Terraform for disaster recovery?
easy
A. To create backups of your source code
B. To speed up Terraform plan and apply commands
C. To safely store the Terraform state file and enable recovery if lost or corrupted
D. To automatically update Terraform providers

Solution

  1. Step 1: Understand Terraform state role

    The Terraform state file tracks your infrastructure resources and their current status.
  2. Step 2: Importance of remote storage for disaster recovery

    Storing state remotely protects it from local loss or corruption, enabling recovery.
  3. Final Answer:

    To safely store the Terraform state file and enable recovery if lost or corrupted -> Option C
  4. Quick Check:

    Remote state protects infrastructure info = D [OK]
Hint: Remote state stores your infra info safely for recovery [OK]
Common Mistakes:
  • Confusing state storage with code backup
  • Thinking remote state speeds up commands
  • Assuming remote state updates providers
2. Which of the following is the correct syntax to configure an S3 backend for Terraform state with versioning enabled?
easy
A. backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" versioning = true }
B. backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" }
C. backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" encrypt = true }
D. backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" versioning = "enabled" }

Solution

  1. Step 1: Review S3 backend configuration syntax

    The S3 backend block supports bucket, key, region, and encrypt but not versioning directly.
  2. Step 2: Understand versioning setup

    Versioning is enabled on the S3 bucket itself, not via Terraform backend config.
  3. Final Answer:

    backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" } -> Option B
  4. Quick Check:

    Versioning is bucket setting, not backend config = C [OK]
Hint: Versioning is set on S3 bucket, not in Terraform backend block [OK]
Common Mistakes:
  • Trying to set versioning inside backend block
  • Confusing encrypt with versioning
  • Using wrong data types for versioning
3. Given this Terraform backend configuration snippet, what will happen if the local state file is deleted but the remote backend is intact?
terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "us-west-2"
  }
}
medium
A. Terraform will prompt to reinitialize the backend and then sync state
B. Terraform will fail because the local state file is missing
C. Terraform will create a new empty state file and overwrite remote state
D. Terraform will automatically download the remote state and continue

Solution

  1. Step 1: Understand backend initialization behavior

    Terraform requires backend initialization to connect local config with remote state.
  2. Step 2: Effect of missing local state file

    If local state is missing, Terraform prompts to reinitialize backend to sync remote state locally.
  3. Final Answer:

    Terraform will prompt to reinitialize the backend and then sync state -> Option A
  4. Quick Check:

    Missing local state triggers reinit and sync = B [OK]
Hint: Missing local state triggers backend reinit and sync prompt [OK]
Common Mistakes:
  • Assuming Terraform fails immediately
  • Thinking Terraform overwrites remote state blindly
  • Believing Terraform auto-downloads without reinit
4. You configured an S3 backend for Terraform state but forgot to enable bucket versioning. What problem might you face during disaster recovery?
medium
A. Terraform will create duplicate state files
B. Terraform will refuse to initialize the backend
C. State file will be encrypted automatically
D. You cannot recover previous versions of the state file if it gets corrupted

Solution

  1. Step 1: Role of versioning in disaster recovery

    Versioning allows keeping multiple versions of the state file to recover from mistakes or corruption.
  2. Step 2: Consequence of missing versioning

    Without versioning, if the state file is overwritten or corrupted, previous versions are lost permanently.
  3. Final Answer:

    You cannot recover previous versions of the state file if it gets corrupted -> Option D
  4. Quick Check:

    No versioning means no state history recovery = A [OK]
Hint: No versioning means lost state history on corruption [OK]
Common Mistakes:
  • Thinking Terraform blocks backend init without versioning
  • Assuming encryption is automatic
  • Believing duplicate state files are created
5. You want to ensure your Terraform state is protected against accidental deletion and corruption. Which combination of practices provides the best disaster recovery setup?
hard
A. Use remote backend with S3 bucket having versioning and server-side encryption enabled
B. Use local state files with manual backups on your computer
C. Use remote backend with S3 bucket without versioning but with encryption enabled
D. Use remote backend with local file copy enabled

Solution

  1. Step 1: Identify best remote backend features for disaster recovery

    Remote backend with S3 bucket versioning keeps multiple state versions; encryption protects data confidentiality.
  2. Step 2: Compare options

    Local files lack safety; no versioning risks losing history; local copy doesn't protect against corruption.
  3. Final Answer:

    Use remote backend with S3 bucket having versioning and server-side encryption enabled -> Option A
  4. Quick Check:

    Versioning + encryption on remote backend = best recovery [OK]
Hint: Combine versioning and encryption on remote backend for best safety [OK]
Common Mistakes:
  • Relying on local files only
  • Skipping versioning on S3 bucket
  • Confusing local copy with remote backup