Terraform Cloud is a SaaS platform for managing Terraform runs, state and variables. Once you have created a project you can set environment variables via the following page:
https://app.terraform.io/app/[organization]/workspaces/[workspace]/variables
These environment variables are then set when Terraform executes a run. Out of an abundance of a desire to automate this process, I have scripted the process of generating these environments variables:
$SubscriptionId = "<subscription-id>"
$PrincipalName = "http://<service-principal-name>"
$SubscriptionUrl = "/subscriptions/$SubscriptionId"
az cloud set --name "AzureCloud" | Out-Null
az account set --subscription $SubscriptionId | Out-Null
$ServicePrincipal = az ad sp create-for-rbac --role="Owner" --scopes=$SubscriptionUrl --name $PrincipalName | ConvertFrom-Json
$env:ARM_CLIENT_ID=$ServicePrincipal.appId
$env:ARM_CLIENT_SECRET=$ServicePrincipal.password
$env:ARM_SUBSCRIPTION_ID=$SubscriptionId
$env:ARM_TENANT_ID=$ServicePrincipal.tenant
This works, however, I would like to extend the script to be able to push the variables to the appropriate Terraform Workspace. I am aware that there is a RESTful Variables API that I can call although I was hoping for a solution that used the terraform
command-line interface rather than relying on parsing backend.tf
and calling a REST API from a script.