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
Newsletter
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

  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • PWA on iOS, Part 1: PWAs on the iPhone Are Older Than They Look
  • Article Moderation: Your Questions, Answered
  • How to Build Your Own AI SRE From Scratch

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 29, 19 · Presentation
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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