Skip to main content

Remote State

Terraform remote state stores the state file in a shared backend such as Amazon S3 or Terraform Cloud instead of on a local disk. It lets teams and CI pipelines share one source of truth and lets one configuration read another's outputs.

What Is Remote State?

With local state, each engineer holds a different copy, which leads to duplicated or broken resources. A remote backend centralises state with locking, versioning, and access control.

HCL
terraform {
backend "s3" {
bucket = "netflix-terraform-state-ap-south-1"
key = "prod/payments-api/terraform.tfstate"
region = "ap-south-1"
encrypt = true
use_lockfile = true # S3-native locking, Terraform 1.11 and later
}
}

Create the bucket separately with versioning on, because Terraform cannot create the bucket that stores its own state. Older versions lock with a DynamoDB table instead. Backend blocks cannot use variables.

Security

State often holds secrets in plaintext. Encrypt it, block public access, and limit who can read it.

Frequently Asked Questions

Why is local state risky beyond solo use?

It has no locking or shared access, so concurrent applies can corrupt it, and it is lost if the laptop is.

Do I still need DynamoDB for S3 locking?

Not on Terraform 1.11 and later, where use_lockfile provides native S3 locking. Older versions still need DynamoDB.

Found a mistake, an outdated step or a broken link?