Skip to main content
https://img.shields.io/github/license/mjuenema/python-terrascript https://img.shields.io/travis/mjuenema/python-terrascript https://img.shields.io/pypi/v/terrascript https://img.shields.io/pypi/pyversions/terrascript https://img.shields.io/pypi/dm/terrascript https://img.shields.io/github/issues/mjuenema/python-terrascript https://img.shields.io/github/stars/mjuenema/python-terrascript https://img.shields.io/badge/zulip-join_chat-brightgreen.svg

Looking for more contributors

IF you feel that this project is useful to you, please consider contributing some of your time towards improving it! For more details on contributions, please have a look at CONTRIBUTORS.md and DEVELOPMENT.md.

About

Python-Terrascript is a Python package for generating Terraform configurations in JSON format.

Creating Terraform through a Python script offers a degree of flexibility superior to writing Terraform configurations by hand.

  • Control structures like if/else, for/continue/break or try/except/finally.

  • More string methods.

  • Python functions may be used as an alternative to Terraform Modules.

  • Access to the Python Standard Library and third-party packages.

Compatibility

Terraform releases

Terraform 0.12 introduced some changes to how it deals with configuration files in JSON format. This is reflected in Terrascript by currently having separate releases for Terraform 0.12 and Terraform 0.11. Earlier releases of Terraform are not supported.

Terraform

Terrascript

Notes

0.13.x

0.9.x

Cleanup efforts and bug fixes, dropping support for Python <3.6, supporting Terraform 0.13.x

0.12.x

0.8.x

Terrascript 0.8 are a (almost) complete rewrite

0.12.x

0.7.x

Never released

0.11.x

0.6.x

Last releases to support Terraform 0.11 and earlier

Terrascript supports Python 3.6 and later.

Module layout

Python-Terrascript release 0.8.0 changed the location of modules. Providers, resources and data sources are now all available through just three modules.

import terrascript
import terrascript.provider     # aws, google, ...
import terrascript.resource     # aws_instance, google_compute_instance, ...
import terrascript.data         # aws_ami, google_compute_image, ...

The legacy layout is still available but should not be used for new projects.

import terrascript
import terrascript.aws          # aws
import terrascript.aws.r        # aws_instance, ...
import terrascript.aws.d        # aws_ami, ...

A first example

The following example has been taken from the official Terraform documentation for the AWS Provider and then converted into a Python script that generates the equivalent configuration in JSON syntax.

The original Terraform HCL format.

provider "aws" {
  version = "~> 2.0"
  region  = "us-east-1"
}

resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

The Terrascript code would look like this.

import terrascript
import terrascript.provider as provider
import terrascript.resource as resource

config = terrascript.Terrascript()

config += provider.aws(version='~> 2.0', region='us-east-1')
config += resource.aws_vpc('example', cidr_block='10.0.0.0/16')

with open('config.tf.json', 'wt') as fp:
    fp.write(str(config))

The content of config.tf.json is shown below. It is equivalent to the original HCL format.

{
  "provider": {
    "aws": [
      {
        "version": "~> 2.0",
        "region": "us-east-1"
      }
    ]
  },
  "resource": {
    "aws_vpc": {
      "example": {
        "cidr_block": "10.0.0.0/16"
      }
    }
  }
}

Terrascript does not verify that the generated JSON code is a valid Terraform configuration. This is a deliberate design decision and is explained in the Frequently Asked Questions (FAQ)

Release files for terrascript 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for terrascript 0.9.0
File Size Uploaded
terrascript-0.9.0.tar.gz 85.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for terrascript 0.9.0
File Interpreter ABI Platform
terrascript-0.9.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 259.7 kB

Release files / terrascript-0.9.0.tar.gz

Download URL terrascript-0.9.0.tar.gz
Size 85.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5330802e3aa26d6c385e08139dc45e6cb6fb54ebcf3edcc3b155339f6fa4c4f3
BLAKE2b-256 checksum
How to use checksums
8314a45bf668e0413167538adffac85d2b58b7ea7c209fc9fed5d30ad1948e01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.6.7

Release files / terrascript-0.9.0-py2.py3-none-any.whl

Download URL terrascript-0.9.0-py2.py3-none-any.whl
Size 174.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1b823ab06b295143f6a1a32c2b6e68b2f63272b9f70e7b66676aa1929dc38552
BLAKE2b-256 checksum
How to use checksums
c5d373498d25910f4e64e9b48a3e6dd2f11d391b3a0e9456f61001c3c4253d84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.6.7

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.0

2 release files

0.6.1

2 release files

0.6.0

3 release files

0.5.1

2 release files

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page