Sobes.tech
Junior — Middle

What are the ways to implement a custom provider in Terraform?

sobes.tech AI

Answer from AI

A custom provider in Terraform can be implemented in several ways:

  1. Using the Terraform Plugin SDK — the official way to create providers in Go. The SDK provides a set of tools and libraries for implementing provider logic, resources, and data sources.

  2. Terraform Plugin Framework — a more modern and convenient framework from HashiCorp for developing providers with improved support for new Terraform features.

  3. Using external providers via external data source — if the provider is implemented as a separate script or application, it can be integrated through the external data source, although this is not a full provider.

An example of a simple provider in Go using the Plugin SDK includes defining resources, their CRUD operations, and registering the provider in main.go.

// main.go
package main

import (
    "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
    "github.com/hashicorp/terraform-plugin-sdk/plugin"
)

func main() {
    plugin.Serve(&plugin.ServeOpts{
        ProviderFunc: func() *schema.Provider {
            return &schema.Provider{
                ResourcesMap: map[string]*schema.Resource{
                    "example_resource": resourceExample(),
                },
            }
        },
    })
}

Then, functions for resources and their state are implemented. This approach requires knowledge of Go and understanding of Terraform's architecture.

What are the ways to implement a custom provider in… - sobes.tech