Terrascript provides a method of generating Terraform files, while harnessing all the features the Python 3 language provides.
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 provider, dump
from terrascript.aws.r import aws_instance
provider('aws', access_key='ACCESS_KEY_HERE',
secret_key='SECRET_KEY_HERE', region='us-east-1')
aws_instance('example', ami='ami-2757f631', instance_type='t2.micro')
# Print the JSON-style configuration to stdout.
print(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.
This section lists some more advanced examples.
All Terraform providers (as of 21-Nov-2017) 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.