Code Walkthrough
Terraform providers let Terraform communicate with third parties, such as cloud providers, SaaS providers, and other APIs. Terraform and Terraform providers use gRPC to communicate. Terraform operates as a gRPC client and providers operate as gRPC servers.
Each provider defines resources that let Terraform manage infrastructure objects and data sources that let Terraform read data. Terraform practitioners then write configuration to define resources, such as compute storage or networking resources. Terraform then communicates this configuration to the provider, and the provider creates the infrastructure.
This example provider shows the relationship between the required provider components. The resources and data sources in a typical provider interact with a cloud provider through an API, but the example only stores values in state.
Core Provider Components
A Terraform plugin provider requires at least the following components:
The provider wraps the resource(s) and/or data source(s), and can be used to configure a client which communicates with a 3rd party service via an API. Resources are used to manage infrastructure objects. Data sources are used to read infrastructure objects.
Provider Server
Each provider must implement a gRPC server that supports Terraform-specific connection and handshake handling on startup. A provider server is required in order for a Terraform provider to:
- expose resources that can be managed by Terraform core.
- expose data sources that can be read by Terraform core.
The main()
function is used for defining a provider server.
The provider.New()
returns a function which returns a type that satisfies the provider.Provider
interface. The provider.Provider
interface defines functions for obtaining the resource(s) and/or data source(s) from a provider.
Refer to Provider Servers for more details.
Provider
The provider wraps resources and data sources which are typically used for interacting with cloud providers, SaaS providers, or other APIs.
In this example the provider wraps a resource and a data source which simply interact with Terraform state. Refer to the tutorial for an example of provider configuration that configures an API client.
New()
returns a function which returns a type that satisfies the provider.Provider
interface. The New()
function is called by the provider server to obtain the provider.
The exampleProvider
struct implements the provider.Provider
interface. This interface defines the following functions:
Schema
: This function returns a providerschema.Schema
struct that defines the provider schema. Schemas specify the constraints of Terraform configuration blocks. They define what fields a provider, resource, or data source configuration block has, and give Terraform metadata about those fields.Configure
: This function lets you configure provider-level data or clients. These configuration values may be from the practitioner Terraform configuration as defined by the schema, environment variables, or other means such as reading vendor-specific configuration files.Resources
: This function returns a slice of functions that return types that implement theresource.Resource
interface. Resources let Terraform manage infrastructure objects, such as a compute instance, an access policy, or disk.Data Sources
: This function returns a slice of functions that return types which implement thedatasource.DataSource
interface. Data sources let Terraform reference external data. For example a database instance.
The exampleProvider
struct also implements the provider.ProviderWithMetadata
interface which defines the Metadata
function. The Metadata
function returns metadata for the provider such as a TypeName
and Version
. The TypeName
is used as a prefix within a provider by for naming resources and data sources.
Refer to Providers for more details and configuration examples.
Resource
A resource is typically used to manage infrastructure objects such as virtual networks and compute instances.
In this example the resource simply interacts with Terraform state.
NewResource()
returns a function which returns a type that satisfies the resource.Resource
interface. The provider calls the NewResource()
function within provider.Resources
to obtain an instance of the resource.
The exampleResource
struct implements the resource.Resource
interface. This interface defines the following functions:
Metadata
: This function returns the full name (TypeName
) of the resource. The full name is used in Terraform configuration asresource <full name> <alias>
.Schema
: This function returns a resourceschema.Schema
struct that defines the resource schema. The schema specifies the constraints of the resource Terraform configuration block. It defines what fields a resource configuration block has, and gives Terraform metadata about those fields. For instance, defining whether a field is required.Create
: This function lets the provider create a new resource of this type.Read
: This function lets the provider read resource values in order to update state.Update
: This function lets the provider update the resource and state.Delete
: This function lets the provider delete the resource.
Refer to Resources for more details and configuration examples.
Data Source
A data source is typically used to provide a read-only view of infrastructure objects.
In this example the data source simply interacts with Terraform state.
NewDataSource()
returns a function which returns a type that satisfies the datasource.DataSource
interface. The NewDataSource()
function is used within the provider.DataSources
function to make the data source available to the provider.
The exampleDataSource
struct implements the datasource.DataSource
interface. This interface defines the following functions:
Metadata
: This function returns the full name (TypeName
) of the data source. The full name is used in Terraform configuration asdata <full name> <alias>
.Schema
: This function returns a data sourceschema.Schema
struct that defines the data source schema. The schema specifies the constraints of the data source Terraform configuration block. It defines what fields a data source configuration block has, and gives Terraform metadata about those fields. For instance, defining whether a field is optional.Read
: This function lets the provider read data source values in order to update state.
Refer to Data Sources for more details and configuration examples.
Terraform Configuration
Refer to terraform-provider-scaffolding-framework for details on how to wire together a provider server, provider, resource and data source.
Once wired together, run the provider by specifying configuration and executing terraform apply
.
Resource Configuration
The configurable_attribute
is defined within the schema as a string type attribute.
Examples of the various types of attributes and their representation within Terraform configuration and schema definitions are detailed in Terraform Concepts.
Data Source Configuration
The configurable_attribute
is defined within the schema as a string type attribute.
Examples of the various types of attributes and their representation within Terraform configuration and schema definitions are detailed in Terraform Concepts.