Skip to main content

terraform_usage

Overview

A Python module to call the Terraform Cloud API and retrieve data for total and applied Runs of Workspaces.

> CAUTION! This may take a while to run if the Organization has a large number of Workspaces and / or Runs.

Usage

Installation:

pip3 install terraform_usage
# or
python3 -m pip install terraform_usage

Bash:

tfc_usage -o <organization> -t <token> -k <keyword> -f <filename> -s <start_date> -e <end_date> -m <mode> -u <api_url> -p <page_size> -d <delay>

Python:

import terraform_usage as tfu
workspaces = tfu.list_workspaces(
   <organization>,
   <token>,
   <keyword>,
   <api_url>,
   <page_size>,
   <delay>
)
runs = tfu.analyze_runs(
   workspaces,
   <token>,
   <start_date>,
   <end_date>,
   <mode>,
   <api_url>,
   <page_size>,
   <delay>
)
create_csv(
   [run.values() for run in runs],
   <filename>,
   <mode>
)

Execution example:

tfc_usage -o myorg -t mytoken -k "*myworkspace-*" -m advanced -f myfile.csv -s 2023-11-01 -e 2023-11-30

Run parameters:
Organization: myorg
Keyword: *myworkspace-*
Filename: myfile.csv
Start date: 2023-11-01
End date: 2023-11-30
Mode: advanced
API URL: https://app.terraform.io/api/v2
Page size: 50
Delay: 1.0
-------
Getting page 1 of Workspaces.
Found 3 Workspaces.
Getting Run data for Workspace aws-myworkspace-dev.
Getting page 1 of Runs.
Getting Run data for Workspace aws-myworkspace-prod.
Getting page 1 of Runs.
Getting Run data for Workspace aws-myworkspace-test.
Getting page 1 of Runs.
Creating CSV file myfile.csv.
Writing data to myfile.csv.

Output in CSV file example:

workspace,all_runs,successful_applies,total_time
aws-myworkspace-dev,4,0,53
aws-myworkspace-prod,0,0,0
aws-myworkspace-test,0,0,0

Execution Modes

  • basic
    • Function
      • Get total number of Runs and successful Applies for all time.

    • Available filters
      • Workspace name pattern

    • Pros and cons
      • Faster execution

      • Less details

  • advanced
    • Function
      • Get total number of Runs, successful Applies, and total Run time.

    • Available filters
      • Workspace name pattern

      • Start date

      • End date

    • Pros and cons
      • Potentially slower execution for a large number of Workspaces and Runs.

      • More details

Arguments

  • organization - Terraform Cloud Organization name. Required.

  • token - Terraform Cloud API token. Required.

  • keyword - Workspace name keyword to filter by. Default is “all”.

  • filename - CSV filename to save the output data to. Default is “report.csv”.

  • start_date - Start date for Run lookups. Default is “all”.

  • end_date - End date for Run lookups. Default is “all”.

  • mode - Execution mode (“basic” or “advanced”). Default is “basic”.

  • api_url - Terraform Cloud API URL. Default is “https://app.terraform.io/api/v2”.

  • page_size - Number of items per page. Default is 50.

  • delay - Delay (in seconds) between API calls. Default is 1.0.

Error Handling

  • Error: Skipping run due to missing attribute(s).
    • An attribute (timestamp, etc.) is missing from the Run data. Can be caused by local agent Runs or a glitch during execution.

  • Error: One or more Python exceptions.
    • Multiple possible causes. One of the most common is due to the script hitting the Terraform Cloud API rate limit (30 requests per second). There is a safeguard that slows down execution to avoid this.

API Documentation

https://developer.hashicorp.com/terraform/cloud-docs/api-docs

New Features

  • Added Workspace resource listing.

Create Python code (execute_tfu.py):

import os
import terraform_usage as tfu
from pprint import pprint as pp
resources = tfu.list_resources(
   "<workspace_id>",
   os.environ['TFE_TOKEN'],
   os.environ['TFE_URL'],
   20,
   1
)
 pp(resources)

Set environment variables and execute:

export TFE_TOKEN="<tfe-token>"
export TFE_URL="https://app.terraform.io/api/v2"

python3 execute_tfu.py

Release files for terraform_usage 2.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for terraform_usage 2.2.2
File Size Uploaded
terraform_usage-2.2.2.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for terraform_usage 2.2.2
File Interpreter ABI Platform
terraform_usage-2.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / terraform_usage-2.2.2.tar.gz

Download URL terraform_usage-2.2.2.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f73709dc9e0d1b89cd433323f250513a1ec23e96326a995f9ad93e7613da6c64
BLAKE2b-256 checksum
How to use checksums
6117b928cfde613adc1b6bbc3e9e6706b34665ff1f4fdd54168029fe573c0695
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Linux/5.15.154+

Release files / terraform_usage-2.2.2-py3-none-any.whl

Download URL terraform_usage-2.2.2-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
347a424eeddd8b476bfcf7d85670be72e13d764826db0e6e53b4b6fa410abc6d
BLAKE2b-256 checksum
How to use checksums
3dfb280c660d5990ef05b839f5885086fb8d411491f0c10a7f2ef17802ada2e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Linux/5.15.154+

Release history Release notifications | RSS feed

This release

2.2.2 This release

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.8

2 release files

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page