Created
February 7, 2023 18:47
-
-
Save pat-s/6cc1a931866e869f1adb1c84f715ef92 to your computer and use it in GitHub Desktop.
Migrate from Terraform Cloud to "Remote" storage backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export TFC_TOKEN=<CHANGEME> # TF cloud token | |
export WORKSPACE_ID=<CHANGEME> # old TF cloud Workspace ID | |
export WORKSPACE_ID_NEW=<CHANGEME> # new OTF workspace ID | |
export TOKEN_REMOTE_BACKEND=<CHANGEME> # OTF token | |
export REMOTE_BACKEND_URL=<CHANGEME> # OTF URL | |
### START | |
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 | |
SERIAL_OLD=$(cat state.tfstate | jq -r '.serial') | |
SERIAL_NEW=$(($SERIAL_OLD + 1)) | |
# create state fle with new serial | |
cat state.tfstate | jq '.serial = '$SERIAL_NEW'' >state.tfstate.new | |
### Create payload | |
serial=$(cat state.tfstate.new | jq '.serial') | |
md5_compute=$(md5 -q state.tfstate.new) | |
md5=\"$md5_compute\" | |
lineage=$(cat state.tfstate.new | jq '.lineage') | |
base64_encode=$(base64 -i state.tfstate.new) | |
state=\"$base64_encode\" | |
echo "{ | |
\"data\": { | |
\"type\": \"state-versions\", | |
\"attributes\": { | |
\"serial\": $serial, | |
\"md5\": "$md5", | |
\"lineage\": "$lineage", | |
\"state\": "$state" | |
} | |
} | |
}" >payload.json | |
# Upload new state file | |
HTTP_POST=$(curl \ | |
--header "Authorization: Bearer "$TOKEN_REMOTE_BACKEND"" \ | |
--header "Content-Type: application/vnd.api+json" \ | |
--request POST \ | |
--data @payload.json \ | |
"https://$REMOTE_BACKEND_URL/api/v2/workspaces/"$WORKSPACE_ID_NEW"/state-versions") | |
echo $HTTP_POST |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment