Skip to main content

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

Project description

tf_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.

  • SSL Verification: SSL verification is skipped if there is any certificate errors.

Usage

1. Setup

Install dependencies:

pip3 install requests
pip3 install python-utilities-tfc

Import the module in your Python script:

from python_utilities_tfc import tf_utils
export TERRAFORM_CLOUD_TOKEN="your-token"
export TERRAFORM_ORG_NAME="your-org-name"

2. Example Operations

Clean a State File

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

Download Workspace State

tf_utils.download_workspace_state(
    workspace_name="my-workspace",
)

Trigger and Monitor a Run

run_id = tf_utils.trigger_run(
    workspace_id="ws-xxxx",
    auto_apply=True
)
tf_utils.monitor_run(
    run_id=run_id
)

Get Run Outputs

outputs = tf_utils.get_run_outputs(
    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-1.3.0.tar.gz (8.0 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-1.3.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python-utilities-tfc-1.3.0.tar.gz
  • Upload date:
  • Size: 8.0 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-1.3.0.tar.gz
Algorithm Hash digest
SHA256 817d289c8aa4b33bc5fc79de890b20b4fa1b8cc1be057c9595114766fea52252
MD5 b5e1210deec2b588f47429f0bdad3f48
BLAKE2b-256 0be4dbf3a55d618a59862e8e4410fb795715b337cf96683737bd5028b436f521

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_utilities_tfc-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45d9ce22764e872cc35b45b002129dd187a0c9c10bcb30b5071e573c38f2fbcf
MD5 470124ac0ddf76f7796cf7e161b8b7b5
BLAKE2b-256 52cb6edd4d396c62397ffcb9042c059519183f0c987765f91658c6144deb347a

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