API-driven runs: Get Run-ID after uploading archive / Error handling

Hi there!

I’ve followed the guidelines for a API-driven run in the documentation: API-driven Runs - Runs - Terraform Cloud and Terraform Enterprise - Terraform by HashiCorp

It’s working fine, but right now I’m trying to make this more robust by trying to find out if any errors are happening after uploading the archive file. Querying the self link for the configuration version is actually not really helpful - even if the run is not being executed because of some misconfiguration of the workspace there is no error message to be found in the response body:

{
   "data":{
      "id":"cv-<redacted>",
      "type":"configuration-versions",
      "attributes":{
         "auto-queue-runs":true,
         "error":null,
         "error-message":null,
         "source":"tfe-api",
         "status":"uploaded",
         "status-timestamps":{
            "uploaded-at":"2021-10-09T10:04:50+00:00"
         },
         "changed-files":[
            
         ]
      },
      "relationships":{
         "ingress-attributes":{
            "data":null,
            "links":{
               "related":"/api/v2/configuration-versions/cv-<redacted>/ingress-attributes"
            }
         }
      },
      "links":{
         "self":"/api/v2/configuration-versions/cv-<redacted>"
      }
   }
}

Is there any way to find out if errors were happening after uploading the archive?

Also I’m trying to find out how I can query the run-id by using the configuration-version id. Is there any way to query the automatically executed run to get some status back?

When POSTing to /runs/ you will get a response that contains the run ID. In my powershell trigger, I’ve been using this:

$Headers = @{}
$Headers.Add("Authorization", "Bearer " + "$TERRAFORM_API_TOKEN")
$Headers.Add("Content-Type", "application/vnd.api+json")
$RunBody = '{
  "data": {
    "attributes": {
      "message": "API Triggered Run"
    },
    "type":"runs",
    "relationships": {
      "workspace": {
        "data": {
          "type": "workspaces",
          "id": "&"
        }
      }
    }
  }
}'

$RunBody = $RunBody.Replace("&",$TFWorkspaceId)

$Run = Invoke-RestMethod -Method POST -Headers $Headers -body $RunBody -Uri "https://app.terraform.io/api/v2/runs"

Then I can get the run state with:

$GetRun = Invoke-RestMethod -Method GET -Headers $Headers -Uri "https://app.terraform.io/api/v2/runs/$($run.data.id)"