Introduction
We may need to find all the workspaces and their corresponding ID within our HCP Terraform Cloud organization. As of now, we can find all the workspaces listed in the HCP Terraform Explorer but the workspace IDs are not listed.
We can use the HCP Terraform API and jq to list the workspace names and corresponding IDs.
Please refer to this documentation to install jq.
Procedure
- Go to your HCP Terraform organization settings >> API Tokens >> Generate and copy the organization token.
- export the token in a variable called token:
-
export TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-
- Execute the below curl request. Replace my-organization with your HCP Terraform organization name.
-
curl \
--header "Authorization: Bearer $TOKEN" \
--header "Content-Type: application/vnd.api+json" \
https://app.terraform.io/api/v2/organizations/my-organization/workspaces | jq '.data[]|"Workspace ID: \(.id), Workspace Name: \(.attributes.name)"'
-
Sample Response
"Workspace ID: ws-w7RBP8sqk1XScTSG, Workspace Name: abc1"
"Workspace ID: ws-6bnfWGJ7gvH5gdfM, Workspace Name: abc2"
"Workspace ID: ws-FWKXcLjTEgmwDYdj, Workspace Name: abc3"
"Workspace ID: ws-YkzUfc76KSZ2Adj2, Workspace Name: abc4"
"Workspace ID: ws-bYdP8bqy4cecZAnp, Workspace Name: abc5"
"Workspace ID: ws-V2UnLRueBKFKGVWD, Workspace Name: abc6"
"Workspace ID: ws-WxrYp3mZCmbyR3gW, Workspace Name: abc7"
"Workspace ID: ws-peCytTwieHw5fwS1, Workspace Name: abc8"
"Workspace ID: ws-pSwxdTGGz3kgtWAx, Workspace Name: abc9"
"Workspace ID: ws-gW8BLmm5j6kKdSLT, Workspace Name: abc10"
"Workspace ID: ws-KC68HwG8BU4JFWEv, Workspace Name: abc11"
"Workspace ID: ws-rWdWeHkGr6QZwR5Q, Workspace Name: abc12"
"Workspace ID: ws-cLefzP9wJjyMN6XD, Workspace Name: abc13"
"Workspace ID: ws-Sc1LJNRHZvmyuqtf, Workspace Name: abc14"
"Workspace ID: ws-pRLZyR7GNxQ8mQLE, Workspace Name: abc15"
"Workspace ID: ws-xfYaNTp6MvW8LNmg, Workspace Name: abc16"
"Workspace ID: ws-bsohyyZctg2JMVwA, Workspace Name: abc17"
"Workspace ID: ws-zvTFvfxGGGWPvBWy, Workspace Name: abc18"
"Workspace ID: ws-awwgkuPajp5UdzT3, Workspace Name: abc19"
"Workspace ID: ws-ZVNGjNzQ3DJsNKZ1, Workspace Name: abc20"