Course contentsShow
← Cloud and Infrastructure
Lesson 787 of 2,98915. Terraform in PracticeFree lesson

DynamoDB for State Locking

Configure DynamoDB table for state locking with S3 backend to enable safe concurrent operations.

DynamoDB for State Locking

What you'll learn: How to configure a DynamoDB table to enable state locking when using an S3 backend, preventing concurrent Terraform operations from corrupting your state.

Why DynamoDB for Locking?

When you configured your S3 backend in earlier lessons, you learned that state locking prevents multiple users from modifying infrastructure simultaneously. While S3 stores your state file, it cannot provide locking on its own. This is where DynamoDB comes in—it acts as a coordination service that Terraform uses to acquire and release locks.

Think of it like a sign-out sheet at a tool library. S3 is the shelf where the tool (your state file) sits, but DynamoDB is the clipboard where you write your name to claim the tool. No one else can check it out until you return it.

Configuring the DynamoDB Table

Your DynamoDB table needs specific attributes for Terraform to use it for locking:

  • Table name: Can be anything you choose (commonly terraform-state-lock)
  • Primary key: Must be named LockID (string type)
  • Billing mode: Pay-per-request is cost-effective for most use cases

Connecting to Your Backend

When you configure your S3 backend, add the dynamodb_table argument pointing to your DynamoDB table name. Terraform automatically handles lock creation, monitoring, and release during operations like plan and apply.

If another user is running an operation, Terraform will detect the lock and wait or fail gracefully, protecting your state from corruption.

Key Takeaway: DynamoDB provides the coordination mechanism for state locking with S3 backends—configure a table with a LockID primary key and reference it in your backend configuration to enable safe concurrent Terraform operations.