Terraform Cloud API Reference tips¶
Tips for executing API requests using curl
to Terraform Cloud.
Setting up Terraform Tokens¶
We need to have API token in $HOME/.terraform.d/credentials.tfrc.json
.
Creating env variables:
export TFE_TOKEN=$(grep token $HOME/.terraform.d/credentials.tfrc.json | cut -d '"' -f4)
export ORG=<MY_ORG>
Listing workspaces and getting IDs¶
curl -H "Authorization: Bearer $TFE_TOKEN" \
-H "Content-Type: application/vnd.api+json" \
-X GET https://app.terraform.io/api/v2/organizations/$ORG/workspaces | jq -r '.data[] | .id + " -- " + .attributes.name'
Creating variables in a Workspace¶
Create a payload JSON with the variable data:
{
"data": {
"type":"vars",
"attributes": {
"key":"some_key",
"value":"some_value",
"description":"some description",
"category":"terraform",
"hcl":false,
"sensitive":false
}
}
}
We need the workspace-id
of the specific Workspace. The API request with the specific payload is:
curl \
--header "Authorization: Bearer $TFE_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/workspaces/$WORKSPACE_ID/vars