Terraform
Ephemerality in resources
Managing infrastructure often requires creating and handling sensitive values that you may not want Terraform to persist outside of the current operation. Terraform provides two tools for resources to manage data you do not want to store in state or plan files: the ephemeral
resource block and ephemeral write-only arguments on specific resources.
Ephemeral resources
Ephemeral resources are Terraform resources that are essentially temporary. Ephemeral resources have a unique lifecycle, and Terraform does not store information about ephemeral resources in state or plan files. Each ephemeral
block describes one or more ephemeral resources, such as a temporary password or connection to another system.
In your configuration, you can only reference an ephemeral
block in other ephemeral contexts.
Lifecycle
The lifecycle of an ephemeral
resource is different from other resources and data sources. When Terraform provisions ephemeral resources, it performs the following steps:
If Terraform needs to access the result of an ephemeral resource, it opens that ephemeral resource. For example, if Terraform opens an ephemeral resource for a Vault secret, the Vault provider obtains a lease and returns a secret.
If Terraform needs access to the ephemeral resource for longer than the remote system's enforced expiration time, Terraform asks the provider to periodically renew it. For example, if Terraform renews a Vault secret
ephemeral
resource, the Vault provider calls Vault's lease renewal API endpoint to extend the expiration time.Once Terraform no longer needs an ephemeral resource, Terraform closes it. This happens after the providers that depend on an ephemeral resource complete all of their work for the current Terraform run phase. For example, closing a Vault secret ephemeral resource means the Vault provider explicitly ends the lease, allowing Vault to immediately revoke the associated credentials.
Terraform follows these lifecycle steps for each instance of an ephemeral resource in a given configuration.
Configuration model
To learn more about the ephemeral
resource block, refer to the Ephemeral resource reference.
Write-only arguments
Terraform's managed resources, defined by resource
blocks, can include ephemeral arguments, called write-only arguments. Write-only arguments are only available during the current Terraform operation, and Terraform does not store them in state or plan files.
Use write-only arguments to securely pass temporary values to resources during a Terraform operation without worrying about Terraform persisting those values. For example, the aws_db_instance
resource has a write-only password_wo
argument that accepts a database password:
ephemeral "random" "password" {
length = 16
}
resource "aws_db_instance" "test" {
instance_class = "db.t5.micro"
allocated_storage = "5"
engine = "postgres"
username = "admin"
skip_final_snapshot = true
password_wo = ephemeral.random.password.value
password_wo_version = 1
}
When Terraform creates the aws_db_instance
resource, Terraform sends the password_wo
argument to the aws
provider. The aws
provider then uses the password_wo
value to configure the database instance, and then Terraform discards the password value without ever storing it.
To learn more about using write-only arguments, refer to the Use write-only arguments.