Renew Ephemeral Resources
Renew 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. During Open
, ephemeral resources can opt to include a timestamp in the RenewAt
response field to indicate to Terraform when a provider must renew an ephemeral resource. If an ephemeral resource's data is still in-use and the RenewAt
timestamp has passed, Terraform calls the provider RenewEphemeralResource
RPC, in which the framework calls the ephemeral.EphemeralResourceWithRenew
interface Renew
method. The request contains any Private
data set in the latest Open
or Renew
call. The response contains Private
data and an optional RenewAt
field for further renew executions.
Note
Renew
cannot return new result data for the ephemeral resource instance, so this logic is only appropriate for remote objects like HashiCorp Vault leases, which can be renewed without changing their data.
Renew
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.
- Close a remote object when Terraform no longer needs the data.
Define Renew Method
The ephemeral.EphemeralResourceWithRenew
interface on the ephemeral.EphemeralResource
interface implementation will enable renew support for an ephemeral resource.
Implement the Renew
method by:
- Accessing private data from
ephemeral.RenewRequest.Private
field needed to renew the remote object. - Performing logic or external calls to renew the remote object.
- Determining if a remote object needs to be renewed again, setting the
ephemeral.RenewResponse.RenewAt
field to indicate to Terraform when to call the providerRenew
method. - Writing private data needed to
Renew
orClose
the ephemeral resource to theephemeral.RenewResponse.Private
field.
If the logic needs to return warning or error diagnostics, they can be added into the ephemeral.RenewResponse.Diagnostics
field.
In this example, an ephemeral resource named examplecloud_thing
with hardcoded behavior is defined. It indicates a renewal should occur 5 minutes from when either the Open
or Renew
method is executed:
Recommendations
- When setting the
RenewAt
response field, add extra time (usually no more than a few minutes) before an ephemeral resource expires to account for latency.