What is Terraform State

Search for a command to run...

No comments yet. Be the first to comment.
In this short series, we will include some articles to go through how to write your own terraform code.
divide and rule
Technologies come and go, but data is here to grow! Introduction Data is the core of every modern application, driving insights, automation, and user experiences. Over time, technologies have evolved

Introduction In this guide, we’ll walk through creating an Amazon EMR cluster in a private subnet using Terraform. By placing your EMR cluster in a private subnet, you prevent it from being directly accessible from the internet — improving both secur...

Introduction Apache Spark, a powerful distributed processing engine, relies heavily on memory management for optimal performance.The Unified Memory Manager (UMM) — introduced in Spark 1.6 — plays a ke

Introduction In this series, we’ll focus on Serverless Framework, one of the fastest ways to build and deploy serverless applications. If you’ve already worked with Terraform, you know how great it is

Once you’ve created an S3 bucket, you’ll likely need to transfer large files — sometimes in the order of gigabytes or even terabytes. AWS S3 allows object sizes of up to 5 TB, but a single PUT upload is limited to 5 GB. To handle larger uploads, S3 o...

In this section, we discuss the concept of Terraform state.
Terraform is responsible for creating or altering resources on a platform (for example, deploying pods to Kubernetes or creating resources on AWS). To manage this, Terraform needs some sort of memory — a record of the current state of resources at any given time.
This “memory” is called the Terraform state.
It’s useful because, based on what you ask Terraform to do, it can look up the current state from its state file and generate the so-called terraform plan. In short:
The current state + desired configuration → Terraform Plan
When you initialize a Terraform project with terraform init, the state is often stored locally in a file named .tfstate.
At first glance, that seems fine — but losing this file means losing Terraform’s memory. Without it, Terraform can no longer understand what resources exist or what to change. Hence, securing this file and avoiding accidental deletion is critical.
Store the state file remotely — for example, in AWS S3, so that your team can share infrastructure changes safely. Using S3 versioning and locking also adds extra security and recoverability.
Alternatively, store it in HashiCorp Cloud, which provides fine-grained access controls and additional convenience.
To store your Terraform state remotely in S3, follow these steps:
Create an S3 bucket to hold the state file. Example: tf-state-bucket
Create a DynamoDB table to manage state locking. Example: tf-state-table with partition key LockID
Ensure your Terraform IAM role has GetObject and PutObject permissions for the S3 bucket.
Then, configure your Terraform backend in a file named backend.tf:
terraform {
backend "s3" {
bucket = "tf-state-bucket"
key = "key-for-deployment"
region = "us-east-2"
dynamodb_table = "tf-state-table"
}
}
This tells Terraform to use S3 as the remote backend. All changes to your infrastructure will now be reflected in the S3 state file.
This is particularly important when using GitHub Actions or other CI/CD workflows, since those runs are ephemeral — they don’t persist local state. A remote S3 backend ensures consistent state management across environments and runs.