0
0
Terraformcloud~5 mins

Why state is essential in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When you use Terraform to create or change cloud resources, it needs to remember what it has done before. This memory is called the state. Without this state, Terraform would not know what resources exist or what changes to make next.
When you want Terraform to track the resources it created so it can update or delete them later.
When you need to share the current infrastructure setup with your team safely.
When you want to avoid creating duplicate resources by mistake.
When you want Terraform to plan changes accurately before applying them.
When you want to keep your infrastructure consistent and avoid manual errors.
Commands
This command sets up Terraform in your project folder and prepares it to manage state and resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do by comparing the current state with your configuration files. It uses the state to know what exists.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-12345678" + instance_type = "t2.micro" } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes to create or update resources and saves the new state so Terraform remembers the current setup.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Creation complete after 10s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command displays the current state of your infrastructure as Terraform knows it from the saved state file.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0abcd1234efgh5678 ami = ami-12345678 instance_type = t2.micro
Key Concept

If you remember nothing else from this pattern, remember: Terraform state is the memory that tracks your real infrastructure so Terraform can manage changes safely and correctly.

Common Mistakes
Deleting the state file manually or losing it.
Terraform loses track of what resources it manages, causing it to try to recreate or delete resources incorrectly.
Always keep the state file safe and use remote state storage for team collaboration.
Not running terraform init before other commands.
Terraform cannot set up the backend or state management, so commands fail or behave unexpectedly.
Always run terraform init first in a new or cloned project.
Editing the state file manually.
Manual changes can corrupt the state and cause Terraform to mismanage resources.
Use Terraform commands like terraform state or terraform import to modify state safely.
Summary
terraform init prepares Terraform and its state management.
terraform plan uses the state to show what changes will happen.
terraform apply makes changes and updates the state to remember them.
terraform show displays the current known infrastructure from the state.