Joey Yi Zhao
Joey Yi Zhao

Reputation: 42576

How to list resources when using remote state?

I am using terraform 0.12.9 and state is saved on s3 bucket. I'd like to list all resources by terraform state list. Based on this document, https://www.terraform.io/docs/commands/state/list.html, it says -state=path - Path to the state file. Defaults to "terraform.tfstate". Ignored when remote state is used.. How can I pass the state file if it is on remote s3 bucket?

Upvotes: 2

Views: 10753

Answers (3)

RSW
RSW

Reputation: 1396

In my case it was my custom module, so I used:

terraform state list 

Followed by state show

terraform state show 'module.sapsystem.azurerm_lb_backend_address_pool_address.backend_nodes["my-lovely-lb-bepool-node"]'

Upvotes: 0

Zac
Zac

Reputation: 730

  1. terraform init
  2. terraform workspace list # show all workspaces
  3. terraform workspace select {env} # select the workspace which you want list resources
  4. terraform state list # list all resources. Could return nothing if there is no resource in the workspace
  5. terraform state show '{resource}' # show the attributes of a single resource

Upvotes: 2

ofirule
ofirule

Reputation: 4679

You need to configure the tfstate bucket path in your terraform.tf file:

terraform {
  backend "s3" {
    bucket = "bucket_name"
    key    = "my/key/location/terraform.tfstate"
    region = "bucket region"
  }
}

and later you need to run terraform init so that terraform would fetch the state from the remote bucket

Upvotes: 2

Related Questions