Terraform Remote State and State Locking Explained
If you've ever worked on a Terraform project with multiple engineers, you've probably encountered state management challenges. One person applies infrastructure changes while another is working from an outdated state file. The result can be failed deployments, resource drift, or even accidental resource destruction.
This is exactly why Remote State and State Locking in Terraform exist.
Remote state allows teams to store Terraform state in a shared location, while state locking prevents multiple operations from modifying the same state file simultaneously. Together, they make Terraform safer, more reliable, and suitable for production environments.
In this guide, you'll learn how remote state works, why local state becomes a problem as teams grow, and how state locking protects your infrastructure from conflicting changes.
What Is Terraform State?
Terraform uses a state file to track the resources it manages.
By default, Terraform stores this information in a file named terraform.tfstate. The state file acts as Terraform's memory, allowing it to understand what infrastructure already exists and what changes need to be made during future runs.
If you're new to state management, start with this guide on Terraform State Introduction to understand the fundamentals.
Terraform state stores information such as:
- Resource IDs
- IP addresses
- Metadata about dependencies
- Resource attributes
- Infrastructure mappings
Terraform relies on state to:
- Map real infrastructure to configuration files
- Determine what changes are required
- Track resource dependencies
- Improve performance during planning and applying operations
- Maintain consistency across deployments
Without state, Terraform would need to rediscover every resource during every execution, making infrastructure management slower and less reliable.
Why Local Terraform State Becomes a Problem
Local state works well when you're experimenting or learning Terraform. Problems begin when multiple engineers start managing the same infrastructure.
No Centralized Collaboration
A local state file only exists on the machine where Terraform was executed.
If another engineer needs to update the infrastructure, they won't automatically have access to the latest state file. Teams often try sharing state through Git repositories, shared folders, or manual transfers, which creates operational risks.
Security Risks
Terraform state files can contain sensitive information.
Depending on the providers and resources used, state may contain:
- Internal IP addresses
- Resource identifiers
- Database endpoints
- Usernames
- Configuration details
For this reason, Terraform state files should never be treated like normal application code.
No Cross-Team State Locking
Git repositories can version state files, but they cannot lock them.
Imagine Engineer A starts running terraform apply. Before the operation finishes, Engineer B starts another apply using a different version of the state file.
Now both operations are attempting to modify infrastructure independently.
This situation can lead to:
- State corruption
- Infrastructure drift
- Failed deployments
- Unexpected resource recreation
Pro Tip: Most production Terraform environments move to remote state long before multiple engineers begin managing the same infrastructure.
What Is State Locking in Terraform and Why Does It Matter?
State locking is Terraform's mechanism for preventing concurrent operations against the same state file.
When Terraform acquires a lock, no other operation can modify the state until the current operation finishes.
For example:
- Engineer A runs
terraform apply - Terraform acquires a lock on the state
- Engineer B attempts another
terraform apply - Terraform blocks the second operation
- The lock is released after the first operation completes
This simple process prevents conflicting updates and keeps infrastructure state consistent.
State locking becomes especially important when:
- Multiple engineers work on the same environment
- CI/CD pipelines execute Terraform automatically
- Infrastructure changes happen frequently
- Teams manage production workloads
Imagine a deployment pipeline running Terraform while an engineer manually applies infrastructure changes. Without locking, both processes could attempt to update the state simultaneously.
Remote State and State Locking in Terraform: Why Teams Need It
Remote state solves the collaboration and reliability problems associated with local state.
Instead of storing state on an individual workstation, Terraform stores the state in a shared backend that all team members can access.
Terraform supports several backend options through its backend configuration system documented in the official Terraform Backend Documentation.
Benefits of remote state include:
- Shared access across teams
- Centralized state management
- Improved security controls
- Backup and recovery options
- State versioning support
- Integrated state locking capabilities
- Better automation support
Common remote state backends include:
- AWS S3
- Terraform Cloud
- Google Cloud Storage
- Azure Blob Storage
- HashiCorp Consul
For AWS environments, an AWS S3 remote backend with Terraform is one of the most widely used production patterns.
Common Terraform Collaboration Problems Solved by Remote State
Let's look at a real-world scenario.
Collaboration Chaos Without Remote State
Imagine two engineers working on the same infrastructure.
Engineer A creates a new S3 bucket and updates the infrastructure.
Engineer B pulls the repository later and makes additional changes using an outdated copy of the state file.
When Engineer B runs Terraform:
- Terraform may not recognize recently created resources
- Infrastructure drift can occur
- Existing resources may be recreated
- Unexpected changes may appear in execution plans
This happens more often than many teams expect, especially when state files are shared manually.
Secure Remote Storage and State Locking
Remote state eliminates these problems.
A common production architecture uses:
- An S3 bucket for storing Terraform state
- A DynamoDB table for state locking
- Encryption for protecting state data
- Versioning for rollback and recovery
In AWS environments:
- State is stored centrally in S3
- Every engineer accesses the latest state automatically
- Terraform uses a DynamoDB lock table to prevent concurrent updates
- Version history protects against accidental changes
To understand implementation details, see this guide on AWS DynamoDB with Terraform.
What Happens When State Locking Is Disabled?
Without state locking, conflicts can occur very quickly.
A typical failure scenario looks like this:
- Engineer A starts
terraform apply - Terraform begins updating resources
- Engineer B starts another apply
- Both processes attempt to update state
- State becomes inconsistent
Potential outcomes include:
- Corrupted state
- Partial deployments
- Infrastructure drift
- Failed future applies
- Unexpected resource recreation
State locking exists specifically to prevent these situations.
Local State vs Remote State in Practice
When deciding between local and remote state, consider the operational requirements of your environment.
Local state may be acceptable for:
- Personal learning projects
- Temporary proof-of-concept environments
- Short-lived experiments
Remote state is strongly recommended for:
- Team environments
- Shared infrastructure
- Production workloads
- CI/CD pipelines
- Long-term infrastructure projects
As infrastructure grows, remote state becomes a necessity rather than a convenience.
Best Practices for Remote State Management
Enable Encryption
State files often contain infrastructure metadata that should be protected.
Always enable encryption at rest and encryption in transit whenever supported by your backend.
Enable Versioning
If you're using S3, enable bucket versioning.
Versioning allows recovery of previous state versions when accidental changes occur.
Use State Locking
Never rely solely on shared storage.
For AWS environments, combine S3 with DynamoDB locking to ensure only one operation modifies state at a time.
Keep State Out of Git
Add the following entries to .gitignore:
terraform.tfstate
terraform.tfstate.backup
.terraform/
State files should never be committed to source control.
Separate Environments Properly
Development, staging, and production environments should have isolated state.
You can achieve this through separate backends or by using Terraform Workspaces when appropriate.
Restrict Access
Only authorized users and automation systems should have access to state storage locations.
Apply least-privilege IAM policies and audit access regularly.
Production Example: AWS Remote State Architecture
A common production setup looks like this:
Terraform CLI
|
v
Amazon S3 Bucket
(terraform-prod-state)
|
v
Amazon DynamoDB
(terraform-state-locks)
Recommended configuration:
- S3 bucket versioning enabled
- S3 server-side encryption enabled
- Restricted IAM access
- Dedicated DynamoDB lock table
- Automated backups
- Separate state files per environment
This architecture is widely used because it is simple, secure, and cost-effective.
Additional Terraform Resources
To deepen your understanding of Terraform state management and infrastructure automation, explore these related guides:
- Purpose of State in Terraform
- Terraform State Commands
- Terraform State Considerations
- Getting Started with Terraform and AWS
- Terraform Registry
- Terraform Language Documentation
Frequently Asked Questions (FAQs)
What happens if multiple people apply Terraform at the same time?
If state locking is not configured, multiple users can modify the same state file simultaneously. This can create state corruption, infrastructure drift, and failed deployments. State locking ensures only one operation can update the state at a time.
Is storing Terraform state in Git safe?
No. Terraform state files may contain sensitive infrastructure information and operational metadata. Storing them in Git increases the risk of accidental exposure and operational conflicts. Remote backends provide a much safer alternative.
Can I use remote state without state locking?
Yes, but it is not recommended. Remote storage solves the collaboration problem, but without locking, concurrent operations can still create inconsistent state. Production environments should always use a locking mechanism when available.
Which Terraform backends support state locking?
Terraform Cloud, Consul, and AWS S3 combined with DynamoDB are common solutions that support locking workflows. Always review the official backend documentation to understand backend-specific behavior and limitations.
What is the difference between local state and remote state?
Local state is stored on the machine where Terraform runs. Remote state is stored in a shared backend that multiple users and automation systems can access. Remote state improves collaboration, security, backup capabilities, and operational reliability.
Final Thoughts
Remote state and state locking are foundational practices for running Terraform in real-world environments.
Local state may be enough for learning Terraform, but production infrastructure requires centralized state management, secure storage, and protection against concurrent modifications.
Using a remote backend such as AWS S3 together with DynamoDB locking provides a reliable foundation for team-based infrastructure management. It reduces operational risk, improves collaboration, and helps ensure Terraform always works from a single source of truth.
8 free, 100% client-side tools for developers — no signup, no data uploads.
Explore all tools