0

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.

Andrew Ramnikov
  • 783
  • 2
  • 9
  • 30

2 Answers2

2

As the doc states, you can call GET /workspaces/:workspace_id/current-state-version whose response will include a hosted-state-download-url attribute. This is the URL for the .tfstate file.

Isaac Kleinman
  • 3,994
  • 3
  • 31
  • 35
2

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
zarathoustra
  • 440
  • 3
  • 15