Close Ephemeral Resources
Close is an optional part of the Terraform lifecycle for an ephemeral resource, which is different from the managed resource lifecycle. During any Terraform operation (like terraform plan
or terraform apply
), when an ephemeral resource's data is needed, Terraform initially retrieves that data with the Open
lifecycle handler. Once the ephemeral resource data is no longer needed, Terraform calls the provider CloseEphemeralResource
RPC, in which the framework calls the ephemeral.EphemeralResourceWithClose
interface Close
method. The request contains any Private
data set in the latest Open
or Renew
call.
Close
is an optional lifecycle implementation for an ephemeral resource, other lifecycle implementations include:
- Open an ephemeral resource by receiving Terraform configuration, retrieving a remote object, and returning ephemeral result data to Terraform.
- Renew an expired remote object at a specified time.
Define Close Method
The ephemeral.EphemeralResourceWithClose
interface on the ephemeral.EphemeralResource
interface implementation will enable close support for an ephemeral resource.
Implement the Close
method by:
- Accessing private data from
ephemeral.CloseRequest.Private
field needed to close the remote object. - Performing logic or external calls to close the remote object.
If the logic needs to return warning or error diagnostics, they can be added into the ephemeral.CloseResponse.Diagnostics
field.
In this example, an ephemeral resource named examplecloud_thing
with hardcoded behavior is defined. Private
data needed to execute Close
is passed from the Open
response: