liveBook · Manning

Terraform state file

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A Terraform state file is a local or remote file that records the state of infrastructure managed by Terraform, including associated resource values.

A Terraform state file records the infrastructure managed by Terraform, including resource details and associated values. After the first terraform apply, Terraform creates terraform.tfstate beside the configuration files. section 7.8.7

Each later terraform apply loads the state, refreshes it from live infrastructure, and uses both sources to make changes efficiently. The state distinguishes infrastructure managed by this project from resources created by other projects or by manual means. section 7.8.7

Inspecting a state file

Run cat terraform.tfstate on Unix-like systems or type terraform.tfstate on Windows. A small state file can show a managed azurerm_resource_group, its provider, resource ID, location, name, and other attributes. section 7.8.7

Treat state as sensitive operational data. Requesting a sensitive Terraform output hides it in terminal output, but the value can still appear in plain text inside terraform.tfstate; automated deployment pipelines should remove local state when finished. section 7.9.4

Production workflows require deliberate state placement:

  • Keep separate state for development, test, and production so updates affect the correct environment. section 12.2.6
  • Store state in cloud storage such as Azure Storage and select it by environment name when Terraform runs in CI/CD. section 12.2.6
{
  "version": 4,
  "terraform_version": "1.5.6",
  "serial": 1,
  "lineage": "b10f693f-e27f-f223-a8ef-011948c56f9c",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "azurerm_resource_group",
      "name": "flixtube",
      "provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "/subscriptions/snip/resourceGroups/flixtube",
            "location": "eastus",
            "managed_by": "",
            "name": "flixtube",
            "tags": null,
            "timeouts": null
          },
          "sensitive_attributes": [],
          "private": snip"
        }
      ]
    }
  ],
  "check_results": null
}

Browse a representative terraform.tfstate document to connect its JSON structure with managed resources and recorded attributes. section 7.8.7

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Explains creation, refresh, ownership, inspection, sensitive storage, and remote multi-environment use without detailing state commands or locking.
Examples
Shows cat or type inspection, an Azure resource group JSON record, plaintext sensitive output, and environment-specific cloud storage.
What it assumes
Assumes Terraform configuration and apply usage, while explaining state from its first generated file and basic command-line inspection.
What you can do after
Operate Terraform state by inspecting records, protecting sensitive values, separating environments, and configuring remote CI/CD storage.
How it connects
Connects Terraform state with infrastructure as code, live infrastructure, Terraform outputs, CI/CD pipelines, and cloud storage.

Also explained in

Compare how 2 books explain Terraform state file →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.