Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list resources when using remote state?

Tags:

terraform

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?

like image 404
Joey Yi Zhao Avatar asked Dec 09 '25 21:12

Joey Yi Zhao


2 Answers

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

like image 175
ofirule Avatar answered Dec 12 '25 16:12

ofirule


  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
like image 25
Zac Avatar answered Dec 12 '25 14:12

Zac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!