DEV Community

Cover image for Custom providers for Terraform
Pavel Kutáč
Pavel Kutáč

Posted on

Custom providers for Terraform

Terraform is a tool for managing infrastructure with config files. It supports many cloud providers like GCP, AWS but also Datadog, and many others. But if you are developing your own service, which should be manageable by Terraform, you have to write your own provider.

🇨🇿 V češtině si lze článek přečíst na

Manage infrastructure without Terraform means to log in to the Console of the provider and set up everything by clicking in the UI. However, it is hard to backup, version, and replicate for other environments like staging. And that is the reason why we have Terraform. If you don't know much about Terraform, watch Terraform in 100s video before we dive into writing own providers.

Basic schema of Terraform

Behind the scenes

Before writing own provider, let's look at how Terraform works in the background. Terraform first reads all *.tf files and prepares dependency graph - the execution plan. Based on the plan Terraform then can perform CRUD operations. Those methods must be implemented in the provider which is responsible for calling backend API. So Terraform does not care if you use REST, gRPC, or SOAP gRPC API.

Terraform core just parses the file and performs the plan by calling the provider's CRUD functions. Theoretically, someone could write blog posts with Terraform.

Schema of communication between Terraform Core and backend API

Demo app

I created a sample project which contains a simple HTTP server with REST API. And also a custom provider which is communicating with that HTTP server. So you can easily play with the provider and Terraform. Full code can be found on my GitHub

# Start the HTTP server
make start_server 

# Build Terraform provider
make terraform_build 

# All *.tf files are in config subfolder
cd provider/config 
terraform plan # to show plan which will be executed
terraform apply # to execute the plan
Enter fullscreen mode Exit fullscreen mode

Discussion (0)