You try to run terraform plan or apply, and instead of seeing your infrastructure changes, you get hit with this wall of text:
Why does this happen?
Terraform locks your State File to prevent two people (or two CI/CD jobs) from making changes at the exact same time. This prevents infrastructure corruption. However, if your terminal crashes or your internet drops during an apply, Terraform might not have the chance to "unlock" the file.
Step 1: The Safe Way (Wait)
Before you do anything, check the Who and Created section in the error. If it says your colleague is currently running a plan, don't touch it. Wait for them to finish.
Step 2: The Manual Fix (Force Unlock)
If you are 100% sure that no one else is running Terraform (e.g., your own previous process crashed), you can manually break the lock using the Lock ID provided in the error message.
Run this command:
Example: terraform force-unlock a1b2c3d4-e5f6-g7h8-i9p0
Step 3: Handling Remote State (S3 + DynamoDB)
If you are using AWS S3 as a backend, Terraform uses a DynamoDB table to manage locks. If force-unlock fails, you can:
Go to the AWS Console.
Open the DynamoDB table used for your state locking.
Find the item with the Lock ID and manually delete the item from the table.
Pro-Tip: Preventing Future Locks
If this happens frequently in your CI/CD (like GitHub Actions or Jenkins), ensure you have a "Timeout" set. Also, always use a Remote Backend rather than local state files to ensure that if your local machine dies, the lock is manageable by the team.