In this article, I want to share my experience of creating a terraform provider from scratch, show how to add it to the Terraform and OpenTofu registries, and share some useful links and tips.
I wrote the provider for Dodo pizza, and I was inspired by
First of all, you need to decide which HashiCorp SDK you will use. For today HashiCorp offers 2 Go programming language Software Development Kits (SDKs) for building Terraform providers:
All articles that I found provide examples of limited sample APIs or include API calls inside the provider itself. But all “large” providers use Go libraries to interact with existing API. For example, Cloudflare provider uses their own
In my case, I used only one command to generate an almost fully functional client SDK for Dodo API:
swagger-codegen generate -i https://globalapi.dodopizza.com/api/swagger/v2/swagger.json -l go -o .
You can find dodo-go here:
I found several articles about terraform provider creation, from HashiCorp and independent writers:
I realized that some terraform providers use API libraries very similar to my generated dodo-go library, and it was super helpful to compare my provider with existing provider, for example: terraform-provider-authentik.
When your provider is ready, or you are happy with existing functionalities, you need to publish it to the Terraform registry and OpenTofu registry if you plan to use it with opentofu.
All the needed steps are well described in HashiCorp’s documentation:
And this article from Kevin Wang can be useful as well:
After publishing provider to the Terraform registry, publishing to OpenTofu will be much easier, because you have already done all the necessary presteps.
You need to create two issues: the first one is the provider itself, and second one is about the provider’s GPG Key.
Example of adding provider:
Example of adding GPG Key:
And that's it!
After your provider is added to all registries, you can add it to the config as any regular provider.
In my test configuration, I have three files and I want to retrieve data about available brands:
cat provider.tf
terraform {
required_providers {
dodo = {
source = "Nmishin/dodo"
version = "0.0.1"
}
}
}
provider "dodo" {}
cat main.tf
data "dodo_brand" "test" {}
cat outputs.tf
output "brands" {
value = data.dodo_brand.test.names
}
tofu plan
data.dodo_brand.test: Reading...
data.dodo_brand.test: Read complete after 1s [id=bf4b2096fab07279e4a6a9db5fb704b5]
Changes to Outputs:
+ brands = [
+ "dodopizza",
+ "doner42",
+ "drinkit",
]
You can apply this plan to save these new output values to the OpenTofu state, without changing any real infrastructure.
Right now everyone with basic programming skills and terraform/opentofu experience can create its own provider. There is so much information about how to start to write code and publish provider, but in this article I wanted to provide high level instruction with good useful links. And I hope my story will help you not to be afraid to start creating a provider from scratch and I am absolutely sure that you will be successful with it!