Bird
Raised Fist0
Terraformcloud~20 mins

State locking with DynamoDB in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DynamoDB State Locking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Configuration
intermediate
2:00remaining
Terraform backend configuration for DynamoDB state locking
Which Terraform backend configuration snippet correctly enables state locking using a DynamoDB table named tf-lock-table in the us-west-2 region?
A
backend "s3" {
  bucket         = "my-terraform-state"
  key            = "state.tfstate"
  region         = "us-west-2"
  dynamodb_table = "tf-lock-table"
}
B
backend "s3" {
  bucket         = "my-terraform-state"
  key            = "state.tfstate"
  region         = "us-west-2"
  dynamodb_table = "tf-lock-table"
  encrypt        = false
}
C
}
eurt =        tpyrcne  
"elbat-kcol-ft" = elbat_bdomanyd  
"2-tsew-su" =         noiger  
"etatsft.etats" =            yek  
"etats-mrofarret-ym" =         tekcub  
{ "3s" dnekcab
D
backend "s3" {
  bucket         = "my-terraform-state"
  key            = "state.tfstate"
  region         = "us-west-2"
  dynamodb_table = "tf-lock-table"
  encrypt        = true
}
Attempts:
2 left
💡 Hint
Look for the correct attribute name to enable DynamoDB state locking and ensure encryption is enabled.
❓ Architecture
intermediate
2:00remaining
DynamoDB table design for Terraform state locking
Which DynamoDB table design is required to support Terraform state locking correctly?
AA table with a primary key named <code>LockID</code> of type string, with no sort key.
BA table with a composite primary key: partition key <code>LockID</code> (string) and sort key <code>StateID</code> (string).
CA table with a primary key named <code>StateName</code> of type number.
DA table with no primary key but with a global secondary index on <code>LockID</code>.
Attempts:
2 left
💡 Hint
Terraform expects a simple primary key named LockID for locking.
❓ service_behavior
advanced
2:00remaining
Effect of missing DynamoDB table on Terraform state locking
What happens when Terraform tries to acquire a lock using a DynamoDB table that does not exist?
ATerraform will create the DynamoDB table automatically and acquire the lock.
BTerraform will fail with an error indicating the DynamoDB table does not exist and stop the operation.
CTerraform will skip locking and proceed with state changes without any warning.
DTerraform will retry indefinitely until the table is created.
Attempts:
2 left
💡 Hint
Terraform requires the DynamoDB table to exist before locking.
❓ security
advanced
2:00remaining
IAM policy for Terraform to use DynamoDB state locking
Which IAM policy snippet grants the minimum required permissions for Terraform to use DynamoDB state locking on a table named tf-lock-table?
A
{
  "Effect": "Allow",
  "Action": ["dynamodb:CreateTable", "dynamodb:DeleteTable"],
  "Resource": "arn:aws:dynamodb:*:*:table/tf-lock-table"
}
B
{
  "Effect": "Allow",
  "Action": ["dynamodb:Scan", "dynamodb:Query"],
  "Resource": "arn:aws:dynamodb:*:*:table/tf-lock-table"
}
C
{
  "Effect": "Allow",
  "Action": ["dynamodb:PutItem", "dynamodb:GetItem", "dynamodb:DeleteItem"],
  "Resource": "arn:aws:dynamodb:*:*:table/tf-lock-table"
}
D
{
  "Effect": "Allow",
  "Action": ["dynamodb:UpdateItem"],
  "Resource": "arn:aws:dynamodb:*:*:table/tf-lock-table"
}
Attempts:
2 left
💡 Hint
Terraform needs to create, read, and delete lock items.
✅ Best Practice
expert
3:00remaining
Ensuring high availability for Terraform state locking with DynamoDB
Which approach best ensures high availability and durability for Terraform state locking using DynamoDB in a multi-region AWS setup?
AUse DynamoDB global tables to replicate the lock table across multiple regions and configure Terraform to use the global table endpoint.
BCreate multiple DynamoDB tables in each region and configure Terraform to switch tables based on region manually.
CCreate a single DynamoDB table in one region and configure Terraform to use that region only.
DUse S3 versioning instead of DynamoDB for locking to avoid regional dependency.
Attempts:
2 left
💡 Hint
Think about automatic replication and consistency across regions.

Practice

(1/5)
1. What is the main purpose of using a DynamoDB table in Terraform state management?
easy
A. To lock the Terraform state and prevent concurrent changes
B. To store Terraform configuration files
C. To host Terraform modules
D. To run Terraform commands remotely

Solution

  1. Step 1: Understand Terraform state locking

    Terraform uses state locking to avoid multiple users changing infrastructure at the same time, which can cause conflicts.
  2. Step 2: Role of DynamoDB in locking

    DynamoDB acts as a lock store to keep the Terraform state safe by preventing concurrent modifications.
  3. Final Answer:

    To lock the Terraform state and prevent concurrent changes -> Option A
  4. Quick Check:

    State locking = prevent concurrent changes [OK]
Hint: DynamoDB locks state to avoid conflicts [OK]
Common Mistakes:
  • Thinking DynamoDB stores config files
  • Confusing locking with module hosting
  • Assuming DynamoDB runs Terraform commands
2. Which Terraform backend configuration snippet correctly enables state locking with DynamoDB?
easy
A. backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" }
B. backend "local" { path = "./terraform.tfstate" dynamodb_table = "terraform-lock" }
C. backend "s3" { bucket = "mybucket" key = "state.tfstate" region = "us-east-1" dynamodb_table = "terraform-lock" }
D. backend "gcs" { bucket = "mybucket" dynamodb_table = "terraform-lock" }

Solution

  1. Step 1: Identify backend type supporting DynamoDB locking

    Only the S3 backend supports DynamoDB for state locking in Terraform.
  2. Step 2: Check for required parameters

    The S3 backend requires bucket, key, region, and dynamodb_table to enable locking.
  3. Final Answer:

    backend "s3" with bucket, key, region, and dynamodb_table -> Option C
  4. Quick Check:

    S3 backend + dynamodb_table = locking enabled [OK]
Hint: Use S3 backend with dynamodb_table for locking [OK]
Common Mistakes:
  • Adding dynamodb_table to unsupported backends
  • Missing key or region in S3 backend
  • Using local backend with DynamoDB
3. Given this backend configuration, what happens if two users run terraform apply at the same time?
backend "s3" {
  bucket         = "mybucket"
  key            = "prod/terraform.tfstate"
  region         = "us-west-2"
  dynamodb_table = "tf-locks"
}
medium
A. Both users can apply changes simultaneously without issues
B. Terraform will lock the state in DynamoDB, allowing only one user to apply at a time
C. Terraform will throw an error because DynamoDB is not supported
D. The state file will be overwritten causing conflicts

Solution

  1. Step 1: Understand DynamoDB locking behavior

    DynamoDB table is used to lock the state file so only one user can modify it at a time.
  2. Step 2: Effect on concurrent applies

    If two users run apply simultaneously, one will get the lock and the other will wait or fail until the lock is released.
  3. Final Answer:

    Terraform locks state in DynamoDB, allowing one user at a time -> Option B
  4. Quick Check:

    DynamoDB lock = single user apply [OK]
Hint: DynamoDB locks state to serialize applies [OK]
Common Mistakes:
  • Assuming simultaneous applies succeed
  • Thinking DynamoDB is unsupported
  • Believing state file overwrites happen
4. You configured your Terraform backend with DynamoDB locking but receive this error: Error locking state: ConditionalCheckFailedException. What is the most likely cause?
medium
A. Terraform version is too old to support DynamoDB locking
B. The S3 bucket name is misspelled
C. The backend block is missing the region parameter
D. The DynamoDB table does not exist or has incorrect permissions

Solution

  1. Step 1: Understand the error meaning

    ConditionalCheckFailedException usually means DynamoDB cannot perform the lock operation, often due to missing table or permission issues.
  2. Step 2: Check backend configuration and AWS resources

    If the DynamoDB table does not exist or Terraform lacks permission to access it, locking fails with this error.
  3. Final Answer:

    DynamoDB table missing or permission issues -> Option D
  4. Quick Check:

    ConditionalCheckFailedException = DynamoDB table or permissions [OK]
Hint: Check DynamoDB table existence and permissions first [OK]
Common Mistakes:
  • Blaming S3 bucket name for DynamoDB error
  • Ignoring Terraform version compatibility
  • Overlooking missing region parameter
5. You want to ensure your Terraform state locking is highly available and prevents accidental unlocks. Which combination of DynamoDB table settings is best practice?
hard
A. Enable point-in-time recovery and use a strong primary key with consistent read
B. Use a simple primary key and disable point-in-time recovery to reduce costs
C. Use eventual consistency and no encryption for faster locking
D. Create multiple DynamoDB tables for locking in different regions

Solution

  1. Step 1: Identify high availability features

    Point-in-time recovery protects against accidental deletes or corruption of the lock table.
  2. Step 2: Understand locking consistency needs

    Using a strong primary key and consistent reads ensures reliable lock state and prevents race conditions.
  3. Step 3: Evaluate other options

    Disabling recovery or using eventual consistency risks losing lock data or causing conflicts; multiple tables add complexity without benefit.
  4. Final Answer:

    Enable point-in-time recovery and use strong primary key with consistent read -> Option A
  5. Quick Check:

    Recovery + strong key + consistency = best locking [OK]
Hint: Enable recovery and strong keys for safe locking [OK]
Common Mistakes:
  • Disabling recovery to save money
  • Using eventual consistency causing conflicts
  • Creating multiple tables unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard