Terraform Advanced — Custom Providers Exercises

1 minute read

Practice custom provider concepts.


Exercise 1 — Choose the Right Approach

Scenario: You need to create DNS records in an internal API that has a RESTful interface. Which approach?

Answer

Use the restapi provider. It’s a REST CRUD API, so a full custom provider is overkill.

resource "restapi_object" "dns" {
  path = "/api/v1/dns"
  data = jsonencode({
    name = "app.internal.example.com"
    type = "A"
    value = "10.0.1.50"
  })
}

Exercise 2 — Provider Schema

Task: Write the schema for a resource with required name (string), optional region (string, default “us-east-1”), and computed id (string).

Answer
resp.Schema = schema.Schema{
    Attributes: map[string]schema.Attribute{
        "id": schema.StringAttribute{
            Computed: true,
        },
        "name": schema.StringAttribute{
            Required: true,
        },
        "region": schema.StringAttribute{
            Optional: true,
            Computed: true,
            Default:  stringdefault.StaticString("us-east-1"),
        },
    },
}

Exercise 3 — Local Dev Override

Task: Write the ~/.terraformrc dev override to test a local provider binary at /home/user/go/bin/terraform-provider-myapi.

Answer
provider_installation {
  dev_overrides {
    "registry.terraform.io/myorg/myapi" = "/home/user/go/bin"
  }
  direct {}
}

Note: the path points to the directory containing the binary, not the binary itself.


Chapter Summary

  • Build custom providers only when simpler alternatives don’t work
  • Use terraform-plugin-framework for new providers (not SDKv2)
  • Providers communicate with Terraform core via gRPC
  • restapi, shell, and http providers cover many use cases
  • Use dev_overrides in ~/.terraformrc for local testing

← Provider Alternatives Chapter 9: Testing →

Updated: