Configure Resources
Resources may require provider-level data or remote system clients to operate correctly. The framework supports the ability to configure this data and/or clients once within the provider, then pass that information to resources by adding the Configure
method.
Prepare Provider Configure Method
Implement the provider.ConfigureResponse.ResourceData
field in the Provider
interface Configure
method. This value can be set to any type, whether an existing client or vendor SDK type, a provider-defined custom type, or the provider implementation itself. It is recommended to use pointer types so that resources can determine if this value was configured before attempting to use it.
During execution of the terraform plan
and terraform apply
commands, Terraform calls the provider ConfigureProvider
RPC, in which the framework calls the provider.Provider
interface Configure
method.
In this example, the Go standard library net/http.Client
is configured in the provider, and made available for resources:
In this example, the code defines an ExampleClient
type that is made available for resources:
In this example, the ExampleCloudProvider
type itself is made available for resources:
Define Resource Configure Method
Implement the resource.ResourceWithConfigure
interface which receives the provider configured data from the Provider
interface Configure
method and saves it into the resource.Resource
interface implementation.
The resource.ResourceWithConfigure
interface Configure
method is called during execution of the terraform validate
, terraform plan
and terraform apply
commands when the ValidateResourceConfig
RPC is sent. Additionally, the resource.ResourceWithConfigure
interface Configure
method is called during execution of the terraform plan
and terraform apply
commands when the ReadResource
RPC is sent.
Note that Terraform calling the ValidateResourceConfig
RPC would not call the ConfigureProvider
RPC first, so implementations need to account for that situation. Configuration validation in Terraform occurs without provider configuration ("offline").
In this example, the provider configured the Go standard library net/http.Client
which the resource uses during Read
: