Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Terraform state list command
📖 Scenario: You are managing cloud infrastructure using Terraform. You want to see which resources Terraform currently manages in your project.
🎯 Goal: Learn how to use the terraform state list command to view all resources tracked in the Terraform state file.
📋 What You'll Learn
Create a Terraform configuration with at least two resources
Initialize Terraform in the project directory
Apply the Terraform configuration to create resources
Use the terraform state list command to list all managed resources
💡 Why This Matters
🌍 Real World
Terraform state files keep track of all resources created by Terraform. Knowing how to list these resources helps you understand what Terraform manages and troubleshoot your infrastructure.
💼 Career
Cloud engineers and DevOps professionals use Terraform daily to manage infrastructure. Being able to inspect the Terraform state is essential for safe and effective infrastructure management.
Progress0 / 4 steps
1
Create a Terraform configuration with two resources
Create a file named main.tf with two resources: an AWS S3 bucket named my_bucket and an AWS DynamoDB table named my_table. Use the exact resource names aws_s3_bucket.my_bucket and aws_dynamodb_table.my_table.
Terraform
Hint
Define two resources with the exact names my_bucket and my_table inside main.tf.
2
Initialize Terraform in the project directory
Run the command terraform init in your project directory to initialize Terraform and download the AWS provider.
Terraform
Hint
Use your terminal to run terraform init to prepare Terraform.
3
Apply the Terraform configuration to create resources
Run the command terraform apply -auto-approve to create the resources defined in main.tf and update the Terraform state file.
Terraform
Hint
Use your terminal to run terraform apply -auto-approve to create resources without manual confirmation.
4
List all resources tracked in the Terraform state
Run the command terraform state list in your project directory to see all resources Terraform currently manages in the state file.
Terraform
Hint
Use your terminal to run terraform state list to see all resources Terraform manages.
Practice
(1/5)
1. What does the terraform state list command do?
easy
A. It destroys all resources in the state.
B. It applies changes to the infrastructure.
C. It initializes the Terraform working directory.
D. It shows all resources managed by Terraform in the current state.
Solution
Step 1: Understand the purpose of terraform state list
This command lists all resources that Terraform currently manages in its state file.
Step 2: Compare with other Terraform commands
Other commands like apply, init, and destroy perform different actions, not listing resources.
Final Answer:
It shows all resources managed by Terraform in the current state. -> Option D
Quick Check:
List resources = D [OK]
Hint: Remember: 'state list' shows current managed resources [OK]
Common Mistakes:
Confusing 'state list' with 'apply' or 'destroy'
Thinking it initializes Terraform
Assuming it modifies resources
2. Which of the following is the correct syntax to list all resources in the Terraform state?
easy
A. terraform list state
B. terraform state list
C. terraform show state list
D. terraform state show list
Solution
Step 1: Recall the correct command structure
The correct command to list resources is terraform state list.
Step 2: Identify incorrect syntax
Options B, C, and D mix command parts incorrectly and will cause errors.
Final Answer:
terraform state list -> Option B
Quick Check:
Correct syntax = A [OK]
Hint: Command order: terraform + state + list [OK]
Common Mistakes:
Swapping 'state' and 'list' order
Adding extra words like 'show'
Using unsupported command combinations
3. Given the following Terraform state list output: module.network.aws_vpc.main module.network.aws_subnet.public module.compute.aws_instance.web What will terraform state list module.network.aws_subnet.public display?
medium
A. module.network.aws_subnet.public
B. module.compute.aws_instance.web
C. module.network.aws_vpc.main
D. No output
Solution
Step 1: Understand filtering by resource name
Using terraform state list with a resource name filters the output to matching resources.
Step 2: Match the exact resource path
The command matches module.network.aws_subnet.public exactly, so it will output that line.
Final Answer:
module.network.aws_subnet.public -> Option A
Quick Check:
Filtered list = C [OK]
Hint: Filter by full resource path to get exact match [OK]
Common Mistakes:
Expecting all resources to show
Confusing module paths
Assuming no output if filter is used
4. You run terraform state list module.app.aws_instance.web but get no output, even though you expect the resource to exist. What is the most likely cause?
medium
A. Terraform state list command does not support filtering.
B. Terraform state file is empty and no resources exist.
C. The resource name or module path is incorrect or misspelled.
D. You need to run terraform apply first.
Solution
Step 1: Check the resource path correctness
If no output appears, the resource path or name likely does not match any resource in the state.
Step 2: Understand filtering behavior
The terraform state list command supports filtering by resource name or module path.
Final Answer:
The resource name or module path is incorrect or misspelled. -> Option C
Quick Check:
Incorrect path = B [OK]
Hint: Check spelling and module path carefully when filtering [OK]
Common Mistakes:
Assuming filtering is unsupported
Thinking state file is always empty
Believing apply is needed to list state
5. You want to list all resources managed by Terraform inside the module database only. Which command will correctly show this filtered list?
hard
A. terraform state list module.database
B. terraform state list database
C. terraform state list module.database.*
D. terraform state list module/database
Solution
Step 1: Understand module path filtering
To filter resources inside a module, use the full module path prefix like module.database.
Step 2: Check command syntax
terraform state list module.database uses the correct prefix without extra characters. terraform state list module.database.* uses a wildcard which is not supported in this command.
Final Answer:
terraform state list module.database -> Option A
Quick Check:
Filter by module prefix = A [OK]
Hint: Use full module path prefix without wildcards to filter [OK]