Introduction to Terraform State
1. Introduction to Terraform State
Let’s get started with an introduction to terraform state. Terraform state is a metadata repository of your infrastructure configuration. Terraform saves the state of the resources that it manages in a state file. By default, state is stored locally in a file named "terraform.tfstate", but it can also be stored remotely, which is recommended for team environments. Terraform uses the local state to create plans and change your infrastructure. Before any operation, Terraform does a refresh to update the state with the real infrastructure. The primary purpose of Terraform state is to store bindings between objects in a remote system and resource instances declared in your configuration. When Terraform creates a remote object in response to a change of configuration, it will record the identity of that remote object against a particular resource instance. Terraform then potentially updates or deletes that object in response to future configuration changes. Every infrastructure component created within the resource block is identified by its name within the terraform state. When a terraform configuration is applied for the first time, infrastructure resources are created and a state file is automatically generated with a reference to the name mentioned in the resource block. If a resource already has an entry within the Terraform state file, then Terraform compares the configuration with the state file and the current live state. Based on the comparison, a plan is generated. When the plan is applied, it updates the resource to match the configuration defined. Once the configuration is applied, the Terraform state file is updated to reflect the current state of the infrastructure. If arguments cannot be updated in-place due to remote API limitations, then the resource is destroyed and re-created. If a resource is removed from the current configuration but has an entry in the state file, then terraform compares the configuration and destroys the resources that no longer exist.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.