Provider schema and Plugin Framework
A provider translates intent into service operations
A Terraform provider is a separate plugin that defines what infrastructure objects mean and how to manage them. The Plugin Framework is HashiCorp’s recommended Go library for new Terraform plugins; SDKv2 remains important when maintaining older providers. Core owns the graph and orchestration, while the provider implements the object contract. In this chapter, the service is a bounded in-memory note store, not a cloud API. Its records last only for one Go test process. The fixture tests service behavior, not a running Terraform provider.
Fictional provider: labProvider configuration: no credentials or remote endpointManaged lab_note: name (required), id (computed), body (required), category (optional)Read-only lab_note lookup: id (required), name and body (computed)Optional future credential input: sensitive; not part of this fixtureIdentity: immutable id; name and body may be updatedYou need Go structs, methods, interfaces, error handling, context cancellation, modules and the testing package. Learn these before attempting a real plugin. Pin a compatible Go toolchain, framework module, plugin protocol and Terraform CLI together using the applicable release documentation. This chapter does not assert an exact compatible combination.
Define the schema before implementing methods
A schema describes the shape of the configuration and the values Terraform may track. Required means the practitioner must supply the attribute. Optional permits omission, and computed permits provider-generated values. Optional plus computed is useful for attributes that may be chosen by the user or by the service. Sensitive marks a value for restricted display; it does not encrypt persisted data. Our toy note needs a name and a body, a service-assigned identifier and an optional category. The category belongs to the design worksheet; the executable Go fixture focuses on identity, name and body.
// Illustrative method fragment; framework imports and surrounding type omitted.func (r *noteResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{Attributes: map[string]schema.Attribute{ "name": schema.StringAttribute{Required: true}, "body": schema.StringAttribute{Required: true}, "id": schema.StringAttribute{Computed: true}, "category": schema.StringAttribute{Optional: true}, }}}Here schema refers to the resource schema package. This fragment is not a buildable provider. A complete plugin needs provider registration, model types, metadata, lifecycle methods, a server entry point and dependency pins. Do not copy it into a live configuration expecting Terraform to install a provider called lab.
Preserve the distinction between null and unknown
Framework value types such as types.String retain Terraform-specific information that a plain Go string cannot represent. Null denotes absence and unknown denotes a value that has not been determined yet. An empty string is a known value. A validator should avoid treating an unknown input as an invalid empty value; validate known values and check required data at the appropriate lifecycle phase. A required attribute can still be unknown while Terraform is planning.
// Illustrative model and validation branch; not a standalone program.type noteModel struct { ID types.String `tfsdk:"id"` Name types.String `tfsdk:"name"` Body types.String `tfsdk:"body"` Category types.String `tfsdk:"category"`}// Inside a validator for a configured name:if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { return}// Check the known string against the documented name rules.Requiredness is enforced by schema; the validator adds domain rules such as a nonblank name. A plan modifier changes what the plan predicts, within Terraform consistency constraints. For example, a genuinely immutable attribute may use a replacement modifier. Do not use a prior state value to conceal a legitimate change or a value that the service may recalculate. The ID in this toy contract remains stable during in-place edits.
Provider configuration and data sources
The provider implements schema and configuration methods, exposes resource and data source constructors, and can prepare shared clients for those implementations. Resource Configure methods can receive the configured client; they must handle an absent configuration safely during validation and reject unexpected client types with diagnostics. Do not make network calls simply to construct schemas. For an actual provider, credentials should come from approved channels; a mock must never fall back to a real endpoint. A data source exposes a lookup, not ownership of a managed object. A managed resource is responsible for reconciling its own object.
# Fictional HCL consumer; no published provider or executable configuration.resource "lab_note" "example" { name = "onboarding" body = "Read the local guide"}data "lab_note" "lookup" { id = lab_note.example.id}The resource returns the new ID. The lookup depends on that value and reads the object. A data source should report a failed lookup as an appropriate diagnostic rather than silently creating a new note. A provider with an in-memory client cannot pretend that notes persist across separate Terraform plugin processes; a real multi-process integration test would need a deliberately isolated persistent mock service.
Choose the correct extension interface
Not every operation belongs in resource CRUD. A provider-defined function exposes computation in Terraform expressions; an ephemeral resource supports temporary values not stored in ordinary plan or state artifacts; an action represents an operation outside the normal managed-resource lifecycle. Each has its own interface, tests and minimum compatible versions. Consult the framework and CLI version-specific documentation before implementing them. Ephemeral storage semantics do not guarantee secrets cannot leak through external systems or logs. Actions may have side effects and need authorization and review. For this lesson, a normalization function could return a normalized name, while a hypothetical action might export notes to an approved local destination. Neither is implemented or invoked in the fixture.
Common mistakes and design review
Avoid marking every field computed merely to suppress a plan difference. Avoid replacing a mutable object because a field changed. Avoid ignoring framework diagnostics after decoding a model. Do not use ordinary Go values when you need to preserve typed unknown or null semantics. Review the schema against the actual API behavior: which fields are user-owned, which values are returned by the service, which attributes can change in place, and what happens when the object is absent? Write the contract first; the next lesson implements and tests the bounded service behavior that a future provider adapter would consume.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Provider schema and Plugin Framework — knowledge check
Review provider schema and lifecycle contracts.
Which package is recommended for new Go Terraform providers?
Which schema flag is appropriate for a service-assigned ID?
A name is unknown at plan time. What should a validator do?
What is a data source normally responsible for?
Does Sensitive encrypt state?