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.
Usage
1. Setup
Install dependencies:
pip install requests
Import the module in your Python script:
from python_utilities_tfc import tf_utils
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(
org_name="my-org",
workspace_name="my-workspace",
token="TERRAFORM_API_TOKEN"
)
Trigger and Monitor a Run
run_id = tf_utils.trigger_run(
api_token="TERRAFORM_API_TOKEN",
organization="my-org",
workspace_id="ws-xxxx",
auto_apply=True
)
tf_utils.monitor_run(
api_token="TERRAFORM_API_TOKEN",
organization="my-org",
run_id=run_id
)
Get Run Outputs
outputs = tf_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+
requestslibrary
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-utilities-tfc-0.6.0.tar.gz.
File metadata
- Download URL: python-utilities-tfc-0.6.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b44ec75834adba56b0835180362aadd33fd1436cfd197b3d15182362c3568797
|
|
| MD5 |
1156ee52d56d7055ec91dcdcf609215f
|
|
| BLAKE2b-256 |
1d252de4ab4f23d80911e24ea2235ccb6fde9fc783849caccd70f59a24051041
|
File details
Details for the file python_utilities_tfc-0.6.0-py3-none-any.whl.
File metadata
- Download URL: python_utilities_tfc-0.6.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0375f552939af481c2d1fe2ff99f9a1eb5f22ff6a4007e36366b3c7996740a13
|
|
| MD5 |
da645d6cb2dfec0b74ede372ced04e9a
|
|
| BLAKE2b-256 |
1525af5cccb6b84854bd77a1e44ef0cfce2da8f14110a3c6e2f953b23a4eea3b
|