Providers
Providers are Terraform plugins that define resources and data sources for practitioners to use. Providers are wrapped by a provider server for interacting with Terraform.
This page describes the basic implementation details required for defining a provider. Further documentation is available for deeper provider concepts:
- Configure data sources with provider-level data types or clients.
- Configure resources with provider-level data types or clients.
- Configure ephemeral resources with provider-level data types or clients.
- Validate practitioner configuration against acceptable values.
Define Provider Type
Implement the provider.Provider interface. Each of the methods described in more detail below.
In this example, a provider implementation is scaffolded:
Conventionally, many providers also create a helper function named New
which can simplify provider server implementations.
Metadata Method
The provider.Provider
interface Metadata
method defines information about the provider itself, such as its type name and version. This information is used to simplify creating data sources and resources.
In this example, the provider type name is set to examplecloud
:
Schema Method
The provider.Provider
interface Schema
method defines a schema describing what data is available in the provider's configuration. This configuration block is used to offer practitioners the opportunity to supply values to the provider and configure its behavior, rather than needing to include those values in every resource and data source. It is usually used to gather credentials, endpoints, and the other data used to authenticate with the API, but it is not limited to those uses.
During the terraform validate
, terraform plan
and terraform apply
commands, Terraform calls the provider GetProviderSchema
RPC, in which the framework calls the provider.Provider
interface Schema
method.
In this example, a sample configuration and schema definition are provided:
If the provider does not accept practitioner Terraform configuration, leave the method defined, but empty.
Configure Method
The provider.Provider
interface Configure
method handles the configuration of any provider-level data or clients. These configuration values may be from the practitioner Terraform configuration, environment variables, or other means such as reading vendor-specific configuration files.
During the terraform plan
and terraform apply
commands, Terraform calls the provider ConfigureProvider
RPC, in which the framework calls the provider.Provider
interface Configure
method.
This is the only chance the provider has to configure provider-level data or clients, so they need to be persisted if other data source or resource logic will need to reference them. Refer to the Configure Data Sources and Configure Resources pages for additional implementation details.
If the logic needs to return warning or error diagnostics, they can added into the provider.ConfigureResponse.Diagnostics
field.
In this example, the provider API token and endpoint are configured via environment variable or Terraform configuration:
Unknown Values
Not all values are guaranteed to be
known when Configure
is called.
For example, if a practitioner interpolates a resource's unknown value into the block,
that value may show up as unknown depending on how the graph executes:
In the example above, random_string.example.result
is a read-only field on
random_string.example
that won't be set until after random_string.example
has been
applied. So the Configure
method for the provider may report that the value
is unknown. You can choose how your provider handles this. If
some resources or data sources can be used without knowing that value, it may
be worthwhile to emit a warning and
check whether the value is set in resources and data sources before attempting
to use it. If resources and data sources can't provide any functionality
without knowing that value, it's often better to return an
error, which will halt the apply.
Resources
The provider.Provider
interface Resources
method returns a slice of resources. Each element in the slice is a function to create a new resource.Resource
so data is not inadvertently shared across multiple, disjointed resource instance operations unless explicitly coded. Information such as the resource type name is managed by the resource.Resource
implementation.
The provider.Provider
interface Resources
method is called during execution of the terraform validate
, terraform plan
and terraform apply
commands when the ValidateResourceConfig
, ReadResource
, PlanResourceChange
and ApplyResourceChange
RPCs are sent.
In this example, the provider implements a single resource:
Use Go slice techniques to include large numbers of resources outside the provider Resources
method code.
In this example, the provider codebase implements multiple "services" which group their own resources:
DataSources
The provider.Provider
interface DataSources
method returns a slice of data sources. Each element in the slice is a function to create a new datasource.DataSource
so data is not inadvertently shared across multiple, disjointed datasource instance operations unless explicitly coded. Information such as the datasource type name is managed by the datasource.DataSource
implementation.
The provider.Provider
interface DataSources
method is called during execution of the terraform validate
, terraform plan
and terraform apply
commands when the ValidateDataResourceConfig
and ReadDataSource
RPCs are sent.
In this example, the provider implements a single data source:
Use Go slice techniques to include large numbers of data sources outside the provider DataSources
method code.
In this example, the provider codebase implements multiple "services" which group their own datasources: