0
0
Terraformcloud~30 mins

Why state is essential in Terraform - See It in Action

Choose your learning style9 modes available
Why state is essential
📖 Scenario: You are managing cloud resources using Terraform. Terraform keeps track of your resources using a special file called the state file. This file helps Terraform know what resources exist and what changes to make.
🎯 Goal: Build a simple Terraform configuration that creates a resource and understand how the state file tracks this resource.
📋 What You'll Learn
Create a Terraform configuration with one AWS S3 bucket resource
Initialize Terraform to create the state file
Add a variable to configure the bucket name
Use Terraform commands to apply the configuration and update the state
💡 Why This Matters
🌍 Real World
Terraform state files are essential for managing cloud infrastructure safely and predictably.
💼 Career
Understanding Terraform state is critical for cloud engineers and DevOps professionals to maintain infrastructure as code.
Progress0 / 4 steps
1
Create a Terraform configuration with an AWS S3 bucket
Create a file called main.tf and write a Terraform resource block named aws_s3_bucket with the resource name my_bucket. Set the bucket attribute to the exact string "my-terraform-bucket-12345".
Terraform
Need a hint?

Use the resource keyword, then the resource type aws_s3_bucket, then the resource name my_bucket. Inside the block, set bucket = "my-terraform-bucket-12345".

2
Add a variable for the bucket name
Create a variable called bucket_name of type string with the default value "my-terraform-bucket-12345". Then update the bucket attribute in the aws_s3_bucket.my_bucket resource to use this variable.
Terraform
Need a hint?

Define the variable block with variable "bucket_name", set type = string and default = "my-terraform-bucket-12345". Then use var.bucket_name inside the resource.

3
Initialize Terraform to create the state file
Run the Terraform command terraform init in your terminal to initialize the working directory and create the state file. Then run terraform apply and confirm the apply to create the S3 bucket and update the state file.
Terraform
Need a hint?

Use your terminal to run terraform init first, then terraform apply. Confirm the apply when prompted.

4
Understand how the state file tracks your resource
Open the terraform.tfstate file created by Terraform. Add a comment in your main.tf file explaining that this file keeps track of the real cloud resources Terraform manages.
Terraform
Need a hint?

Write a comment starting with # The terraform.tfstate file tracks the real cloud resources explaining the state file's purpose.