Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Created August 23, 2020 07:14
Show Gist options
  • Save hongkongkiwi/daf9c2a59e9131fcacec7349a0c93187 to your computer and use it in GitHub Desktop.
Save hongkongkiwi/daf9c2a59e9131fcacec7349a0c93187 to your computer and use it in GitHub Desktop.
An incomplete script to import terraform cloud variables.
#!/bin/sh
ORGANIZATION_NAME=`cat backend.tf | grep "organization" | cut -f2 -d'=' | tr -d ' ' | tr -d '"'`
WORKSPACE_NAME=`cat backend.tf | grep ' name = "' | cut -f2 -d'=' | tr -d ' ' | tr -d '"'`
TERRAFORM_CLOUD_TOKEN=${TERRAFORM_CLOUD_TOKEN:-""}
while IFS= read -r VARIABLE; do
PAYLOAD='' read -r -d '' String <<"EOF"
{
"data": {
"type":"vars",
"attributes": {
"key":"some_key",
"value":"some_value",
"description":"some description",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"ws-4j8p6jX1w33MiDC7",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer $TERRAFORM_CLOUD_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data "$PAYLOAD" \
"https://app.terraform.io/api/v2/vars"
done <<< "$VARIABLES"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment