devops-workflow / python-terrascript

Create Terraform files using Python scripts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

python-terrascript

PyPI license GitHub issues Travis Codecov

Terrascript provides a method of generating Terraform files, while harnessing all the features the Python 3 (3.3+) language provides.

IMPORTANT: Release 0.5.0 introduced changes that are not backwards compatible with earlier releases.

Example

As an example let's translate the following Terraform configuration into Terrascript.

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.micro"
}

The equivalent terrascript example would look like this.

from terrascript import Terrascript, provider
from terrascript.aws.r import aws_instance

ts = Terrascript()

# Add a provider (+= syntax)
ts += provider('aws', access_key='ACCESS_KEY_HERE',
               secret_key='SECRET_KEY_HERE', region='us-east-1')

# Add an AWS EC2 instance (add() syntax).
inst = ts.add(aws_instance('example', ami='ami-2757f631', instance_type='t2.micro'))

# Print the JSON-style configuration to stdout.
print(ts.dump())

Creating instances of provider and aws_instance will automatically add them to the Terraform configuration. Calling dump() will return the configuration in JSON format.

{
  "provider": {
    "aws": {
      "access_key": "ACCESS_KEY_HERE",
      "region": "us-east-1",
      "secret_key": "SECRET_KEY_HERE"
    }
  },
  "resource": {
    "aws_instance": {
      "example": {
        "ami": "ami-2757f631",
        "instance_type": "t2.micro"
      }
    }
  }
}

IMPORTANT: Terrascript does not perform any error checking whatsoever. It is entirely up to you to ensure that the generated output makes sense to Terraform.

Documentation

Status

Terrascript works with Terraform release 0.10.6 and later.

All Terraform providers are supported but most haven't seen any testing at all. Please let me know if you run into any problems.

I'd also like to add more examples.

FAQ

About

Create Terraform files using Python scripts.

License:BSD 2-Clause "Simplified" License


Languages

Language:Python 99.1%Language:Makefile 0.9%