Bird
Raised Fist0
Terraformcloud~5 mins

Terraform state list command - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

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
Introduction
When you use Terraform to create cloud resources, it keeps track of them in a state file. The 'terraform state list' command shows you all the resources Terraform knows about in that state. This helps you see what Terraform is managing right now.
When you want to see all the cloud resources Terraform is currently managing in your project.
When you need to check if a specific resource was created by Terraform.
When you want to verify the names of resources before running commands that target them.
When troubleshooting to confirm Terraform's view of your infrastructure matches reality.
When preparing to remove or modify resources and you want to list them first.
Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares Terraform to work with your configuration and state.
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 applies your Terraform configuration to create or update resources. The '-auto-approve' flag skips the manual approval step to speed up the process.
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 - Automatically approve the apply without prompting for confirmation
This command lists all the resources currently tracked in the Terraform state file. It helps you see what Terraform is managing.
Terminal
terraform state list
Expected OutputExpected
aws_instance.example
Key Concept

If you remember nothing else, remember: 'terraform state list' shows all resources Terraform manages in your current state.

Common Mistakes
Running 'terraform state list' before 'terraform init'
Terraform needs to be initialized to download providers and set up the backend before it can read the state.
Always run 'terraform init' first to prepare your working directory.
Expecting 'terraform state list' to show resources not created by Terraform
Terraform only tracks resources it created or imported; other resources won't appear.
Use cloud provider consoles or CLI tools to see resources outside Terraform.
Summary
Run 'terraform init' to prepare your working directory and download providers.
Use 'terraform apply -auto-approve' to create or update resources without manual approval.
'terraform state list' shows all resources Terraform currently manages in your state file.

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

  1. Step 1: Understand the purpose of terraform state list

    This command lists all resources that Terraform currently manages in its state file.
  2. Step 2: Compare with other Terraform commands

    Other commands like apply, init, and destroy perform different actions, not listing resources.
  3. Final Answer:

    It shows all resources managed by Terraform in the current state. -> Option D
  4. 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

  1. Step 1: Recall the correct command structure

    The correct command to list resources is terraform state list.
  2. Step 2: Identify incorrect syntax

    Options B, C, and D mix command parts incorrectly and will cause errors.
  3. Final Answer:

    terraform state list -> Option B
  4. 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

  1. Step 1: Understand filtering by resource name

    Using terraform state list with a resource name filters the output to matching resources.
  2. Step 2: Match the exact resource path

    The command matches module.network.aws_subnet.public exactly, so it will output that line.
  3. Final Answer:

    module.network.aws_subnet.public -> Option A
  4. 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

  1. 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.
  2. Step 2: Understand filtering behavior

    The terraform state list command supports filtering by resource name or module path.
  3. Final Answer:

    The resource name or module path is incorrect or misspelled. -> Option C
  4. 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

  1. Step 1: Understand module path filtering

    To filter resources inside a module, use the full module path prefix like module.database.
  2. 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.
  3. Final Answer:

    terraform state list module.database -> Option A
  4. Quick Check:

    Filter by module prefix = A [OK]
Hint: Use full module path prefix without wildcards to filter [OK]
Common Mistakes:
  • Using wildcards which are unsupported
  • Omitting 'module.' prefix
  • Using partial or incorrect module names