Skip to main content

Terraform task management wrapper for Python

Project description

twrapform

A Python library for running Terraform commands from Python with asynchronous workflow management.

✨ Features

  • Run Terraform commands natively from Python
  • Asynchronous execution using asyncio
  • Immutable task definitions with unique IDs
  • Grouped workflow orchestration
  • Granular error handling with structured results

📦 Requirements

  • Python 3.10+
  • Terraform installed and available in your system's PATH

🔧 Installation

pip install twrapform

🚀 Usage Examples

Run a single workflow with chained Terraform tasks

import asyncio

from twrapform import Workflow
from twrapform.exception import TwrapformError
from twrapform.options import InitTaskOptions, PlanTaskOptions, ApplyTaskOptions, OutputTaskOptions


async def main():
    # Create an instance of Twrapform
    twrap = Workflow(work_dir="/terraform_rootpath")

    # Add Terraform tasks one by one
    twrap = twrap.add_task(InitTaskOptions())

    # Chain multiple tasks
    twrap = (
        twrap
        .add_task(PlanTaskOptions(var={"var1": 1}))
        .add_task(ApplyTaskOptions(var={"var1": 1}))
        .add_task(OutputTaskOptions(json=True))
    )

    # Execute all tasks
    results = await twrap.execute()

    try:
        # Raise errors if any task fails
        results.raise_on_error()
    except TwrapformError as e:
        print(f"Error occurred: {e.message}")

    # Output results for successful tasks
    for success_task in results.get_success_tasks():
        print(success_task.stdout)


if __name__ == "__main__":
    asyncio.run(main())

Manage multiple workflows in a group using WorkflowManager

from twrapform import Workflow, WorkflowManager
from twrapform.options import InitTaskOptions
import asyncio

async def main():
    # Define workflows with Terraform initialization tasks
    workflow1 = Workflow(work_dir="infra/project1").add_task(InitTaskOptions())
    workflow2 = Workflow(work_dir="infra/project2").add_task(InitTaskOptions())
    
    # Add workflows into a group and initialize the manager
    manager = WorkflowManager().add_workflows(workflow1, workflow2, group_id="init-group")
    
    # Run the grouped workflows asynchronously
    result = await manager.execute()
    
    # Display summaries for successfully completed tasks
    for group_result in result.group_results:
        for wf_result in group_result.workflow_results:
            for task in wf_result.get_success_tasks():
                print(task.summary())

                
if __name__ == "__main__":
    asyncio.run(main())

⚙️ Supported Terraform Commands

twrapform currently supports the following Terraform commands:

  • terraform init
  • terraform plan
  • terraform apply
  • terraform output
  • terraform workspace select

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

twrapform-0.1.1.tar.gz (29.7 kB view details)

Uploaded Source

Built Distribution

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

twrapform-0.1.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file twrapform-0.1.1.tar.gz.

File metadata

  • Download URL: twrapform-0.1.1.tar.gz
  • Upload date:
  • Size: 29.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for twrapform-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f0e88455489492070bb8a2aec9c6df9ee9036e24c8ac03962479bd107001c138
MD5 e17dd8560d2f5f95834b8e5eb2f89b07
BLAKE2b-256 5f094b783e595a0fa09a759db1530c3f9ab9c3a05775941046290c3ec6830829

See more details on using hashes here.

Provenance

The following attestation bundles were made for twrapform-0.1.1.tar.gz:

Publisher: publish-pypi.yml on minthem/python-terraform-wrapper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file twrapform-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: twrapform-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for twrapform-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c0598a1f8969ec8025582b2e1f76c914f989ddd971b51c7b933ff18e45b447b
MD5 3bbd2a20810edf7a02b0eaeea41b6cef
BLAKE2b-256 9a1533dc269e949a1532c44455785e737ae45feb0f771c63368ae918a0c73084

See more details on using hashes here.

Provenance

The following attestation bundles were made for twrapform-0.1.1-py3-none-any.whl:

Publisher: publish-pypi.yml on minthem/python-terraform-wrapper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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