github.com/hugorut/terraform@v1.1.3/website/docs/language/state/index.mdx (about) 1 --- 2 page_title: State 3 description: >- 4 An introduction to state, information that Terraform uses to map resources to 5 a configuration, track metadata, and improve performance. 6 --- 7 8 # State 9 10 Terraform must store state about your managed infrastructure and 11 configuration. This state is used by Terraform to map real world 12 resources to your configuration, keep track of metadata, and to improve 13 performance for large infrastructures. 14 15 This state is stored by default in a local file named "terraform.tfstate", 16 but it can also be stored remotely, which works better in a team environment. 17 18 Terraform uses this local state to create plans and make changes to your 19 infrastructure. Prior to any operation, Terraform does a 20 [refresh](/cli/commands/refresh) to update the state with the 21 real infrastructure. 22 23 The primary purpose of Terraform state is to store bindings between objects in 24 a remote system and resource instances declared in your configuration. 25 When Terraform creates a remote object in response to a change of configuration, 26 it will record the identity of that remote object against a particular 27 resource instance, and then potentially update or delete that object in 28 response to future configuration changes. 29 30 For more information on why Terraform requires state and why Terraform cannot 31 function without state, please see the page [state purpose](/language/state/purpose). 32 33 ## Inspection and Modification 34 35 While the format of the state files are just JSON, direct file editing 36 of the state is discouraged. Terraform provides the 37 [terraform state](/cli/commands/state) command to perform 38 basic modifications of the state using the CLI. 39 40 The CLI usage and output of the state commands is structured to be 41 friendly for Unix tools such as grep, awk, etc. Additionally, the CLI 42 insulates users from any format changes within the state itself. The Terraform 43 project will keep the CLI working while the state format underneath it may 44 shift. 45 46 Terraform expects a one-to-one mapping between configured resource instances 47 and remote objects. Normally that is guaranteed by Terraform being the one 48 to create each object and record its identity in the state, or to destroy 49 an object and then remove the binding for it. 50 51 If you add or remove bindings in the state by other means, such as by importing 52 externally-created objects with `terraform import`, or by asking Terraform to 53 "forget" an existing object with `terraform state rm`, you'll then need to 54 ensure for yourself that this one-to-one rule is followed, such as by manually 55 deleting an object that you asked Terraform to "forget", or by re-importing it 56 to bind it to some other resource instance. 57 58 ## Format 59 60 State snapshots are stored in JSON format and new Terraform versions are 61 generally backward compatible with state snapshots produced by earlier versions. 62 However, the state format is subject to change in new Terraform versions, so 63 if you build software that parses or modifies it directly you should expect 64 to perform ongoing maintenance of that software as the state format evolves 65 in new versions. 66 67 Alternatively, there are several integration points which produce JSON output 68 that is specifically intended for consumption by external software: 69 70 * [The `terraform output` command](/cli/commands/output) 71 has a `-json` option, for obtaining either the full set of root module output 72 values or a specific named output value from the latest state snapshot. 73 * [The `terraform show` command](/cli/commands/show) has a `-json` 74 option for inspecting the latest state snapshot in full, and also for 75 inspecting saved plan files which include a copy of the prior state at the 76 time the plan was made. 77 78 A typical way to use these in situations where Terraform is running in 79 automation is to run them immediately after a successful `terraform apply` 80 to obtain a representation of the latest state snapshot, and then store that 81 result as an artifact associated with the automated run so that other software 82 can potentially consume it without needing to run Terraform itself.