Terraform Advanced — Custom Providers Exercises
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-frameworkfor new providers (not SDKv2) - Providers communicate with Terraform core via gRPC
restapi,shell, andhttpproviders cover many use cases- Use
dev_overridesin~/.terraformrcfor local testing