π Terraform Series β Day 12

Search for a command to run...

No comments yet. Be the first to comment.
π Terraform Series β Automate Your Infrastructure Starting a complete **Terraform series** where Iβll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β’ What is Infrastructure as Code (IaC) & why it matters β’ Terraform fundamentals (providers, resources, state) β’ Writing and managing Terraform configurations β’ Variables, outputs & modules β’ Remote state & state management β’ Provisioning infrastructure on AWS β’ Automation & real-world use cases β’ Advanced concepts like workspaces, modules, and best practices π― Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and letβs build infrastructure the smart way β‘π» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation
Introduction to Terraform & Infrastructure as Code (IaC) In modern DevOps practices, managing infrastructure manually is no longer scalable or efficient. Organizations are rapidly shifting towards aut
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices π Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs π Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml π Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices π Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

Secure State Management (S3 + DynamoDB Locking)
In Terraform, the state file (terraform.tfstate) is the most critical component that connects your configuration with real infrastructure. However, storing it locally can lead to security risks, data loss, and team conflicts.
This blog explains how to securely manage Terraform state using AWS S3 (remote storage) and DynamoDB (state locking), which is the industry-standard approach for production environments.
After completing this blog, you will be able to:
Understand Terraform state and its importance
Know why .tfstate should never be pushed to GitHub
Handle state loss scenarios
Understand state conflicts in team environments
Implement remote backend using S3 + DynamoDB
Test state locking in real scenarios
π· Step 1: What is Terraform State?
Terraform maintains a file:
terraform.tfstate
Real infrastructure details
Resource IDs and attributes
Mapping between Terraform code β AWS resources
π· Step 2: Should You Push
.tfstateto GitHub?
π β NO β Never do this
Because it contains:
Secrets (API keys, credentials)
Resource IDs
Internal infrastructure data
π This can lead to security breaches
.gitignore*.tfstate
*.tfstate.backup
π· Step 3: What if
.tfstateis Deleted?
π Terraform loses tracking of infrastructure
Terraform thinks β nothing exists
Next terraform apply β tries to recreate everything β
Restore from backup (.tfstate.backup)
Use remote backend (best practice)
π· Step 4: State Conflict (Very Important)
Developer 1 β runs terraform apply
Developer 2 β runs terraform apply
Both modify same state file
File gets overwritten or corrupted
π This is called State Conflict
π· Step 5: Solutions
β Local Shared State
Not safe
Not scalable
β Remote Backend (Best Practice)
Use:
S3 Bucket β Store state file
DynamoDB β Lock state
π· Step 6: Architecture Flow
Terraform stores state in S3
Before update β checks DynamoDB
If no lock β creates LockID
While locked β β no parallel execution
After completion β lock removed
π· Step 7: Practical Implementation
mkdir remote-infra
cd remote-infra
touch provider.tf terraform.tf s3.tf dynamodb.tf
provider "aws" {
region = "us-east-2"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
resource "random_id" "suffix" {
byte_length = 2
}
resource "aws_s3_bucket" "remote_s3" {
bucket = "dev-tf-state-${random_id.suffix.hex}"
tags = {
Name = "tf-state-bucket"
Environment = "dev"
}
}
resource "aws_dynamodb_table" "state_lock" {
name = "apurv-table"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
tags = {
Name = "apurv-table"
Environment = "Dev"
}
}
Ensure your AWS user/role has:
S3 Full Access
DynamoDB Full Access
terraform init
terraform validate
terraform plan
terraform apply
π· Step 9: Configure Remote Backend
Now go to your main project folder and update:
terraform {
backend "s3" {
bucket = "bucket<name>"
key = "terraform.tfstate"
region = "us-east-2"
dynamodb_table = "apurv-table"
}
}
terraform init
π· Step 10: Remove Local State
rm terraform.tfstate*
terraform state list
π Resources will still appear
β Because state is now stored in S3
π· Step 11: Test State Locking
terraform apply
terraform apply
Terminal 2 β β blocked / waits
Reason β Lock exists in DynamoDB
Lock is removed
Second execution proceeds
After testing all the things you can destroy your resources
Terraform state is critical for infrastructure tracking
Never store state locally in production
Use S3 for storage + DynamoDB for locking
Prevents:
Data loss
State conflicts
Security risks
βA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.β
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar