Apply/Destroy Terraform Modules via a Simple REST API Endpoint
This tool makes it easier for your to use Terraform runs in a simple REST API.
Join the DZone community and get the full member experience.
Join For FreeTerraform is a great tool for infrastructure as code and one of my personal favorites, but while it is very easy to apply and destroy runs via a CLI and having plenty of tools to ensure Terraform runs via git push/pull requests, applying Terraform runs via an API command is a lot trickier.
That is why I created Terraformize to fill in that gap, the basic idea being that, by having the ability to create Terraform runs via a simple REST API, a developer can integrate a Terraform environment creation into their product code flow in an easy way.
Features
- REST API to run Terraform apply/destroy
- No code changes needed and it supports 100% of all Terraform modules unmodified
- Built-in support for multiple Terraform workspaces
- Can pass variables to the Terraform run via the request body (passed as a
-var
argument to theterraform apply
orterraform destroy
command) - Supports multiple module directories
- Automatically runs
terraform init
before changes - Returned response includes all the logs of
stdout
andstderr
of Terraform for easy debugging - Stateless (requires you use a nonlocal Terraform backend)
- Containerized
- Health check endpoint included
- Support all Terraform backends that support multiple workspaces
- No database is needed and all the data stored at the Terraform backend of your choosing
- Terraformize scales out as much as you need risk-free (requires you use a backend that supports state locking)
- AMD64 and Arm support (Arm64 not supported as there is no current binary for Terraform for it).
Possible Use Cases
- Setting up SaaS clusters and products for clients in a fully automatic way
- CI/CD integration
- Automatic system creation and\or scaling
Case Study Example
1. First, we will need a Terraform module, so create a folder named terraformize_test
:
mkdir terraformize_test
2. Now we need a valid Terraform configuration in it, if it works in Terraform it will work with Terraformize, but for this example, we will keep it simple with a single terraformize_test/test.tf
file:
resource "null_resource" "test" {
count = 1
}
variable "test_var" {
description = "an example variable"
default = "my_variable_default_value"
}
output "test" {
value = var.test_var
}
3. We will also need to add the folder we created into the Terraformize container. This can be done in many different ways. For example, creating a container that copies our modules into a new image with the FROM
base image being Terraformize base image, but for this example, we will simply mount the folder path into the container as we run it:
docker run -d -p 80:80 -v `pwd`:/www/terraform_modules/ naorlivne/terraformize
4. Now we can run the Terraform module by simply calling it, which will run terraform apply
for us (notice how we are passing variables in the body):
curl -X POST \
http://127.0.0.1/v1/terraformize_test/my_workspace \
-H ‘Content-Type: application/json’ \
-H ‘cache-control: no-cache’ \
-d ‘{
“test_var”: “hello-world”
}’
5. And let's create another copy of the infrastructure of the same module in another workspace:
curl -X POST \
http://127.0.0.1/v1/terraformize_test/my_other_workspace \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"test_var": "hello-world"
}'
6. Now that we are done, let’s delete them both (this will run terrafrom destroy
for us):
curl -X DELETE \
http://127.0.0.1/v1/terraformize_test/my_workspace \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"test_var": "hello-world"
}'
curl -X DELETE \
http://127.0.0.1/v1/terraformize_test/my_other_workspace \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"test_var": "hello-world"
}'
Further Reading
Opinions expressed by DZone contributors are their own.
Comments