Appearance
Infrastructure as Code
595. What is infrastructure as code, and why use it?
The short answer
Infrastructure as code means describing your servers, networks, databases and permissions in text files, kept in Git, and letting a tool create and change the real infrastructure to match. Instead of clicking in a console, you change a file, review it in a pull request, and apply it. Environments become repeatable, every change is reviewed and recorded, mistakes can be reverted, and a whole environment can be rebuilt from scratch after a disaster.
The old way, and its problems. An engineer clicks through the console to build the production network. Six months later, nobody remembers why a firewall rule exists. Staging was built by someone else, slightly differently. When the region fails, rebuilding takes days of guesswork.
What changes with code:
| Without code | With code |
|---|---|
| Changes by clicking | Changes by pull request |
| No review | Reviewed like application code |
| History in people's heads | History in Git: who, what, when, why |
| Environments drift apart | Environments built from the same code |
| Rebuilding is guesswork | Rebuilding is running the code again |
| Mistakes are hard to undo | Revert the commit, apply again |
What the code looks like, a storage account for the shop's receipts, in Terraform:
hcl
resource "azurerm_storage_account" "receipts" {
name = "stshopreceipts"
resource_group_name = "rg-shop-prod"
location = "westeurope"
account_tier = "Standard"
account_replication_type = "ZRS"
}Read aloud: "there should be a storage account called stshopreceipts, in this resource group, in West Europe, with zone-redundant copies". The tool works out whether to create it, change it, or leave it alone (question 597).
It is a foundation for much else in this module: disaster recovery by rebuilding (question 585), landing zones (question 556), and GitOps for clusters (question 507).
596. What is the difference between declarative and imperative infrastructure tools?
The short answer
A declarative tool takes a description of the end state you want, such as "three servers and one load balancer", and works out the steps itself, by comparing what exists with what you described. An imperative tool runs the steps you write, in order, such as "create a server, then another". Declarative tools are naturally idempotent: running them twice gives the same result as running them once. Most infrastructure tools are declarative, like Terraform, CloudFormation and Bicep, while scripts with the cloud's CLI are imperative.
An example of the difference. You want three web servers.
- Imperative: "create a server" three times. Run the script again and you have six.
- Declarative: "there should be three servers". Run it again and the tool sees three already exist, and does nothing. Change it to four, and it creates exactly one more.
| Declarative | Imperative | |
|---|---|---|
| You write | The desired end state | The steps |
| Running twice | Same result | Steps run again |
| Order of steps | Worked out by the tool | Yours to get right |
| Removing something | Delete it from the code | Write a delete step |
| Examples | Terraform, OpenTofu, CloudFormation, Bicep, Kubernetes manifests | Shell scripts with the cloud CLI, many custom scripts |
Idempotent means doing it many times has the same effect as doing it once (question 570). It is what makes declarative tools safe to run again and again, including from a pipeline on every merge.
Some tools mix both. Pulumi and the AWS CDK let you write real code, in TypeScript or Python, with loops and functions, but what that code produces is a declarative description, which the engine then compares and applies. Ansible is mostly a list of steps, but each step is written to be idempotent (question 608).
597. What is the Terraform workflow, and what do providers, resources and data sources do?
The short answer
You write configuration files that declare resources. terraform init downloads the providers, which are plug-ins that know how to talk to each cloud's API. terraform plan compares your configuration with the state and with reality, and shows exactly what it would create, change or destroy. terraform apply makes those changes and records the result in the state. Resources are things Terraform creates and manages. Data sources only read existing things, such as looking up a network created elsewhere.
A small configuration, a resource group and a storage account that uses an existing network:
hcl
terraform {
required_providers {
azurerm = { source = "hashicorp/azurerm", version = "~> 4.0" } # (1)
}
}
provider "azurerm" {
features {}
}
data "azurerm_subnet" "app" { # (2)
name = "snet-app"
virtual_network_name = "vnet-shop-prod"
resource_group_name = "rg-network"
}
resource "azurerm_resource_group" "shop" { # (3)
name = "rg-shop-prod"
location = "westeurope"
}
resource "azurerm_storage_account" "receipts" {
name = "stshopreceipts"
resource_group_name = azurerm_resource_group.shop.name # (4)
location = azurerm_resource_group.shop.location
account_tier = "Standard"
account_replication_type = "ZRS"
network_rules {
default_action = "Deny"
virtual_network_subnet_ids = [data.azurerm_subnet.app.id] # (5)
}
}- The provider, pinned to version 4.x, so a future major version cannot change behaviour without you choosing it.
- A data source reads a subnet that the network team manages elsewhere. Terraform never changes it.
- A resource, which Terraform creates, changes and can destroy.
- A reference to another resource's attribute. It also tells Terraform the order: the resource group must exist first.
- A reference to the data source, only allowing traffic from the app subnet.
The commands:
| Command | Does |
|---|---|
terraform init | Downloads providers and modules, connects to the state backend |
terraform fmt | Formats files the standard way |
terraform validate | Checks syntax and references |
terraform plan | Shows the changes it would make |
terraform apply | Makes the changes, after confirmation |
terraform destroy | Removes everything this configuration manages |
Read every plan before applying. The plan shows + for create, ~ for change in place, and -/+ for destroy and recreate. A -/+ on a database means its data is about to be deleted. That one symbol is worth stopping for (question 604).
598. What is Terraform state, and why does Terraform need it?
The short answer
The state file is Terraform's record of which real resources belong to which blocks in your code, with their IDs and last known settings. Terraform needs it to know what it manages: to tell "this resource was removed from the code, so destroy it" from "this resource was never mine". It also stores attributes for references, and speeds up planning. Because it maps code to real things, and may contain secrets, the state must be stored safely, shared, locked during changes, and never edited by hand.
What the state links together:
| In the code | In the state | In the cloud |
|---|---|---|
azurerm_storage_account.receipts | ID /subscriptions/…/stshopreceipts, and its settings | The real storage account |
Why it cannot simply look at the cloud instead:
- Ownership. The cloud has thousands of resources. Only the state says which ones this configuration created and manages.
- Deletions. When you delete a block from the code, Terraform can only destroy the matching resource if it remembers which one that was.
- Renames and IDs. Many resources are known by generated IDs, not by names.
- Speed. Reading every attribute of everything on each plan is slow for big setups.
What is inside it. A JSON file listing every resource, with its attributes. That often includes sensitive values, such as database passwords or generated keys, in plain text (question 606).
Rules for state:
- Remote and shared, never only on a laptop (question 599).
- Locked while someone applies.
- Encrypted and access-controlled, since it may contain secrets.
- Versioned, so a damaged state can be restored.
- Changed only through Terraform commands, such as
terraform state mv, never by editing the JSON.
599. How do remote state and state locking work?
The short answer
Remote state keeps the state file in shared storage, such as an S3 bucket, an Azure storage container, or HCP Terraform, instead of on one person's laptop, so the whole team and the pipeline use the same one. Locking makes sure only one apply runs at a time: the first run takes a lock, and any other run waits or fails until it is released. Without locking, two people applying at once could each write their own version of the state, and one set of changes would be lost from it.
A backend for Azure, storing state in a storage account:
hcl
terraform {
backend "azurerm" {
resource_group_name = "rg-tfstate"
storage_account_name = "sttfstateshop"
container_name = "tfstate"
key = "prod/network.tfstate"
use_azuread_auth = true
}
}The key is the path of this state file inside the container. Each separate configuration gets its own key. Locking works automatically here, using a lease on the blob, a short exclusive claim that storage gives to one writer. use_azuread_auth means Terraform signs in with an identity rather than the storage account key.
On AWS, the S3 backend now locks with a lock file in the same bucket, using use_lockfile = true, available since Terraform 1.10. Older setups used a DynamoDB table for locks, which is now deprecated.
Protect the backend:
- Turn on versioning and soft delete, so an earlier state can be restored.
- Restrict access to the pipeline and a few admins.
- Keep it in a separate account or subscription from the resources it describes, so a mistake in production cannot destroy its own state.
Split state into pieces. One giant state for everything means slow plans, a lock that blocks everyone, and a huge blast radius. Split by layer and environment: network, shared services and each application, per environment (question 602).
600. What is configuration drift, and how do you detect and handle it?
The short answer
Drift is when the real infrastructure no longer matches the code, usually because someone changed something by hand in the console, or another tool changed it. Terraform detects it during plan, which reads the real resources and shows the difference. Run plans on a schedule to catch it early. Then decide case by case: either put the manual change into the code so it becomes official, or apply the code to undo it. Prevent it by removing write access to production from people, and making the pipeline the only way to change things.
A scenario. During an incident at 2 a.m., Ravi opens port 8443 on a network security group in the portal. The next morning, an unrelated pull request's plan shows:
text
# azurerm_network_security_group.app will be updated in-place
~ resource "azurerm_network_security_group" "app" {
- security_rule {
- destination_port_range = "8443"
...
}
}Terraform wants to remove the rule, because it is not in the code. Applying blindly might break whatever the rule fixed.
How to handle it:
- Find out why the change was made, from the audit log and the person (question 555).
- If it should stay, add it to the code in a pull request. The next plan shows no change.
- If it should go, apply the code, which removes it.
- Never ignore it. A plan that always shows "unexpected" changes trains people to stop reading plans.
Detecting drift regularly. A scheduled pipeline job runs every night:
bash
terraform plan -detailed-exitcode -lock=false-detailed-exitcode makes the command exit with code 0 when nothing differs, 1 on an error, and 2 when there are changes. Code 2 raises an alert. -lock=false lets this read-only check run without blocking real applies.
terraform plan -refresh-only shows only what changed outside Terraform, and terraform apply -refresh-only accepts those changes into the state without changing any resource.
Preventing it:
- Read-only access to production for people, with break-glass access for emergencies that is logged and reviewed (question 271).
- Fix forward through code, even in incidents, when the pipeline is fast enough.
ignore_changesfor attributes another system legitimately manages, such as an autoscaler changing instance counts (question 604).
601. What are Terraform modules, and how should you design them?
The short answer
A module is a folder of Terraform code with inputs, called variables, and outputs, that you can call many times, like a function. Teams use modules to package a good, standard version of something, such as "a storage account with private endpoint, logging and no public access", so every team gets it right without copying code. Design modules around one clear purpose, with a small number of meaningful inputs, safe defaults, outputs others need, and version them so changes can be adopted deliberately.
Calling a module, for the shop's receipts storage:
hcl
module "receipts_storage" {
source = "git::https://git.shop.example/platform/tf-azure-storage.git?ref=v2.3.0" # (1)
name = "stshopreceipts" # (2)
resource_group = azurerm_resource_group.shop.name
location = "westeurope"
private_subnet = data.azurerm_subnet.app.id
}
output "receipts_url" {
value = module.receipts_storage.primary_blob_endpoint # (3)
}- The source and version. A Git tag,
v2.3.0, pins the exact module version. Upgrading is a deliberate change in a pull request. - Inputs. Only what the caller must decide. Everything else, such as zone redundancy, TLS 1.2 and blocked public access, is built in.
- Outputs expose what callers need, here the storage address.
Inside the module, the folder usually holds:
text
tf-azure-storage/
main.tf resources
variables.tf inputs, with types, descriptions and validation
outputs.tf values for callers
versions.tf required Terraform and provider versions
README.md how to use itDesign rules:
| Do | Avoid |
|---|---|
| One clear purpose per module | A "everything" module with 80 inputs |
| Secure, sensible defaults | Making callers remember every safety setting |
| Typed inputs with validation | Free-form strings checked nowhere |
| Semantic version tags | Pointing at the main branch |
| Few layers of nesting | Modules inside modules inside modules |
Validation rejects bad inputs at plan time:
hcl
variable "name" {
type = string
validation {
condition = can(regex("^st[a-z0-9]{3,22}$", var.name))
error_message = "Name must start with 'st' and use 3 to 22 lowercase letters or digits."
}
}Public registries offer modules for common things. Read them before using them: a module runs with your permissions.
602. How do you manage several environments with Terraform?
The short answer
Give each environment its own state, so a mistake in development cannot touch production. The most common layout is one folder per environment, each calling the same versioned modules with different settings. Terraform workspaces, which keep several states for one configuration, work for short-lived or identical copies, but hide which environment you are in and make different settings awkward. Separate accounts or subscriptions per environment, and separate pipeline credentials, make the wall real.
A typical layout:
text
infra/
modules/
app/ shared module: the app's resources
envs/
dev/
main.tf calls modules/app with small sizes
backend.tf state key: dev/app.tfstate
staging/
main.tf
backend.tf state key: staging/app.tfstate
prod/
main.tf calls modules/app with large sizes and zones
backend.tf state key: prod/app.tfstateEach environment folder is a small file of settings:
hcl
module "app" {
source = "../../modules/app"
env = "prod"
instance_size = "Standard_D4s_v5"
min_replicas = 3
zones = ["1", "2", "3"]
}| One folder per environment | Workspaces | |
|---|---|---|
| Separate state | Yes | Yes |
| Different settings | Natural | Through conditions on terraform.workspace |
| Which environment am I in? | Obvious from the folder | Easy to forget; check terraform workspace show |
| Different backends or credentials | Easy | Hard: one backend for all |
| Good for | Long-lived environments | Temporary copies, such as one per feature branch |
Promoting a change through environments: change the module, release a new version, then update dev, then staging, then prod, each in its own pull request with its own plan. Production only gets a module version that already worked twice, the same idea as promoting one image across environments (question 134).
Tools that reduce repetition, such as Terragrunt, generate the backend and common settings for each folder. They help in large estates, at the cost of one more tool to learn.
603. What is the difference between count and for_each, and how does Terraform decide the order of changes?
The short answer
Both create several copies of a resource from one block. count makes a numbered list, so copies are known as 0, 1, 2, and removing one from the middle shifts the numbers and can destroy and recreate the others. for_each makes copies keyed by name, from a map or set, so adding or removing one touches only that one. Prefer for_each for anything with an identity. Terraform orders changes from the references between resources, building a dependency graph, and runs independent changes in parallel.
The count trap. Three subnets, made with count:
hcl
variable "subnets" { default = ["web", "app", "db"] }
resource "azurerm_subnet" "this" {
count = length(var.subnets)
name = var.subnets[count.index]
# ...
}They are stored as this[0] web, this[1] app, this[2] db. Remove "web" from the list, and now this[0] should be app and this[1] db. Terraform plans to change or replace this[0] and this[1], and destroy this[2]. For subnets with machines in them, that is a disaster.
The same with for_each:
hcl
variable "subnets" {
default = {
web = "10.20.0.0/24"
app = "10.20.1.0/24"
db = "10.20.2.0/24"
}
}
resource "azurerm_subnet" "this" {
for_each = var.subnets
name = each.key
address_prefixes = [each.value]
# ...
}They are stored as this["web"], this["app"], this["db"]. Removing web destroys only this["web"].
count | for_each | |
|---|---|---|
| Copies known by | Number | Key |
| Removing one in the middle | Shifts the others | Touches only that one |
| Good for | Zero or one, an on and off switch: count = var.enabled ? 1 : 0 | Anything with a name |
Ordering. Terraform builds a graph from references. The storage account refers to the resource group's name, so the group is created first, and destroyed last. Things that do not refer to each other are created in parallel.
depends_on adds an order that references cannot express, such as "do not create this app until that role assignment exists", when the app never refers to the role directly. Use it rarely: most dependencies should come from real references.
604. How do lifecycle rules work, and how do you safely replace or protect a resource?
The short answer
A lifecycle block changes how Terraform treats one resource. prevent_destroy makes any plan that would destroy it fail, which protects databases and state storage. create_before_destroy builds the replacement before removing the old one, avoiding gaps. ignore_changes tells Terraform to leave certain attributes alone when something else manages them. To deliberately rebuild one resource, run terraform apply -replace with its address. The older terraform taint command is deprecated in its favour.
hcl
resource "azurerm_postgresql_flexible_server" "orders" {
name = "psql-orders-prod"
# ...
lifecycle {
prevent_destroy = true # (1)
ignore_changes = [storage_mb] # (2)
}
}
resource "azurerm_linux_virtual_machine_scale_set" "web" {
# ...
lifecycle {
create_before_destroy = true # (3)
}
}prevent_destroy: a plan that would destroy the orders database stops with an error. Someone must remove this line on purpose, in a reviewed change, first.ignore_changes: storage grows automatically, so Terraform should not try to shrink it back to the number in the code.create_before_destroy: when the scale set must be replaced, the new one is built first, so traffic always has somewhere to go.
Why plans replace things. Some settings cannot be changed on a live resource, such as a database's name, a storage account's name, or a virtual machine's zone. Changing them in code produces -/+ destroy and then create replacement, and the plan marks the attribute with # forces replacement. Always look for that line before applying.
Forcing a rebuild of a broken machine:
bash
terraform apply -replace='azurerm_linux_virtual_machine.worker["eu1"]'Terraform plans to destroy and recreate exactly that one resource, and nothing else.
Protect against deletion outside Terraform too. prevent_destroy only stops Terraform. Cloud-side protection, such as Azure resource locks, AWS deletion protection on databases, and key vault purge protection, also stops a person or another tool.
605. How do you bring existing resources under Terraform, or rename them without destroying them?
The short answer
To adopt a resource created by hand, write an import block with the resource's real ID and its address in your code, and let Terraform generate or check the matching configuration in the plan. To rename or move a resource inside your code, such as into a module, add a moved block saying the old address is now the new one, so Terraform updates the state instead of destroying and recreating it. To stop managing something without deleting it, use a removed block.
Importing, with an import block, available since Terraform 1.5:
hcl
import {
to = azurerm_storage_account.legacy_uploads
id = "/subscriptions/1111.../resourceGroups/rg-shop/providers/Microsoft.Storage/storageAccounts/stshopuploads"
}Then:
bash
terraform plan -generate-config-out=generated.tfTerraform reads the real resource and writes a starting configuration for it into generated.tf. Tidy that file, move it next to your code, and run plan until it shows the import and no other changes. Apply records the resource in the state, without changing it. Because the import is in code, it is reviewed in a pull request like any other change, unlike the older terraform import command.
Renaming, with a moved block:
hcl
moved {
from = azurerm_storage_account.receipts
to = module.receipts_storage.azurerm_storage_account.this
}Without it, moving the resource into a module looks to Terraform like "delete the old one, create a new one". With it, the plan says the resource has moved, and nothing in the cloud changes.
Letting go, with a removed block, since Terraform 1.7:
hcl
removed {
from = azurerm_storage_account.old_logs
lifecycle {
destroy = false
}
}Terraform forgets the resource but leaves it running, for example when another team takes ownership.
Older commands do the same through the command line: terraform state mv and terraform state rm. They change the state immediately, without a review, so the code blocks are preferred.
606. How do you handle secrets in Terraform and its state?
The short answer
Never write secrets in .tf files or variable files committed to Git. Read them at plan time from a secret store with a data source, pass them through environment variables in the pipeline, or better, let the cloud generate them and store them straight into a key vault. Remember that the state file may still contain secret values in plain text, so protect the state as a secret itself: encrypted storage, tight access, and no copies on laptops. Marking variables sensitive only hides them from output, not from the state.
What sensitive = true does, and does not do:
hcl
variable "db_admin_password" {
type = string
sensitive = true
}| Where the value appears | Hidden? |
|---|---|
| Plan and apply output | Yes |
| Pipeline logs of Terraform's output | Yes |
| The state file | No |
Better patterns, from best:
- No secret at all. Use managed identities and Entra ID or IAM authentication for the database, so there is no password to manage (question 582).
- Let the cloud or the database generate it and store it directly in the key vault. Some managed databases can keep the admin password in the secret store for you, such as RDS's managed master password in Secrets Manager.
- Generate it once and write it straight to the key vault, so apps read it from there (question 553). Its value is still in state, so the state must be protected.
- Newer Terraform versions add ephemeral values and write-only arguments that are used during apply but never saved in state. Use them where the provider supports them.
Protect the state like any secret store: encrypted at rest, accessed only by the pipeline identity and a few admins, logged, and never downloaded to laptops (question 599). OpenTofu, an open-source fork of Terraform, can also encrypt the state file itself.
Scan for mistakes. Secret scanners in the pipeline catch passwords committed in .tfvars files before they are merged (question 148).
607. How should Terraform run in a CI/CD pipeline?
The short answer
A pull request triggers formatting checks, validation, security and policy scans, and a plan, and the plan is posted on the pull request for reviewers to read. After approval and merge, the pipeline runs apply using that reviewed plan, with an environment approval for production. The pipeline signs in to the cloud with short-lived federated credentials, not stored keys, and runs are serialised per state so two applies never race. People keep read-only access, so the pipeline is the only way to change infrastructure.
The pull request stage:
terraform fmt -checkandterraform validate.- Security scans of the configuration, such as Checkov or Trivy, for things like public storage or open firewall rules (question 215).
- Policy checks against company rules (question 609).
terraform plan -out=tfplan, with the readable plan posted as a comment.
The apply stage, after merge:
- Sign in with OIDC federation, a short-lived token instead of stored keys (question 269).
- Wait for a manual approval on production environments (question 246).
terraform apply tfplan, applying exactly the reviewed plan. If the saved plan is out of date because something changed since, Terraform refuses it, and a new plan is needed.
Practical rules:
- Serialise runs per state with pipeline concurrency settings, so two merges do not race. The state lock is the last line of defence, not the first.
- Separate identities per environment. The dev pipeline's identity cannot touch production.
- Plan on a schedule for drift detection (question 600).
- Show cost changes. Tools such as Infracost add the monthly cost difference to the pull request.
Dedicated services, such as HCP Terraform, Spacelift or Atlantis, provide this whole flow ready-made.
608. How do Terraform, OpenTofu, CloudFormation, Bicep, Pulumi, and Ansible compare?
The short answer
Terraform and its open-source fork OpenTofu are declarative, work with almost every cloud and service through providers, and keep their own state. CloudFormation for AWS and Bicep for Azure are the clouds' own declarative tools, with state managed by the cloud. Pulumi and the AWS CDK let you write infrastructure in real programming languages. Ansible is mainly for configuring what runs inside machines, step by step and idempotently, rather than for creating cloud resources, though it can do both.
| Tool | Style | Clouds | State | Language |
|---|---|---|---|---|
| Terraform | Declarative | Almost any, through providers | Your state file | HCL |
| OpenTofu | Declarative | Same as Terraform | Your state file, optionally encrypted | HCL |
| CloudFormation | Declarative | AWS | Managed by AWS, as stacks | YAML or JSON |
| AWS CDK | Code that produces CloudFormation | AWS | Managed by AWS | TypeScript, Python and more |
| Bicep | Declarative | Azure | Managed by Azure | Bicep |
| Pulumi | Code, with a declarative engine | Almost any | Pulumi's service or your storage | TypeScript, Python, Go, C# |
| Ansible | Steps, each idempotent | Any, through modules | None | YAML playbooks |
Why OpenTofu exists. In 2023, HashiCorp changed Terraform's licence from open source to the Business Source License, which limits some commercial uses. The community forked the last open version as OpenTofu, now under the Linux Foundation. The two are still very similar for everyday use.
Provisioning versus configuration management:
- Provisioning creates the infrastructure: networks, machines, databases. Terraform's strength.
- Configuration management sets up the inside of machines: packages, files, services, users. Ansible's strength, along with Chef and Puppet.
With containers and golden images, much configuration management moved into Dockerfiles and image builds (question 567). Ansible is still common for existing fleets of servers and network devices.
How to choose:
- Several clouds or many SaaS services, such as DNS, monitoring and Git hosting, together: Terraform or OpenTofu.
- Only Azure, and a team that likes the native tool: Bicep.
- Only AWS: CloudFormation or CDK are natural, and Terraform is equally common.
- A team of developers who want loops, types and tests in a real language: Pulumi or CDK.
609. What is policy as code for infrastructure?
The short answer
Policy as code means writing your infrastructure rules, such as "no public storage", "all resources must have an owner tag" or "only these regions", as code that runs automatically. Rules run at two points. In the pipeline, tools such as OPA with Conftest, Checkov or Sentinel check the Terraform plan before anything is applied. In the cloud itself, Azure Policy or AWS service control policies enforce the rules, whatever tool or person makes the change. Together they catch mistakes early and still stop anything that slips through.
| Where | Tool examples | Catches | Can be bypassed by |
|---|---|---|---|
| Pull request, on the plan | Conftest with OPA, Checkov, Trivy, Sentinel | Mistakes before merge, with clear feedback | Anyone changing the cloud directly |
| Cloud, on every change | Azure Policy, AWS service control policies, AWS Config rules | Everything, from any source | Nobody below the guardrail |
A rule in Rego, the policy language of OPA, the Open Policy Agent, checked against the JSON form of a Terraform plan:
text
package terraform
deny contains msg if {
some r in input.resource_changes # (1)
r.type == "azurerm_storage_account" # (2)
r.change.after.public_network_access_enabled == true # (3)
msg := sprintf("%s must not allow public network access", [r.address]) # (4)
}- Look at every resource the plan will create or change.
- Only storage accounts.
- After the change, would public network access be on?
- Then deny, with a message naming the resource.
The pipeline produces the JSON with terraform show -json tfplan > plan.json, then runs conftest test plan.json. A failure blocks the merge, and the message tells the author what to fix.
Rolling out new rules gently: start in warning mode, fix what already breaks the rule, then switch to blocking. The same approach applies to cloud guardrails and Kubernetes policies (questions 556 and 500).
610. A Terraform apply failed halfway and the state is locked; how do you recover?
The short answer
First make sure no apply is still running, because removing the lock under a live run can corrupt the state. If the run really died, release the lock with terraform force-unlock and the lock ID. Then run plan: Terraform saves the state for whatever it completed, so the plan shows only what is left, or what failed. Fix the cause of the failure, such as a missing permission or a name already taken, and apply again. If the state itself is damaged, restore the previous version from the backend's version history.
Step by step:
- Check nothing is running. Look at the pipeline for a run still in progress, and ask the team. The lock error shows who holds it and since when:
text
Error: Error acquiring the state lock
Lock Info:
ID: 6f0c2a1e-...
Path: tfstate/prod/app.tfstate
Who: runner@ci-agent-7
Created: 2026-10-27 09:14:03 UTC- Release a stale lock, only when sure:
bash
terraform force-unlock 6f0c2a1e-...- Plan again. Terraform writes state as resources finish, so completed work is recorded. The plan shows what is still missing or wrong.
- Fix the real error. Common ones:
| Error | Fix |
|---|---|
403 or authorization failed | Give the pipeline identity the missing role (question 549) |
| Name already exists | Pick another name, or import the existing resource (question 605) |
| Quota exceeded | Request more quota, or use a smaller size |
| Timed out | Check the resource in the console; it may have finished, so plan again |
- Apply again.
When a resource was created but not recorded, for example the process was killed right after the cloud call, the next plan tries to create it again and fails because it already exists. Import it into the state, then plan again.
When the state file itself is broken or wrong, restore the previous version from the backend's versioning, such as blob versions or S3 object versions, then plan to see the difference from reality. This is why state backends must have versioning turned on (question 599).
Never edit the state JSON by hand, and never delete the lock in storage directly while unsure whether a run is alive.