Python binding for Terraform.

Overview

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.1.7', 'platform': 'darwin_arm64', 'provider_selections': {}, 'terraform_outdated': False}
>>> TerraformCommand().version(json=False)
<CommandResult retcode=0 json=False>
>>> _.value
'Terraform v1.1.7\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.1.7\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'])

Building & Testing

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

  • GoLang (Version 1.17.x or 1.16.x)
  • Python (Version 3.6~3.10)
  • GCC

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.

You might also like...
PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

PRAW, an acronym for "Python Reddit API Wrapper", is a python package that allows for simple access to Reddit's API.

PRAW: The Python Reddit API Wrapper PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's AP

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API.

alpaca-trade-api-python is a python library for the Alpaca Commission Free Trading API. It allows rapid trading algo development easily, with support for both REST and streaming data interfaces

🖥️ Python - P1 Monitor API Asynchronous Python Client

🖥️ Asynchronous Python client for the P1 Monitor

Volt is yet another discord api wrapper for Python. It supports python 3.8 +

Volt Volt is yet another discord api wrapper for Python. It supports python 3.8 + How to install [Currently Not Supported.] pip install volt.py Speed

Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot
Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring files on the Internet to our beloved Google Drive. Based on python-aria-mirror-bot

- [ MAYBE UPDATE & ADD MORE MODULE ] Bagas Mirror&Leech Bot Bagas Mirror&Leech Bot is a multipurpose Telegram Bot written in Python for mirroring file

A python Discord wrapper made in well, python.

discord.why A python Discord wrapper made in well, python. Made to be used by devs who want something a bit more, general. Basic Examples Sending a me

A wrapper for aqquiring Choice Coin directly through a Python Terminal. Leverages the TinyMan Python-SDK.

CHOICE_TinyMan_Wrapper A wrapper that allows users to acquire Choice Coin directly through their Terminal using ALGO and various Algorand Standard Ass

Python On WhatsApp - Run your python codes on whatsapp along with talking to a chatbot
Python On WhatsApp - Run your python codes on whatsapp along with talking to a chatbot

Python On WhatsApp Run your python codes on whatsapp along with talking to a chatbot This is a small python project to run python on whatsapp. and i c

Comments
  • Could not find a version that satisfies the requirement libterraform

    Could not find a version that satisfies the requirement libterraform

    $ pip install libterraform ERROR: Could not find a version that satisfies the requirement libterraform (from versions: none) ERROR: No matching distribution found for libterraform $

    opened by judab5ericom 0
  • ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    getting the following error when trying to compile local Installing build dependencies ... done Getting requirements to build wheel ... done Preparing wheel metadata ... done /home/judab/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.23ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( /home/judab/.local/lib/python3.8/site-packages/pkg_resources/init.py:123: PkgResourcesDeprecationWarning: 0.1.36ubuntu1 is an invalid version and will not be supported in a future release warnings.warn( Building wheels for collected packages: libterraform Building wheel for libterraform (PEP 517) ... error ERROR: Command errored out with exit status 1: command: /usr/bin/python3 /tmp/tmpjxsbgvg0 build_wheel /tmp/tmpmuv6vjmq cwd: /tmp/pip-req-build-4kwbn1bw Complete output (33 lines):

    sigs.k8s.io/json/internal/golang/encoding/json

    /home/judab/go/pkg/mod/sigs.k8s.io/[email protected]/internal/golang/encoding/json/encode.go:1249:12: sf.IsExported undefined (type reflect.StructField has no field or method IsExported) /home/judab/go/pkg/mod/sigs.k8s.io/[email protected]/internal/golang/encoding/json/encode.go:1255:18: sf.IsExported undefined (type reflect.StructField has no field or method IsExported) - Patching go-plugin package - Building libterraform Traceback (most recent call last): File "build.py", line 72, in build({}) File "build.py", line 54, in build subprocess.check_call( File "/usr/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['go', 'build', '-buildmode=c-shared', '-o=libterraform.so', 'github.com/hashicorp/terraform']' returned non-zero exit status 2. Traceback (most recent call last): File "/tmp/tmpjxsbgvg0", line 280, in main() File "/tmp/tmpjxsbgvg0", line 263, in main json_out['return_val'] = hook(**hook_input['kwargs']) File "/tmp/tmpjxsbgvg0", line 204, in build_wheel return _build_backend().build_wheel(wheel_directory, config_settings, File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/api.py", line 68, in build_wheel return unicode(WheelBuilder.make_in(poetry, Path(wheel_directory))) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 78, in make_in wb.build() File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 106, in build self._build(zip_file) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 152, in _build self._run_build_script(self._package.build_script) File "/tmp/pip-build-env-e48g7589/overlay/lib/python3.8/site-packages/poetry/core/masonry/builders/wheel.py", line 202, in _run_build_script subprocess.check_call([self.executable.as_posix(), build_script]) File "/usr/lib/python3.8/subprocess.py", line 364, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['/usr/bin/python3', 'build.py']' returned non-zero exit status 1.

    ERROR: Failed building wheel for libterraform Failed to build libterraform ERROR: Could not build wheels for libterraform which use PEP 517 and cannot be installed directly

    opened by judab5ericom 0
Releases(v0.4.0)
  • v0.3.1(Apr 19, 2022)

  • v0.3.0(Apr 6, 2022)

    TerraformCommand supports all Terraform commands. Compared with v0.2.1, the current version supports the following commands:

    • fmt
    • force unlock (The corresponding function is force_unlock)
    • graph
    • import (The corresponding function is import_resource)
    • refresh
    • state and all sub commands of state
    • taint
    • untaint
    • test
    • workspace and all sub commands of workspace

    The project adds Makefile so we can use make command, like make init, make test, make build etc.

    Source code(tar.gz)
    Source code(zip)
  • v0.2.1(Mar 31, 2022)

    Implement TerraformCommand which is used to invoke various Terraform commands.

    Currently, supports version and all main commands (init, validate, plan, show, apply and destroy), returning 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.
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Mar 11, 2022)

    Implement TerraformConfig which 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.

    Source code(tar.gz)
    Source code(zip)
Owner
Prodesire
Pythonista. Microsoft MVP.
Prodesire
Cookies is a project inspired by python cookiecutter but used for terraform generation.

Introduction Cookies is a project inspired by python cookiecutter but used for terraform generation. How to run your terraform After you download your

Digger 6 Mar 14, 2022
tfquery: Run SQL queries on your Terraform infrastructure. Query resources and analyze its configuration using a SQL-powered framework.

??️ tfquery ??️ Run SQL queries on your Terraform infrastructure. Ask questions that are hard to answer ?? What is tfquery? tfquery is a framework tha

Mazin Ahmed 311 Dec 21, 2022
MAASTA is a wrapper to create an Ansible inventory for MAAS instances that are provisioned by Terraform.

MAASTA is a wrapper to create an Ansible inventory for MAAS instances that are provisioned by Terraform.

Saeid Bostandoust 144 Dec 16, 2022
Terraform module to ship CloudTrail logs stored in a S3 bucket into a Kinesis stream for further processing and real-time analysis.

AWS infrastructure to ship CloudTrail logs from S3 to Kinesis This repository contains a Terraform module to ship CloudTrail logs stored in a S3 bucke

Nexthink 8 Sep 20, 2022
Python binding for Microsoft LightGBM

pyLightGBM: python binding for Microsoft LightGBM Features: Regression, Classification (binary, multi class) Feature importance (clf.feature_importanc

Ardalan 330 Nov 18, 2022
Python binding to the OneTimeSecret API

Thin Python binding for onetimesecret.com API. Unicode-safe. Description of API itself you can find here: https://onetimesecret.com/docs/api Usage:

Vladislav Stepanov 10 Jun 12, 2022
fhempy is a FHEM binding to write modules in Python language

fhempy (BETA) fhempy allows the usage of Python 3 (NOT 2!) language to write FHEM modules. Python 3.7 or higher is required, therefore I recommend usi

Dominik 27 Dec 14, 2022
Official Python client for the MonkeyLearn API. Build and consume machine learning models for language processing from your Python apps.

MonkeyLearn API for Python Official Python client for the MonkeyLearn API. Build and run machine learning models for language processing from your Pyt

MonkeyLearn 157 Nov 22, 2022