惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
D
Docker
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
博客园_首页
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
V
V2EX
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Terraform State File Management with Remote Backend
Brian Mengo · 2026-05-01 · via DEV Community
Cover image for Terraform State File Management with Remote Backend

Brian Mengo

On day 4 of my learning journey I learnt how Terraform remembers what it creates, and how to manage that memory safely using a remote backend.

When running terraform apply, a file named terraform.tfstate is generated in the working directory.
This file is critical because it stores metadata that Terraform uses to track infrastructure resources.

Why Remote State Matters
When Terraform runs, it keeps track of infrastructure in a state file.

If that file stays on a local machine:

  • It’s not shareable
  • It’s not safe
  • It can easily get corrupted or lost

StateFile Best Practices
Store StateFile to a Remote Backend: The problem with Statefile is it also contains all the important information like configuration, access keys and other sensitive information. So we shouldn't log that to a github or any other personal folders. It is better to keep that in a Remote Backend like S3 in AWS, Blob in Azure and GCP Cloud Staorage.

Do Not Update/Delete StateFile: StateFile will always be generated by Terraform. You should not make any manual changes to that file or shouldn't delete them.

State Locking: Just imagine there is a StateFile and 2 devops engineers tries to modify or create infra using that file with different changes. This will corrupt StateFile. So It should be locked in such a way until first user completes terraform apply, then it should be unlocked and second user should apply his changes after that.

Isolation of StateFile: StateFile should be isolated for multiple environments in a different folder. Not all StateFile should be combined with a same name or in a same folder.

Regular Backup: It is essential to take regular backup's of a StateFile as we cannot access them in case of Global outages. So Enable Versioning on AWS S3 Buckets and also setup policies to store older StateFiles to a different account or tar them to other location.

Backend Configuration Details

  • The S3 bucket used for state storage must already exist before initializing Terraform.
  • The bucket is not created by Terraform itself because it must be available to store the state file before Terraform runs.
  • Creating the bucket manually can be done via AWS CLI, AWS Console, or CI/CD pipelines.
  • Avoid including the bucket creation in Terraform resources to prevent circular dependencies.

Configured Remote Backend
I updated my Terraform configuration to use S3 as backend:

backend "s3" {
  bucket       = "my-terraform-state-bucket"
  key          = "day-04/dev/terraform.tfstate"
  region       = "us-east-1"
  use_lockfile = true
  encrypt      = true
}

Enter fullscreen mode Exit fullscreen mode

Observing Remote State File Behaviour

  • When running terraform plan or terraform apply, no state file is created locally except a minimal metadata file.
  • The actual state file is stored inside the S3 bucket under the specified key (e.g.,dev/terraform.tfstate).
  • The remote state file is a JSON file containing all resource details, including some encoded and sensitive information.
  • This setup improves security by avoiding sensitive state information on local machines and centralising management.

Verified state in S3

terraform.tfstate file

Managing Terraform State

Terraform provides commands to manage state without manual JSON editing:

terraform state list Lists resources in the state file
terraform state show Shows detailed info about a specific resource
terraform state rm Removes a resource from the state file (safe method)
terraform state pull Fetches the current state file from backend

These commands help manage the state file programmatically and safely.

Below is the Youtube Video for reference: Tech Tutorials with Piyush — “Terraform StateFile Management with S3”