Skip to content

Spikeophant/python-terrascript

 
 

Repository files navigation

python-terrascript

PyPI license GitHub issues Travis

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

Notes

  • Terrascript release 0.5.0 introduced changes that are not backwards compatible with earlier releases.
  • Terraform 0.12 seems to introduce some changes to its JSON syntax. I have not investigated this yet and any feedback is very welcome.

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 99.2%
  • Makefile 0.8%