Remote backends: S3, GCS and azurerm
Move state off your laptop into a bucket: the S3, GCS and azurerm backends, the bootstrap problem, partial configuration and what to lock down.
Remote backends: S3, GCS and azurerm
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A backend decides where state is stored and how it is locked. The default `local` backend writes `terraform.tfstate` next to your code, which only works for one person on one machine.
- S3, GCS and azurerm each store the state as one object in a bucket or container that must already exist. Turn on versioning for it, because versioning is the way back from a bad write.
- A `backend` block cannot use variables in Terraform. Leave arguments out and pass them with `-backend-config` on `init`. OpenTofu 1.8 and later allows variables and locals in backend blocks, with limits.
- The bucket that holds state cannot be created by the configuration that stores state in it. Create it once by hand or from a small separate configuration, then point everything else at it.
- Changing the backend block and running `terraform init -migrate-state` copies existing state to the new location. Back up the old file first.
Two people, one laptop’s worth of state
The first plan after a teammate joins goes wrong in a boring way. You applied last Tuesday, so the state file is on your machine. Their plan sees no state and proposes to create everything again. If they apply, you have two copies of every resource. If they don’t, they ask you to send the file, and now there are two copies of state to keep in step.
A backend fixes this by putting state in one place everyone reads and writes. The default backend is local, which is the terraform.tfstate file you already know. The others move it to shared storage and, in most cases, add locking, which the next lesson covers.
Three object stores
The three most common backends for the big clouds are all the same idea. State is one object in a bucket or container, and the backend reads it before each plan and writes it after each apply. The configurations below are written from the backend docs. I did not run them, since there is no Terraform, AWS, Google or Azure account on this machine.
# AWS: a bucket and a key
terraform {
backend "s3" {
bucket = "acme-terraform-state"
key = "notes/prod/terraform.tfstate"
region = "eu-west-1"
encrypt = true
use_lockfile = true
}
}
# Google Cloud: a bucket and a prefix
terraform {
backend "gcs" {
bucket = "acme-terraform-state"
prefix = "notes/prod"
}
}
# Azure: a storage account, a container and a blob name
terraform {
backend "azurerm" {
resource_group_name = "tfstate-rg"
storage_account_name = "acmetfstate"
container_name = "tfstate"
key = "notes/prod.tfstate"
use_azuread_auth = true
}
}
A few details that differ. The S3 backend stores state at key, and locking is opt-in through use_lockfile. The GCS backend writes <prefix>/<workspace>.tfstate, and the docs say it supports state locking. The azurerm backend uses Azure Blob Storage’s own capabilities for locking, and use_azuread_auth = true uses Microsoft Entra ID authentication instead of a storage account key that anyone with the key can use. Each docs page recommends versioning on the bucket or container. I would treat that as required, not optional. When a bad apply or a bad hand edit writes the wrong state, versioning is what gets the previous object back.
What to lock down
State holds secrets, as the previous lesson showed, so the bucket is a secrets store. Block public access. Give write access to the CI role and a small group of humans, and read access to fewer still. Turn on server-side encryption. On S3 the docs list what the principal needs: s3:ListBucket on the bucket, s3:GetObject and s3:PutObject on the state key, and, with use_lockfile, those plus s3:DeleteObject on the .tflock object next to it. The lesson on authenticating providers without static keys applies to the backend too, because the backend needs credentials before any provider does.
The bootstrap problem
You want state in a bucket. A bucket is a resource. A resource needs state. This loop has a simple answer: create the bucket outside of the configuration that uses it. People do it three ways: by hand in the console or CLI, with a tiny configuration kept on local state whose file is committed or stored securely, or from a platform-team repository that sets up the bucket, the roles and the first backend for every project. Pick one and write it down. Once the bucket exists, everything else uses it.
Moving later is possible. Change the backend block and run terraform init -migrate-state. Terraform sees the change, offers to copy the current state to the new place, and does so when you confirm. The backend docs recommend copying terraform.tfstate somewhere safe first, and so do I.
Partial configuration
A backend block cannot use input variables, locals or data sources in Terraform. That is awkward when dev and prod use different buckets. The workaround is partial configuration: leave those arguments out of the block, then pass them at init.
terraform init -backend-config="bucket=acme-terraform-state-dev"
terraform init -backend-config=dev.s3.tfbackend
The second form reads key = "value" lines from a file. This is a Terraform restriction, not an OpenTofu one. OpenTofu 1.8 added variables and locals in backend configuration, with limits, so a configuration that relies on that will not work in Terraform. I confirmed this against the OpenTofu 1.8 changelog and the Terraform backend docs.
What changed lately
Two things in the older material are out of date. DynamoDB locking was the standard S3 setup for years, and many videos still show it. As of Terraform 1.11, dynamodb_table is deprecated and the docs say it will be removed in a future minor version. And OpenTofu 1.10 added locking to its S3 backend without DynamoDB. The next lesson goes through both.
About the video
The embed is ProgrammingKnowledge’s “Terraform Remote State Explained: Secure Collaboration & Locking with S3, Azure, GCS Backends”, published October 2025, 25 minutes, no chapters. I picked it because it is the only candidate I found that covers all three backends in one sitting. It is a small channel, and I judged it only from its title and description, which say it spends time on locking, where it may show the older DynamoDB setup. The lesson text above is the corrected version. Travis Roberts’s video in deeper is a clearer walkthrough of one backend, azurerm, from 2022, using an account key. English was judged from the title, description and the English auto-caption track.
Where this fits
A shared bucket gets everyone onto one state. It does nothing yet about two people applying at the same time. That is the job of state locking.
Check yourself
Why can't the same configuration create the bucket and also store its state there?
To plan, Terraform has to read state from the backend, and the bucket does not exist yet. You bootstrap the bucket first, with local state or by hand, then switch to it.
What does `terraform init -migrate-state` do?
After you change the backend block, it copies the existing state to the new backend and asks for confirmation, so the new location starts with your real state and not an empty one.
You want to use the same backend block in `dev` and `prod` with different buckets. What do you do?
Leave the differing arguments out of the block and give them at init time, with `terraform init -backend-config=dev.s3.tfbackend`. The block cannot read a variable.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.