Need help implementing isolated workspaces to fix DynamoDB state lock contention

Fath Swift 0 Reputation points
2026-10-07T16:08:14.64+00:00

Hi there,

I'm having trouble running parallel CI/CD runners to apply infrastructure plans because we are hitting DynamoDB state lock contention, so our deployments keep failing. Can we get advice on how to implement isolated workspace deployment stages to resolve this?

Windows for business | Windows 365 Enterprise
0 comments No comments

1 answer

Sort by: Most helpful
  1. Ronald Sabiiti 160 Reputation points Independent Advisor
    2026-10-07T16:15:47.6033333+00:00

    Hello @Fath Swift

    Welcome to Microsoft Q&A!

    Thank you for your patience, and taking the time to elaborate on your concerns.

    DynamoDB state lock contention is a common pain point when multiple Terraform runners try to apply plans in parallel. The supported way to avoid this is to isolate state usage so each runner has its own lock scope.

    Practical Implementation Options.

    1. Workspaces per Pipeline/Environment

    Use terraform workspace to separate state files.

    Each workspace automatically uses a different key prefix in the DynamoDB lock table.

    In CI/CD, set TF_WORKSPACE=$CI_PIPELINE_ID or $ENVIRONMENT so each runner gets its own isolated workspace.

    2. Separate Backend Paths

    Configure unique S3 key prefixes for each environment/stage:

    hcl

    backend "s3" {
      bucket         = "my-terraform-state"
      key            = "env/${var.env}/terraform.tfstate"
      region         = "us-east-1"
      dynamodb_table = "terraform-locks"
    }
    

    This ensures dev, test, and prod don’t fight over the same lock.

    3. Ephemeral Workspaces for CI/CD

    For parallel test runs, generate a unique workspace name tied to the build ID.

    Example: terraform workspace new build-${CI_JOB_ID}

    Destroy or clean up after the run to avoid clutter.

    4. Serialized Deployment for Shared Environments

    • If multiple jobs must target the same environment, enforce serialization.
    • Use CI/CD pipeline stages with explicit dependencies so only one runner applies at a time.

      Best Practices.

    • Keep state files scoped to a single environment or service.
    • Avoid “global” state unless absolutely necessary.
    • Document workspace naming conventions so teams don’t accidentally overlap.
    • Monitor DynamoDB lock table entries to confirm isolation is working.

    In Summary: To fix DynamoDB lock contention, configure isolated workspaces or backend paths for each CI/CD runner. For ephemeral builds, generate unique workspace names; for shared environments, enforce serialization. This ensures parallel jobs don’t collide on the same lock.

    I hope this helps to better understand the diagnostic options available and the risks involved.

    If this information was helpful, please click Accept Answer.

    Thank you for choosing Microsoft Q&A.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.