Introduction
This article outlines the procedure to retrieve detailed user information associated with HCP Terraform or Terraform Enterprise runs using the API. You can use this method to extract the user_id of the individual who triggered a specific run and then fetch their username. This information is useful for auditing, troubleshooting, and monitoring purposes.
Prerequisites
- An API token with permissions to access run and user data.
- The ID of the specific run you want to investigate (e.g.,
run-EXA5Ldb78CDjaDQS).
Procedure
Follow these steps to identify the user who initiated a Terraform run.
Step 1: Retrieve the Run Event Data
To identify the user associated with a specific run, make an API call to the runs API. This response contains the user_id of the user who created the run.
$ curl \ --header "Authorization: Bearer $TOKEN" \ https://<HOSTNAME>/api/v2/runs/<RUN_ID>/run-events | jq .
Note:
- Replace
<HOSTNAME>with your Terraform Enterprise hostname orapp.terraform.iofor HCP Terraform. - Replace
<RUN_ID>with the actual ID of the run. - Replace
$TOKENwith your API token.
The jq . command formats the JSON output for readability.
Step 2: Fetch User Details with the User ID
Once you have the user_id from the previous step, you can retrieve the username and other details for that user.
$ curl \ --header "Authorization: Bearer $TOKEN" \ --header "Content-Type: application/vnd.api+json" \ https://<HOSTNAME>/api/v2/users/<USER_ID> | jq .
Note:
- Replace
<HOSTNAME>with your Terraform Enterprise hostname orapp.terraform.iofor HCP Terraform. - Replace
<USER_ID>with theuser_idobtained in Step 1. - Replace
$TOKENwith your API token.
The API response will contain detailed information about the user, including their username.
Additional Information
- For more details on managing users via the API, refer to the Users API Documentation.