Header Banner Header Banner
Topics In Demand
Notification
New

No notification found.

Developing Custom Terraform Plugins
Developing Custom Terraform Plugins

July 29, 2025

19

0

Terraform plugin development empowers you to automate infrastructure provisioning across various cloud platforms, streamlining your workflow with expert Cloud Automation services. But what if your needs extend beyond these built-in functionalities, and your infrastructure relies on a unique API? Here's where custom Terraform providers come to the rescue!

Imagine managing any service with an API directly through Terraform. Custom Terraform providers unlock this potential, granting you complete control and seamless integration within your existing workflows. In this blog, we'll explore why you might need a terraform plugin and then guide you through the terraform provider development process step-by-step.

What is a Terraform provider?

A terraform plugin, specifically a custom Terraform provider, is a Go binary plugin responsible for interacting with cloud APIs or self-hosted APIs. The provider implements a set of resources and data sources that Terraform can manage. In a nutshell, terraform plugins bridge the gap between Terraform and external services by translating Terraform's configuration language (HCL) into API calls, and the target service understands and interprets the responses back to Terraform. There are built-in providers for many services, but custom Terraform providers unlock the potential to manage resources from any unique API.

Terraform can be broken down into two key components: Terraform Core and Terraform Plugins

  • Terraform Core: This is the Terraform binary written in the GO programming language that communicates with plugins through RPC (remote procedure calls). Terraform core is responsible for reading the configuration, building the resource dependency graph, managing the resources' states, executing Terraform plans (e.g., Terraform plan, apply, destroy, refresh), and communicating with the custom Terraform providers/plugins.
  • Terraform Plugins: These are separately executable binaries written in Go that communicate with Terraform Core over an RPC interface. Each Terraform plugin exposes an implementation for a specific application. Plugin is responsible for communicating with the actual application/service eg. initialization of REST libraries used to make API calls, authentication for application.

Custom Terraform Provider

Writing a Terraform plugin

Let's build a custom Terraform plugin that will help configure the simple Python-based Flask application.

Prerequisites:

  1. Let's create a simple Flask web application that can manage the information for a single resource: a Person. This application exposes APIs to perform CRUD operations for a person. Install the Person app from the GitHub repository.
  2. Download and install Go 1.13+ by following the official installation guide.
  3. Install Terraform 0.13+. Refer here for installing Terraform.

Let's write a simple Terraform plugin to help configure the Person app.

1. Create a source code directory for Terraform plugin inside $GOPATH/src. 

Example:

The code structure will look like this:

2. Create a REST client library to interact with the Person application APIs for CRUD operations. Refer to rest.go for implementation details.

3. Then write the provider.go, which will read the provider block from the Terraform plan and initialize the session for our application. Here, define the schema for the Terraform provider block. Along with it, map the available data sources and resources for the app you will implement in the following steps. Implement a single available resource from the application, i.e., a person.


4. Implement the resources we have just mapped to the provider.go. Map the methods to perform the CRUD operations for the resource. As there is only one resource (person) in the current application, create resource_person.go and implement CRUD to manage person.


5. Now, configure our newly created terraform plugin in main.go.

6. Build the Terraform plugin

  • This command will create binary terraform-provider-person_v99.9.1
  • Run following commands to check your os and architecture
  • Check if the ${HOME}/.terraform.d/plugins/personterraform/person/person/99.9.1/<GOOS>_<GOARCH> path exists on your machine. If not then create it. Replace <GOOS> and <GOARCH> with the respective command output.
  • Copy terraform-provider-person_v99.9.1 binary into directory ${HOME}/.terraform.d/plugins/personterraform/person/person/99.9.1/<GOOS>_<GOARCH>/
  • Here we have considered 99.9.1 as a version of our plugin. Will use this version while writing our terraform plan.
  • Now the plugin is ready to use.

    To test the provider:

    - Create a Terraform plan for the provider
    - Run Terraform commands
    Create Terraform plan

    Create declarative Terraform plans for our application using HCL (Hashicorp Configuration Language).


    Run Terraform commands

    This command will locate the binary based on the configuration we have provided in the required provider's block.

    Now, you can run Terraform commands, Terraform plan, Terraform apply, and Terraform destroy commands to create, update, and delete the configuration as needed.

    Conclusion

    In conclusion, custom Terraform providers empower you to bridge the gap between Terraform and any API-driven service. This unlocks the potential to manage unique in-house systems, niche cloud offerings, and more – all within your existing Terraform workflows. You can publish Terraform provider by following the terraform documentation. We can write the custom Terraform Provider for any API based application. Please refer to this for the source code. Explore the possibilities and unleash the full potential of your infrastructure automation! Need help with your custom plugin? Contact our Terraform experts to streamline your workflows.


That the contents of third-party articles/blogs published here on the website, and the interpretation of all information in the article/blogs such as data, maps, numbers, opinions etc. displayed in the article/blogs and views or the opinions expressed within the content are solely of the author's; and do not reflect the opinions and beliefs of NASSCOM or its affiliates in any manner. NASSCOM does not take any liability w.r.t. content in any manner and will not be liable in any manner whatsoever for any kind of liability arising out of any act, error or omission. The contents of third-party article/blogs published, are provided solely as convenience; and the presence of these articles/blogs should not, under any circumstances, be considered as an endorsement of the contents by NASSCOM in any manner; and if you chose to access these articles/blogs , you do so at your own risk.


© Copyright nasscom. All Rights Reserved.