Terraform Cloud/Enterprise Activity Meter

Terraform Cloud/Enterprise Activity Meter

Written: 2020-10-20
Author: WhatsARanjit
Links:

The problem

You have an organization setup in Terraform Cloud. You’re thinking you want to upgrade to Terraform Cloud Business Tier. You’re wondering things like, “How many applies do I actually do in a given period of time?” That thought might spark similar questions like, “How many nightly plans come back with no change?” or “How many policy infractions occur per quarter?” The information can all be collected through the API, but there’s not an easy workflow to do so.

The fix

All run data is available via the Runs API, but if you’re looking for org-level information, you’ll want to iterate through each workspace. At the workspace-level, the API provides paged output- in other words, each API response by default returns 20 runs. Because of this, if you want to explore a specific time interval, you’ll have to page through the data to find all relevant runs for your time period. What we need is a way to iterate through all the relevant data and piece together individual and grand totals. The interface at:

https://whatsaranjit.herokuapp.com/hashicorp/tfc_activity

…allows you to query a Terraform Cloud/Enterprise organization and find out how much of a thing you are doing for a given period of time.

Setup

You’ll need some setup values to make this work. Here are the basics:

After inputting this information, hit Retrieve Workspaces to advance in the workflow. You’ll be presented with a few more options.

Usage

  1. Bring up https://whatsaranjit.herokuapp.com/hashicorp/tfc_activity in your browser.
  2. Enter the private DNS name of your TFE server, if it is not set, Terraform Cloud will be used (the web app needs access to your DNS name to work).
  3. Provide the TFC token to use. https://www.terraform.io/docs/enterprise/users-teams-organizations/users.html#api-tokens
  4. Hit the Retrieve Workspace button.
  5. Select a workspace or click the All Workspaces checkbox.
  6. Enter start and end dates.
  7. Select a metric to filter runs or click the All Runs checkbox.
  8. Hit the Retrieve Activity button.
  9. Examine the output for each workspace and what they contain and lastly the grand total across the organization or All Workspaces was selected.

In the end

We’re trying to get a sense of the level of activity your organization is experiencing. Unseen patterns could emerge. Maybe one of your policies creates an inordinate number of hard-fails. Or maybe one workspace in particular has a ton more error runs than others. For automation reporting you can hit the API directly. Good counting!