i have deployed terraform script using Terraform Cloud. In tab 'STATE' I can see all the data of my VM machine. For my pipeline I need to download the sv-xxxx.tfstate file in order to get the VM IP. My question is how can I download this file using API? I looked in documentation but couldn't find any hint.
Terraform Cloud: How to download .tfstate file using API?
1.3k Views Asked by Andrew Ramnikov At
2
There are 2 best solutions below
0
On
you can use this command that I found in this tutorial: https://developer.hashicorp.com/terraform/tutorials/cloud/cloud-state-api#modify-and-create-the-state-payload
#!/bin/bash
HTTP_RESPONSE=$(curl \
--header "Authorization: Bearer "$TFC_TOKEN"" \
--header "Content-Type: application/vnd.api+json" \
"https://app.terraform.io/api/v2/workspaces/"$WORKSPACE_ID"/current-state-version" | jq -r '.data | .attributes | ."hosted-state-download-url"')
curl -o state.tfstate $HTTP_RESPONSE
As the doc states, you can call
GET /workspaces/:workspace_id/current-state-versionwhose response will include ahosted-state-download-urlattribute. This is the URL for the.tfstatefile.