Terraform
- Plugin Framework
- v1.16.x (latest)
- No versions of this document exist before v1.15.x. Click below to redirect to the version homepage.
- v1.15.x
- v1.14.x
- v1.13.x
- v1.12.x
- v1.11.x
- v1.10.x
- v1.9.x
- v1.8.x
- v1.7.x
- v1.6.x
- v1.5.x
- v1.4.x
- v1.3.x
- v1.2.x
- v1.1.x
- v1.0.x
- v0.16.x
- v0.15.x
- v0.14.x
- v0.13.x
- v0.12.x
- v0.11.x
- v0.10.x
- v0.9.x
- v0.8.x
- v0.7.x
Configure Resources
Note: The Plugin Framework is in beta.
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.
In this example, the Go standard library net/http.Client is configured in the provider, and made available for resources:
// With the provider.Provider implementation
func (p *ExampleCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
  resp.ResourceData = &http.Client{/* ... */}
}
In this example, the code defines an ExampleClient type that is made available for resources:
type ExampleClient struct {
  /* ... */
}
// With the provider.Provider implementation
func (p *ExampleCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
  resp.ResourceData = &ExampleClient{/* ... */}
}
In this example, the ExampleCloudProvider type itself is made available for resources:
// With the provider.Provider implementation
type ExampleCloudProvider struct {
  /* ... */
}
func (p *ExampleCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
  resp.ResourceData = p
}
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.
In this example, the provider configured the Go standard library net/http.Client which the resource uses during Read:
// With the resource.Resource implementation
type ThingResource struct {
  client *http.Client
}
func (r *ThingResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
  // Prevent panic if the provider has not been configured.
  if req.ProviderData == nil {
    return
  }
  client, ok := req.ProviderData.(*http.Client)
  if !ok {
    resp.Diagnostics.AddError(
      "Unexpected Resource Configure Type",
      fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
    )
    return
  }
  r.client = client
}
func (r *ThingResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
  // Prevent panic if the provider has not been configured.
  if r.client == nil {
    resp.Diagnostics.AddError(
      "Unconfigured HTTP Client",
      "Expected configured HTTP client. Please report this issue to the provider developers.",
    )
    return
  }
  httpResp, err := r.client.Get("https://example.com")
  /* ... */
}