Package: terrascript
Looking for more contributorsIF 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.AboutPython-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 likeif/else,for/continue/breakortry/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.CompatibilityTerraform releasesTerraform 0.12introduced 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.TerraformTerrascriptNotes0.13.x0.9.xCleanup efforts and bug fixes, dropping support for Python <3.6, supporting Terraform 0.13.x0.12.x0.8.xTerrascript 0.8 are a (almost) complete rewrite0.12.x0.7.xNever released0.11.x0.6.xLast releases to support Terraform 0.11 and earlierTerrascript supports Python 3.6 and later.Module layoutPython-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 exampleThe following example has been taken from the official Terraform documentation
for theAWS Providerand 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 ofconfig.tf.jsonis 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)LinksDocumentationfor Python-Terrascript.Githubpage of Python-Terrascript.Community Chaton Zulip.Terraform JSONsyntax.