|
| 1 | +--- |
| 2 | +page_title: 'Plugin Development - Framework: Ephemeral Resources' |
| 3 | +description: >- |
| 4 | + How to build ephemeral resources in the provider development framework. Ephemeral |
| 5 | + resources allow Terraform to reference external data, while guaranteeing that this |
| 6 | + data will not be persisted in plan or state. |
| 7 | +--- |
| 8 | + |
| 9 | +# Ephemeral Resources |
| 10 | + |
| 11 | +<Highlight> |
| 12 | + |
| 13 | +Ephemeral resource support is in technical preview and offered without compatibility promises until Terraform 1.10 is generally available. |
| 14 | + |
| 15 | +</Highlight> |
| 16 | + |
| 17 | +[Ephemeral resources](/terraform/language/resources/ephemeral) are an abstraction that allows Terraform to reference external data. Unlike [data sources](/terraform/language/data-sources), Terraform guarantees that ephemeral resource data will not be persisted in plan or state artifacts. The data produced by an ephemeral resource can only be referenced in [specific ephemeral contexts](/terraform/language/resources/ephemeral#referencing-ephemeral-resources) or Terraform will throw an error. |
| 18 | + |
| 19 | +This page describes the basic implementation details required for supporting an ephemeral resource within the provider. Ephemeral resources, as a part of their lifecycle, must implement: |
| 20 | + |
| 21 | +- [Open](/terraform/plugin/framework/ephemeral-resources/open) an ephemeral resource by receiving Terraform configuration, retrieving a remote object, and returning ephemeral result data to Terraform. |
| 22 | + |
| 23 | +Further documentation is available for deeper ephemeral resource concepts: |
| 24 | + |
| 25 | +- [Configure](/terraform/plugin/framework/ephemeral-resources/configure) an ephemeral resource with provider-level data types or clients. |
| 26 | +- [Validate](/terraform/plugin/framework/ephemeral-resources/validate-configuration) practitioner configuration against acceptable values. |
| 27 | +- [Renew](/terraform/plugin/framework/ephemeral-resources/renew) an expired remote object at a specified time. |
| 28 | +- [Close](/terraform/plugin/framework/ephemeral-resources/close) a remote object when Terraform no longer needs the data. |
| 29 | + |
| 30 | +## Define Ephemeral Resource Type |
| 31 | + |
| 32 | +Implement the [`ephemeral.EphemeralResource` interface](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/ephemeral#EphemeralResource). Ensure the [Add Ephemeral Resource To Provider](#add-ephemeral-resource-to-provider) documentation is followed so the ephemeral resource becomes part of the provider implementation, and therefore available to practitioners. |
| 33 | + |
| 34 | +### Metadata Method |
| 35 | + |
| 36 | +The [`ephemeral.EphemeralResource` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/ephemeral#EphemeralResource.Metadata) defines the ephemeral resource name as it would appear in Terraform configurations. This name should include the provider type prefix, an underscore, then the ephemeral resource specific name. For example, a provider named `examplecloud` and an ephemeral resource that reads "thing" ephemeral data would be named `examplecloud_thing`. |
| 37 | + |
| 38 | +In this example, the ephemeral resource name in an `examplecloud` provider that reads "thing" ephemeral resource data is hardcoded to `examplecloud_thing`: |
| 39 | + |
| 40 | +```go |
| 41 | +// With the ephemeral.EphemeralResource implementation |
| 42 | +func (r *ThingEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 43 | + resp.TypeName = "examplecloud_thing" |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +To simplify ephemeral resource implementations, the [`provider.MetadataResponse.TypeName` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#MetadataResponse.TypeName) from the [`provider.Provider` interface `Metadata` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#Provider.Metadata) can set the provider name so it is available in the [`ephemeral.MetadataRequest.ProviderTypeName` field](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/ephemeral#MetadataRequest.ProviderTypeName). |
| 48 | + |
| 49 | +In this example, the provider defines the `examplecloud` name for itself, and the ephemeral resource is named `examplecloud_thing`: |
| 50 | + |
| 51 | +```go |
| 52 | +// With the provider.Provider implementation |
| 53 | +func (p *ExampleCloudProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) { |
| 54 | + resp.TypeName = "examplecloud" |
| 55 | +} |
| 56 | + |
| 57 | +// With the ephemeral.EphemeralResource implementation |
| 58 | +func (d *ThingEphemeralResource) Metadata(ctx context.Context, req ephemeral.MetadataRequest, resp *ephemeral.MetadataResponse) { |
| 59 | + resp.TypeName = req.ProviderTypeName + "_thing" |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### Schema Method |
| 64 | + |
| 65 | +The [`ephemeral.EphemeralResource` interface `Schema` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/ephemeral#EphemeralResource.Schema) defines a [schema](/terraform/plugin/framework/handling-data/schemas) describing what data is available in the ephemeral resource's configuration and result data. |
| 66 | + |
| 67 | +## Add Ephemeral Resource to Provider |
| 68 | + |
| 69 | +Ephemeral resources become available to practitioners when they are included in the [provider](/terraform/plugin/framework/providers) implementation via the optional [`provider.ProviderWithEphemeralResources` interface `EphemeralResources` method](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/provider#ProviderWithEphemeralResources.EphemeralResource). |
| 70 | + |
| 71 | +In this example, the `ThingEphemeralResource` type, which implements the `ephemeral.EphemeralResource` interface, is added to the provider implementation: |
| 72 | + |
| 73 | +```go |
| 74 | +var _ provider.ProviderWithEphemeralResources = (*ExampleCloudProvider)(nil) |
| 75 | + |
| 76 | +func (p *ExampleCloudProvider) EphemeralResources(_ context.Context) []func() ephemeral.EphemeralResource { |
| 77 | + return []func() ephemeral.EphemeralResource{ |
| 78 | + func() ephemeral.EphemeralResource { |
| 79 | + return &ThingResource{}, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +To simplify provider implementations, a named function can be created with the ephemeral resource implementation. |
| 86 | + |
| 87 | +In this example, the `ThingEphemeralResource` code includes an additional `NewThingEphemeralResource` function, which simplifies the provider implementation: |
| 88 | + |
| 89 | +```go |
| 90 | +// With the provider.ProviderWithEphemeralResources implementation |
| 91 | +func (p *ExampleCloudProvider) EphemeralResources(_ context.Context) []func() ephemeral.EphemeralResource { |
| 92 | + return []func() ephemeral.EphemeralResource{ |
| 93 | + NewThingEphemeralResource, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// With the ephemeral.EphemeralResource implementation |
| 98 | +func NewThingEphemeralResource() ephemeral.EphemeralResource { |
| 99 | + return &ThingEphemeralResource{} |
| 100 | +} |
| 101 | +``` |
0 commit comments