๐ Terraform Remote Backend with S3 and DynamoDB


๐ Introduction
When using Terraform in real-world projects, managing state locally (terraform.tfstate
) can become risky. If multiple engineers apply changes at the same time, the state file may get corrupted.
To solve this, Terraform supports remote backends. In this project, I implemented a Terraform Remote Backend using AWS S3 and DynamoDB.
S3 Bucket โ Stores Terraform state files centrally.
DynamoDB Table โ Ensures state locking and consistency.
Alongside the backend, this project also provisions an EC2 instance on AWS.
โ๏ธ Project Structure
backend-project/
โโโ main.tf # Defines AWS resources (EC2, S3, DynamoDB)
โโโ backend.tf # Configures Terraform remote backend
๐ ๏ธ Step 1: Define AWS Resources
In main.tf
, we define three resources:
EC2 Instance โ A simple t3.micro instance.
S3 Bucket โ To store
.tfstate
files.DynamoDB Table โ For state locking.
provider "aws" {
region = "eu-north-1"
}
resource "aws_instance" "harshal" {
instance_type = "t3.micro"
ami = "ami-042b4708b1d05f512"
}
resource "aws_s3_bucket" "s3_bucket" {
bucket = "harshal-bucket-s3buck"
}
resource "aws_dynamodb_table" "terraform-lock" {
name = "terraform-lock"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
๐ ๏ธ Step 2: Configure Remote Backend
In backend.tf
, we configure Terraform to use the S3 bucket and DynamoDB table created above.
terraform {
backend "s3" {
bucket = "harshal-bucket-s3buck"
key = "harshal/terraform.tfstate"
region = "eu-north-1"
dynamodb_table = "terraform-lock"
}
}
bucket
โ Name of your S3 bucket.key
โ Path inside the bucket where state file is stored.dynamodb_table
โ Table used for locking.
๐ ๏ธ Step 3: Initialize and Apply
Run the following commands:
# Initialize Terraform with remote backend
terraform init
# Review execution plan
terraform plan
# Apply configuration
terraform apply
โ After applying, your:
EC2 instance will be provisioned.
Terraform state file will be stored in S3.
State locking will be handled by DynamoDB.
โ Why Remote Backend?
Collaboration Ready โ Teams can work on the same infrastructure without conflicts.
State Locking โ DynamoDB ensures no two users modify state simultaneously.
Disaster Recovery โ S3 versioning allows rollback of previous state.
Security โ State file is stored safely in AWS instead of local machines.
๐ Project Repository
๐ Terraform Backend Project on GitHub
๐จโ๐ป Author
Harshal Vernekar
- GitHub: @Harshalv21
๐ With this setup, you now have a production-ready Terraform workflow using S3 + DynamoDB for state management.
Subscribe to my newsletter
Read articles from HARSHAL VERNEKAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

HARSHAL VERNEKAR
HARSHAL VERNEKAR
๐ Aspiring DevOps & Cloud Engineer with a strong foundation in cloud platforms (AWS), infrastructure automation, and container orchestration tools like Docker and Kubernetes. Iโm passionate about building reliable, scalable, and secure cloud-native applications. ๐ง Currently building real-world projects using Terraform, Ansible, Jenkins, GitHub Actions, and EKS to understand how modern infrastructure is deployed, managed, and monitored. I enjoy breaking things (safely), debugging, and learning from hands-on experience. ๐ฆ Comfortable working with: AWS (EC2, S3, IAM, VPC, EKS) Docker, Kubernetes (Minikube & EKS) CI/CD tools like Jenkins & GitHub Actions IaC tools like Terraform & Ansible Monitoring with Prometheus & Grafana Linux, Bash, Git, and Networking fundamentals ๐ก Always learning โ currently exploring deeper concepts in Kubernetes workloads, Helm, and scaling best practices. ๐ Open to DevOps, Cloud, or SRE roles where I can grow, contribute, and solve real-world problems.