DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]

Trending

  • Content Lakes: Harness Unstructured Data for Enterprise AI Readiness
  • Architecting Petabyte-Scale Hyperspectral Pipelines on AWS
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  • RAG Done Right: When to Use SQL, Search, and Vector Retrieval and How To Combine Them
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Apply/Destroy Terraform Modules via a Simple REST API Endpoint

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.

By 
Naor Livne user avatar
Naor Livne
·
Oct. 29, 19 · Presentation
Likes (3)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free

rest

Now you can do even more with your REST.

Terraform 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 the terraform apply or terraform destroy command)
  • Supports multiple module directories
  • Automatically runs terraform init before changes
  • Returned response includes all the logs of stdout and stderr 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

Terraform CLI Cheat Sheet

Terraform (software) REST API Web Protocols Kubernetes Infrastructure as code

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook