teluzia.Cloud Engineer · Day 92
CHAPTER 14 · ADVANCED TERRAFORM

Remote state and locking

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Remote state is a shared inventory cabinet; locking is the sign-out system preventing two engineers from editing it simultaneously. The S3 backend stores state at a configured bucket and key. The bucket must already exist before backend initialization. Protect access and enable versioning for recovery. Backend credentials should come from supported secure credential mechanisms rather than hardcoded secrets in configuration or copied plan files. Current Terraform supports S3 state locking with use_lockfile = true; it is opt-in. The lock object uses the state key plus .tflock and needs GetObject, PutObject, and DeleteObject permission. DynamoDB locking is deprecated, so teach migration and compatibility explicitly rather than assume it is required.

Think of it this way

Remote state is a shared inventory cabinet; locking is the sign-out system preventing two engineers from editing it simultaneously.

TOPIC 01 / 03Step by step

S3 backend and versioning

The S3 backend stores state at a configured bucket and key. The bucket must already exist before backend initialization. Protect access and enable versioning for recovery. Backend credentials should come from supported secure credential mechanisms rather than hardcoded secrets in configuration or copied plan files. For example, an environment uses key dev/network.tfstate in a restricted versioned bucket separate from its managed application bucket.

WORKED EXAMPLE
An environment uses key dev/network.tfstate in a restricted versioned bucket separate from its managed application bucket.

Two engineers need one authoritative state for a network project. They configure an existing restricted S3 bucket and a specific key, then initialize the backend deliberately. State reads and writes now use that location rather than each laptop's independent inventory. The backend bucket should not be owned by the same disposable application stack if its deletion would erase management data.

A backend is the state storage and coordination mechanism. A key is the S3 object path chosen for that state. Versioning retains earlier object versions for recovery. Before migration, stop concurrent writers, securely back up state, verify destination account and access, and review the migration procedure. Remote storage improves collaboration but adds availability and permission dependencies. Access to state can expose sensitive attributes even without access to the application resource itself. Separate environment keys and preferably stronger account/access boundaries where needed; a path named prod is not a protection unless policies enforce it.

Why this matters

An environment uses key dev/network.tfstate in a restricted versioned bucket separate from its managed application bucket.

A common trap

Do not overlook this operational distinction: Backend credentials should come from supported secure credential mechanisms rather than hardcoded secrets in configuration or copied plan files.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

S3 backend and versioning

The S3 backend stores state at a configured bucket and key. The bucket must already exist before backend initialization. Protect access and enable versioning for recovery. Backend credentials should come from supported secure credential mechanisms rather than hardcoded secrets in configuration or copied plan files. For example, an environment uses key dev/network.tfstate in a restricted versioned bucket separate from its managed application bucket.

WORKED EXAMPLE
An environment uses key dev/network.tfstate in a restricted versioned bucket separate from its managed application bucket.

Two engineers need one authoritative state for a network project. They configure an existing restricted S3 bucket and a specific key, then initialize the backend deliberately. State reads and writes now use that location rather than each laptop's independent inventory. The backend bucket should not be owned by the same disposable application stack if its deletion would erase management data.

A backend is the state storage and coordination mechanism. A key is the S3 object path chosen for that state. Versioning retains earlier object versions for recovery. Before migration, stop concurrent writers, securely back up state, verify destination account and access, and review the migration procedure. Remote storage improves collaboration but adds availability and permission dependencies. Access to state can expose sensitive attributes even without access to the application resource itself. Separate environment keys and preferably stronger account/access boundaries where needed; a path named prod is not a protection unless policies enforce it.

Why this matters

An environment uses key dev/network.tfstate in a restricted versioned bucket separate from its managed application bucket.

A common trap

Do not overlook this operational distinction: Backend credentials should come from supported secure credential mechanisms rather than hardcoded secrets in configuration or copied plan files.

Native S3 lock files

Current Terraform supports S3 state locking with use_lockfile = true; it is opt-in. The lock object uses the state key plus .tflock and needs GetObject, PutObject, and DeleteObject permission. DynamoDB locking is deprecated, so teach migration and compatibility explicitly rather than assume it is required. For example, the backend sets use_lockfile = true and the role may manage dev/network.tfstate.tflock while lacking DeleteObject on the actual state file.

WORKED EXAMPLE
The backend sets use_lockfile = true and the role may manage dev/network.tfstate.tflock while lacking DeleteObject on the actual state file.

A Terraform operation using native S3 locking first attempts to create the lock object for its state key. Another operation using the same state sees the active lock and cannot safely proceed as a competing writer. At completion, the first operation releases the lock. Current configuration enables this with use_lockfile = true, which is not the default.

The lock object ends in .tflock. State storage preserves mappings; locking coordinates concurrent modifications. The role needs GetObject, PutObject, and DeleteObject on the lock object, while deleting the actual state object is not normally required for this mechanism. If lock deletion is denied, an operation may leave a lock despite useful work finishing. DynamoDB locking is deprecated in current Terraform guidance; older-client migration needs explicit compatibility planning. Locking does not protect against a wrong plan, compromised credentials, or application data deletion. It is one narrow coordination control, not universal infrastructure safety.

Why this matters

The backend sets use_lockfile = true and the role may manage dev/network.tfstate.tflock while lacking DeleteObject on the actual state file.

A common trap

Do not overlook this operational distinction: DynamoDB locking is deprecated, so teach migration and compatibility explicitly rather than assume it is required.

Lock recovery and migration

A lock protects concurrent operations, not every cause of data corruption or bad configuration. A failed operation can leave a lock needing investigation. Force-unlock only when certain no active process owns it; backend migration needs a backup, verified destination, and deliberate initialization procedure. For example, an operator checks the owning job and process before releasing an abandoned lock rather than interrupting a teammate’s apply.

WORKED EXAMPLE
An operator checks the owning job and process before releasing an abandoned lock rather than interrupting a teammate’s apply.

An apply job loses connectivity and its lock remains. A second engineer sees the lock and should first identify the owning operation, job, and process. If it is still active, unlocking can permit two writers and corrupt operational assumptions. Only after establishing that the operation stopped should an authorized recovery release an abandoned lock and inspect resulting state.

A stale lock is one no active operation still owns. Force-unlock is a recovery action, not a normal performance shortcut. State migration changes backend location under a controlled workflow. Before migration, confirm secure backups, stop writers, verify destination, and use the supported initialization migration behavior. Afterward, inspect state addresses and a reviewed plan, expecting no accidental recreation. A restored earlier state version may no longer match real resources changed afterward, so restoration requires reconciliation. Never confuse recoverable state history with a rollback of the actual infrastructure or stored business data.

Why this matters

An operator checks the owning job and process before releasing an abandoned lock rather than interrupting a teammate’s apply.

A common trap

Do not overlook this operational distinction: Force-unlock only when certain no active process owns it; backend migration needs a backup, verified destination, and deliberate initialization procedure.

KEEP THESE WITH YOU

Today’s takeaways

S3 backend and versioning: The S3 backend stores state at a configured bucket and key. The bucket must already exist before backend initialization. Protect access and enable versioning for recovery. Backend credentials should come from supported secure credential mechanisms rather than hardcoded secrets in configuration or copied plan files.

Native S3 lock files: Current Terraform supports S3 state locking with use_lockfile = true; it is opt-in. The lock object uses the state key plus .tflock and needs GetObject, PutObject, and DeleteObject permission. DynamoDB locking is deprecated, so teach migration and compatibility explicitly rather than assume it is required.

Lock recovery and migration: A lock protects concurrent operations, not every cause of data corruption or bad configuration. A failed operation can leave a lock needing investigation. Force-unlock only when certain no active process owns it; backend migration needs a backup, verified destination, and deliberate initialization procedure.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed