Skip to main content

A library for generating and manipulating Terraform configurations in HCL format.

Project description

TerraForge

TerraForge is a Python library for generating and manipulating Terraform configurations in HCL format. It provides an intuitive, programmatic interface to create and manage Terraform files, making it easier to build, modify, and maintain infrastructure-as-code configurations.

Features

  • HCL Generation: Create HCL expressions, blocks, and entire Terraform configurations.
  • Flexible Configuration: Easily add providers, variables, resources, and modules.
  • Simple API: Designed to be used as a Python module for quick integration into your projects.

Installation

TerraForge is available on PyPI. You can install it using pip:

pip install terraforge

Alternatively, install it from source:

  1. Clone the repository:
    git clone https://github.com/yourusername/terraforge.git
    
  2. Change to the project directory:
    cd terraforge
    
  3. Install the package:
    pip install .
    

Usage

Here is a simple example that demonstrates how to create a Terraform configuration with TerraForge:

from terraforge import TerraformConfig

# Create a new Terraform configuration
config = TerraformConfig()

# Add a provider configuration
config.add_provider("aws", region="us-west-2")

# Add a variable
config.add_variable("instance_type", default="t2.micro")

# Add a resource
config.add_resource("aws_instance", "example", ami="ami-0abcdef1234567890", instance_type="t2.micro")

# Format the configuration as HCL and print it
hcl_config = config.format_config()
print(hcl_config)

# Save the configuration to a file
config.save("main.tf")

This example creates a basic Terraform configuration with:

  • An AWS provider configured for the us-west-2 region.
  • A variable called instance_type with a default value.
  • A resource (AWS EC2 instance) with specified attributes.

API Overview

Core Classes

  • HCLExpression
    Wraps a raw HCL expression so that it is output without quotes when rendered.

  • HCLBlock
    Represents a generic HCL block. This class supports:

    • Adding attributes (key/value pairs)
    • Nesting blocks (useful for complex configurations)
  • TerraformConfig
    A configuration builder that provides methods to:

    • add_required_provider(provider_name, source, version): Specify a required provider.
    • add_provider(provider_name, **kwargs): Add a provider block.
    • add_variable(var_name, **kwargs): Define a variable.
    • add_resource(resource_type, resource_name, **kwargs): Define a resource.
    • add_module(module_name, source, **kwargs): Add a module.
    • format_config(): Render the complete configuration as an HCL string.
    • save(filename): Write the configuration to a file.

Utility Functions

  • render_value(value, indent=0)
    Recursively renders a Python value (strings, numbers, booleans, lists, dicts, etc.) into an HCL-formatted string.

  • is_simple_scalar(value)
    Returns True if the value is a simple scalar (int, float, bool, str, or HCLExpression) that can be rendered inline.

Running Tests

To run the tests, execute the following command from the root of the project:

python -m unittest discover

This command will discover and run all tests located in the tests/ directory.

Contributing

Contributions are welcome! If you have suggestions or improvements:

  • Open an issue or submit a pull request on GitHub.
  • Follow PEP 8 style guidelines.
  • Include tests for any new features or bug fixes.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Contact

For questions or feedback, please open an issue in the GitHub repository.


Happy Terraforming!

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

terraforge-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

terraforge-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file terraforge-0.1.0.tar.gz.

File metadata

  • Download URL: terraforge-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for terraforge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc6a71a7b9d490019e230beda4f0a2bc9849256121babca2f27e5ead4c27be00
MD5 8e37c4067ec5e00b8f88e689be2cfc81
BLAKE2b-256 f23d5a4a9fe399f727e4c31ad80c8f7c16d79f998de69e13c7890e43c52320a7

See more details on using hashes here.

File details

Details for the file terraforge-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: terraforge-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for terraforge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa75e3a4203846eb665d7318cf0375517b1a4061fad73f5367a3727d434b9884
MD5 c0a54c36c6a7867852e47044b4d6f3f6
BLAKE2b-256 815c2ab6df3cd17069020b51d07edc99fb0a8f0e0e692ee5b280835502be3bdd

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