0
0
Terraformcloud~5 mins

State file purpose and structure in Terraform - Commands & Configuration

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. The state file is like a notebook where Terraform writes down what resources it manages and their current details. This helps Terraform know what to add, change, or remove next time you run it.
When you want Terraform to track the resources it created so it can update them safely.
When you need to share Terraform work with your team and keep resource info consistent.
When you want to see what resources Terraform knows about before making changes.
When you want to avoid accidentally deleting or recreating resources by mistake.
When you want to plan changes and see what will happen before applying them.
Commands
This command sets up Terraform in your project folder. It downloads necessary plugins and prepares the environment. It also creates the initial state file if it doesn't exist.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) 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 creates or updates cloud resources as defined in your Terraform files. It also writes the current state of resources into the state file so Terraform remembers them.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips the confirmation prompt to apply changes immediately
This command displays the current state of your infrastructure as recorded in the state file. It helps you see what Terraform knows about your resources.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0abcd1234efgh5678 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = "example-instance" }
Key Concept

If you remember nothing else from this pattern, remember: the state file is Terraform's memory of your cloud resources, enabling safe and accurate updates.

Common Mistakes
Deleting the state file manually from the project folder.
Terraform loses track of existing resources and may try to recreate or delete them unexpectedly.
Never delete the state file manually; use Terraform commands to manage state safely.
Not sharing the state file when working in a team.
Each team member has a different view of resources, causing conflicts and errors.
Use remote state backends like Terraform Cloud or S3 to share state securely among the team.
Editing the state file directly with a text editor.
Manual edits can corrupt the file and cause Terraform to behave unpredictably.
Use Terraform commands like terraform state rm or terraform state mv to modify state safely.
Summary
terraform init prepares your project and creates the state file.
terraform apply creates resources and updates the state file with their details.
terraform show lets you view the current resource state stored in the state file.