professional-service/examples/terraform-pg-backend-state-locking/01-example-project
Mauritz Uphoff 1bc2b0e694
All checks were successful
Default CI / Check for Open TODOs (pull_request) Successful in 31s
Default CI / Pre-Commit Hooks (pull_request) Successful in 2m15s
ci: switch to gitleaks
2026-06-29 11:22:36 +02:00
..
scripts chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
.terraform.lock.hcl chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
000-variables.tf chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
010-backend.tf chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
020-provider.tf chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
030-project.tf chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
backend.conf.example chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00
README.md ci: switch to gitleaks 2026-06-29 11:22:36 +02:00
terraform.tfvars.example chore(terraform pg backend): renamed example to 'terraform-pg-backend-state-locking' (#44) 2026-06-26 09:51:10 +00:00

Phase 1: Example Infrastructure

This module contains the core infrastructure configuration. It uses the pg backend to store state and enforce state locks via the STACKIT PostgreSQL Flex database provisioned in Phase 0.

Implementation Steps

  1. Create a backend.conf file in this directory and define the connection string using the credentials generated by the bootstrap module:

    conn_str = "postgres://tf_state_user:<PASSWORD>@<STACKIT_INSTANCE_HOST>:5432/terraform_state?sslmode=require"
    
  2. Initialize Terraform and bind it to the remote PostgreSQL backend

    terraform init -backend-config=backend.conf
    
  3. Provision the infrastructure or run the lock validation script:

    chmod +x ./scripts/validate_lock.sh
    ./scripts/validate_lock.sh
    

Log Output

➜  01-example-project ~ ./scripts/test-state-lock.sh
[INFO] Initiating background 'terraform apply' to acquire the state lock...
[INFO] Attempting concurrent 'terraform plan'...
[INFO] ------------------------------------------------------------------
╷
│ Error: Error acquiring the state lock
│
│ Error message: Already locked for workspace creation: default
│ Lock Info:
│   ID:        XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXXXXXX
│   Path:
│   Operation: OperationTypePlan
│   Who:       XXXX
│   Version:   1.14.9
│   Created:   2026-06-25 16:21:59.636986 +0000 UTC
│   Info:
│
│
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended.
╵
[INFO] ------------------------------------------------------------------
[SUCCESS] Concurrent operation rejected. State locking is active and functional.
[INFO] Waiting for the background 'terraform apply' process to terminate...
[INFO] Evaluation complete. Cleaning up temporary logs...