Terraform Remote State Storage & Locking¶
Step-01: Introduction¶
- Understand Terraform Backends
- Understand about Remote State Storage and its advantages
- This state is stored by default in a local file named "terraform.tfstate", but it can also be stored remotely, which works better in a team environment.
- Create AWS S3 bucket to store
terraform.tfstatefile and enable backend configurations in terraform settings block - Understand about State Locking and its advantages
- Create DynamoDB Table and implement State Locking by enabling the same in Terraform backend configuration
Step-02: Create S3 Bucket¶
- Go to Services -> S3 -> Create Bucket
- Bucket name: terraform-stacksimplify
- Region: US-East (N.Virginia)
- Bucket settings for Block Public Access: leave to defaults
- Bucket Versioning: Enable
- Rest all leave to defaults
- Click on Create Bucket
- Create Folder
- Folder Name: dev
- Click on Create Folder
Step-03: Terraform Backend Configuration¶
- Reference Sub-folder: terraform-manifests
- Terraform Backend as S3
- Add the below listed Terraform backend block in
Terrafrom Settingsblock inmain.tf
Step-04: Test with Remote State Storage Backend¶
# Initialize Terraform
terraform init
Observation:
Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
# Verify S3 Bucket for terraform.tfstate file
bucket-name/dev/terraform.tfstate
# Validate Terraform configuration files
terraform validate
# Verify S3 Bucket for terraform.tfstate file
bucket-name/dev/terraform.tfstate
# Format Terraform configuration files
terraform fmt
# Verify S3 Bucket for terraform.tfstate file
bucket-name/dev/terraform.tfstate
# Review the terraform plan
terraform plan
# Verify S3 Bucket for terraform.tfstate file
bucket-name/dev/terraform.tfstate
# Create Resources
terraform apply -auto-approve
# Verify S3 Bucket for terraform.tfstate file
bucket-name/dev/terraform.tfstate
Observation: Finally at this point you should see the terraform.tfstate file in s3 bucket
# Access Application
http://<Public-DNS>
Step-05: Bucket Versioning - Test¶
- Update in
c2-variables.tf - Execute Terraform Commands
Step-06: Destroy Resources¶
- Destroy Resources and Verify Bucket Versioning
Step-07: Terraform State Locking Introduction¶
- Understand about Terraform State Locking Advantages
Step-08: Add State Locking Feature using DynamoDB Table¶
- Create Dynamo DB Table
- Table Name: terraform-dev-state-table
- Partition key (Primary Key): LockID (Type as String)
- Table settings: Use default settings (checked)
- Click on Create
Step-09: Update DynamoDB Table entry in backend in c1-versions.tf¶
- Enable State Locking in
c1-versions.tf
Step-10: Execute Terraform Commands¶
# Initialize Terraform
terraform init
# Review the terraform plan
terraform plan
Observation:
1) Below messages displayed at start and end of command
Acquiring state lock. This may take a few moments...
Releasing state lock. This may take a few moments...
2) Verify DynamoDB Table -> Items tab
# Create Resources
terraform apply -auto-approve
# Verify S3 Bucket for terraform.tfstate file
bucket-name/dev2/terraform.tfstate
Observation: New version of terraform.tfstate file will be created in dev2 folder
Step-11: Destroy Resources¶
- Destroy Resources and Verify Bucket Versioning
References¶
- AWS S3 Backend
- Terraform Backends
- Terraform State Storage
- Terraform State Locking
- Remote Backends - Enhanced
🎉 New Course
Ultimate DevOps Real-World Project Implementation on AWS
$15.99
$84.99
81% OFF
MARCH2026
Enroll Now on Udemy →
🎉 Offer