Skip to main content

Utility functions to work with Terraform Cloud and manage Terraform state.

Project description

terraform_state_utils.py

A Python utility module for advanced management and automation of Terraform Cloud workspaces and state files.

Features

  • State File Filtering:
    Clean and filter Terraform state files by resource/module prefixes.

  • Terraform Cloud API Automation:

    • Download/upload state files.
    • Lock/unlock workspaces.
    • Trigger, monitor, and manage runs (cancel, discard, apply).
    • Manage workspace variables.
    • Retrieve run outputs and plan summaries.
  • Logging:
    Consistent, timestamped logging for all operations.

Usage

1. Setup

Install dependencies:

pip install requests

Import the module in your Python script:

from utilities import terraform_state_utils

2. Example Operations

Clean a State File

terraform_state_utils.clean_state_file_by_prefixes(
    input_path="input.tfstate",
    output_path="cleaned.tfstate",
    keep_prefixes=["module1"],
    remove_prefixes=["module2"]
)

Download Workspace State

terraform_state_utils.download_workspace_state(
    org_name="my-org",
    workspace_name="my-workspace",
    token="TERRAFORM_API_TOKEN"
)

Trigger and Monitor a Run

run_id = terraform_state_utils.trigger_run(
    api_token="TERRAFORM_API_TOKEN",
    organization="my-org",
    workspace_id="ws-xxxx",
    auto_apply=True
)
terraform_state_utils.monitor_run(
    api_token="TERRAFORM_API_TOKEN",
    organization="my-org",
    run_id=run_id
)

Get Run Outputs

outputs = terraform_state_utils.get_run_outputs(
    api_token="TERRAFORM_API_TOKEN",
    run_id="run-xxxx"
)
print(outputs)

Functions Overview

  • setup_logging(): Configure logging.
  • clean_state_file_by_prefixes(): Filter resources in a state file.
  • download_workspace_state(): Download the latest state from Terraform Cloud.
  • push_state_to_terraform_cloud(): Upload a state file to Terraform Cloud.
  • lock_workspace(), unlock_workspace(): Lock/unlock a workspace.
  • trigger_run(), monitor_run(), cancel_run(), discard_run(), apply_run(): Manage runs.
  • get_run_outputs(), get_plan_summary(): Retrieve outputs and plan details.
  • get_variable_id(), update_variable(): Manage workspace variables.

Requirements

  • Python 3.11+
  • requests library

Notes

  • All API operations require a valid Terraform Cloud API token.
  • Use caution when discarding or canceling runs, as this may affect your infrastructure state.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-utilities-tfc-0.2.0.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_utilities_tfc-0.2.0-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

File details

Details for the file python-utilities-tfc-0.2.0.tar.gz.

File metadata

  • Download URL: python-utilities-tfc-0.2.0.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.5

File hashes

Hashes for python-utilities-tfc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b5cc7a28349a14b7557773d19ba640a466ab200dc8cd8f01a14db172b492a675
MD5 d5ed4d55548a3423a493ca279f59154e
BLAKE2b-256 e5984d8add50c6beab13625ab6df316d858a2e8b578d5bf6b63e86670a7fb514

See more details on using hashes here.

File details

Details for the file python_utilities_tfc-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_utilities_tfc-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 772e1753ddd59af3ab7740bfb2a7dfc2b99c9938a36abc27a82f4c0bfa310080
MD5 07f01ec3136f2cb60943ec79216689a9
BLAKE2b-256 c3e8896bd486eb137d62d35392ab3c6e0ab54c2e71c813911e9b14c97b00206f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page