Skip to main content

Python binding for Terraform.

Project description

Python libterraform

libterraform libterraform libterraform Test libterraform

Python binding for Terraform.

Installation

$ pip install libterraform

NOTE

  • Please install version 0.3.1 or above, which solves the memory leak problem.
  • This library does not support multithreading.

Usage

Terraform CLI

TerraformCommand is used to invoke various Terraform commands.

Now, support all commands (plan, apply, destroy etc.), and return a CommandResult object. The CommandResult object has the following properties:

  • retcode indicates the command return code. A value of 0 or 2 is normal, otherwise is abnormal.
  • value represents command output. If json=True is specified when executing the command, the output will be loaded as json.
  • json indicates whether to load the output as json.
  • error indicates command error output.

To get Terraform verison:

>>> from libterraform import TerraformCommand
>>> TerraformCommand().version()
<CommandResult retcode=0 json=True>
>>> _.value
{'terraform_version': '1.2.2', 'platform': 'darwin_arm64', 'provider_selections': {}, 'terraform_outdated': False}
>>> TerraformCommand().version(json=False)
<CommandResult retcode=0 json=False>
>>> _.value
'Terraform v1.2.2\non darwin_arm64\n'

To init and apply according to Terraform configuration files in the specified directory:

>>> from libterraform import TerraformCommand
>>> cli = TerraformCommand('your_terraform_configuration_directory')
>>> cli.init()
<CommandResult retcode=0 json=False>
>>> cli.apply()
<CommandResult retcode=0 json=True>

Additionally, run() can execute arbitrary commands, returning a tuple (retcode, stdout, stderr).

>>> TerraformCommand.run('version')
(0, 'Terraform v1.2.2\non darwin_arm64\n', '')
>>> TerraformCommand.run('invalid')
(1, '', 'Terraform has no command named "invalid".\n\nTo see all of Terraform\'s top-level commands, run:\n  terraform -help\n\n')

Terraform Config Parser

TerraformConfig is used to parse Terraform config files.

For now, only supply TerraformConfig.load_config_dir method which reads the .tf and .tf.json files in the given directory as config files and then combines these files into a single Module. This method returns (mod, diags) which are both dict, corresponding to the *Module and hcl.Diagnostic structures in Terraform respectively.

>>> from libterraform import TerraformConfig
>>> mod, _ = TerraformConfig.load_config_dir('your_terraform_configuration_directory')
>>> mod['ManagedResources'].keys()
dict_keys(['time_sleep.wait1', 'time_sleep.wait2'])

Version comparison

libterraform Terraform
0.5.0 1.3.0
0.4.0 1.2.2
0.3.1 1.1.7

Building & Testing

If you want to develop this library, should first prepare the following environments:

Then, initialize git submodule:

$ git submodule init
$ git submodule update

pip install necessary tools:

$ pip install poetry pytest

Now, we can build and test:

$ poetry build -f wheel
$ pytest

Why use this library?

Terraform is a great tool for deploying resources. If you need to call the Terraform command in the Python program for deployment, a new process needs to be created to execute the Terraform command on the system. A typical example of this is the python-terraform library. Doing so has the following problems:

  • Requires Terraform commands on the system.
  • The overhead of starting a new process is relatively high.

This library compiles Terraform as a dynamic link library in advance, and then loads it for calling. So there is no need to install Terraform, nor to start a new process.

In addition, since the Terraform dynamic link library is loaded, this library can further call Terraform's internal capabilities, such as parsing Terraform config files.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

libterraform-0.5.0-cp311-cp311-win_amd64.whl (35.7 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

libterraform-0.5.0-cp311-cp311-manylinux_2_31_x86_64.whl (35.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.31+ x86-64

libterraform-0.5.0-cp311-cp311-macosx_13_0_arm64.whl (35.3 MB view hashes)

Uploaded CPython 3.11 macOS 13.0+ ARM64

libterraform-0.5.0-cp311-cp311-macosx_12_0_x86_64.whl (37.8 MB view hashes)

Uploaded CPython 3.11 macOS 12.0+ x86-64

libterraform-0.5.0-cp311-cp311-macosx_12_0_arm64.whl (35.2 MB view hashes)

Uploaded CPython 3.11 macOS 12.0+ ARM64

libterraform-0.5.0-cp310-cp310-win_amd64.whl (35.7 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

libterraform-0.5.0-cp310-cp310-manylinux_2_31_x86_64.whl (35.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.31+ x86-64

libterraform-0.5.0-cp310-cp310-macosx_13_0_arm64.whl (35.3 MB view hashes)

Uploaded CPython 3.10 macOS 13.0+ ARM64

libterraform-0.5.0-cp310-cp310-macosx_12_0_x86_64.whl (37.8 MB view hashes)

Uploaded CPython 3.10 macOS 12.0+ x86-64

libterraform-0.5.0-cp310-cp310-macosx_12_0_arm64.whl (35.2 MB view hashes)

Uploaded CPython 3.10 macOS 12.0+ ARM64

libterraform-0.5.0-cp39-cp39-win_amd64.whl (35.7 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

libterraform-0.5.0-cp39-cp39-manylinux_2_31_x86_64.whl (35.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.31+ x86-64

libterraform-0.5.0-cp39-cp39-macosx_13_0_arm64.whl (35.3 MB view hashes)

Uploaded CPython 3.9 macOS 13.0+ ARM64

libterraform-0.5.0-cp39-cp39-macosx_12_0_x86_64.whl (37.8 MB view hashes)

Uploaded CPython 3.9 macOS 12.0+ x86-64

libterraform-0.5.0-cp39-cp39-macosx_12_0_arm64.whl (35.2 MB view hashes)

Uploaded CPython 3.9 macOS 12.0+ ARM64

libterraform-0.5.0-cp38-cp38-win_amd64.whl (35.7 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

libterraform-0.5.0-cp38-cp38-manylinux_2_31_x86_64.whl (35.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.31+ x86-64

libterraform-0.5.0-cp38-cp38-macosx_13_0_arm64.whl (35.3 MB view hashes)

Uploaded CPython 3.8 macOS 13.0+ ARM64

libterraform-0.5.0-cp38-cp38-macosx_12_0_x86_64.whl (37.8 MB view hashes)

Uploaded CPython 3.8 macOS 12.0+ x86-64

libterraform-0.5.0-cp38-cp38-macosx_12_0_arm64.whl (35.2 MB view hashes)

Uploaded CPython 3.8 macOS 12.0+ ARM64

libterraform-0.5.0-cp37-cp37m-win_amd64.whl (35.7 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

libterraform-0.5.0-cp37-cp37m-manylinux_2_31_x86_64.whl (35.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.31+ x86-64

libterraform-0.5.0-cp37-cp37m-macosx_13_0_arm64.whl (35.3 MB view hashes)

Uploaded CPython 3.7m macOS 13.0+ ARM64

libterraform-0.5.0-cp37-cp37m-macosx_12_0_x86_64.whl (37.8 MB view hashes)

Uploaded CPython 3.7m macOS 12.0+ x86-64

libterraform-0.5.0-cp37-cp37m-macosx_12_0_arm64.whl (35.2 MB view hashes)

Uploaded CPython 3.7m macOS 12.0+ ARM64

Supported by

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