Terraform ephemeral secrets,
why is it a big thing?
Author

Kevin Duterne
CTO
This post will explore the storage of secret values, in plaintext, within state files, in Terraform.
Terraform is an Infrastructure-as-Code (IaC) software tool created by HashiCorp. IaC is the process of managing and provisioning cloud providers or data center resources through machine-readable definition files, which enables a more reproducible configuration in cloud environments than using the UI to manage and provision infrastructure.
Terraform enables users to define and provide cloud infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL)
The benefits of Terraform and Infrastructure as Code (IaC) are undeniable. However, there has been a longstanding concern about the storage of secret values, in plaintext, within state files. This concern is pertinent because Terraform’s management of infrastructure involves handling secrets, such as private keys, certifications, API tokens, etc. For example, a data source may fetch a secret and write it to a managed resource’s attribute. Alternatively, a secret can be generated by a resource type. The secret could take the form of a random password. This is written to another resource type – such as a dedicated secrets manager.
Currently, these secrets persist in the plan or state file. These secrets are stored in plaintext within these artifacts. Thus, any inappropriate access to or manipulation of these files would compromise the integrity of these secrets. This concern has been remedied with the release of Terraform 1.10 with the introduction of Ephemeral Values.
Ephemeral Values
Ephemeral Values are not stored in any artifact such as the plan file or the state file. They do not remain consistent from plan to apply, or from one plan/apply round to the next. Ephemeral values are characterised by the following syntax variables:
This is a leap forward in addressing security concerns. The availability of Ephemeral Values means that when you generate credentials and fetch a token, the related values are not persisted in Terraform artifacts.1
Key Security Improvements
Regarding concerns over the storage of secret values in Terraform, the introduction of Ephemeral Values brings the following improvements:
Implementation and Best Practices
Declaring Ephemeral Resources
Ephemeral resources are defined by using a new `ephemeral` block in HCL. This is similar to regular resource blocks [1]. The ephemeral block enables easy integration into existing Terraform configurations.
Security Implications and Recommendations
Examples of ephemeral resources available in different cloud providers:
These ephemeral resources allow developers to securely handle sensitive data like credentials and secrets without persisting them in Terraform state files, significantly enhancing security in infrastructure-as-code practices [1][4][5].
In conclusion, the introduction of ephemeral resources in Terraform 1.10 marks a significant leap forward in securing infrastructure-as-code practices. By implementing this feature in Terraform stacks, that handle sensitive data, organizations can substantially reduce the risk of secret exposure and enhance their overall security posture in cloud and infrastructure management.
Questions?
Sources:
[1] Ephemeral Environments in Cloud Infrastructure: Use Cases and … https://www.withcoherence.com/articles/ephemeral-environments-in-cloud-infrastructure-use-cases-and-benefits
[2] Exploring Ephemeral Resources | Terraform Tuesdays – YouTube https://www.youtube.com/watch?v=JnXEiWA1TE0
[3] Kubernetes Resource Management: A Practical Guide | Spot.io https://spot.io/resources/kubernetes-architecture/kubernetes-resource-management-a-practical-guide/
[4] Terraform Ephemeral Resources – mattias.engineer https://mattias.engineer/blog/2024/terraform-ephemeral-resources/
[5] Terraform 1.10 improves handling secrets in state with ephemeral … https://www.hashicorp.com/blog/terraform-1-10-improves-handling-secrets-in-state-with-ephemeral-values
[6] Ephemeral resource configuration reference | Terraform https://developer.hashicorp.com/terraform/language/resources/ephemeral
[7] Automating Ephemeral Environments with Kubernetes: A Quick Guide https://www.qovery.com/blog/automating-ephemeral-environments-with-kubernetes-a-quick-guide/
[8] Plugin Development – Framework: Ephemeral Resources | Terraform https://developer.hashicorp.com/terraform/plugin/framework/ephemeral-resources
[9] Acceptance Testing: Ephemeral Resources | Terraform https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests/ephemeral-resources
[10] Use ephemeral resources in the Google Cloud provider | Guides https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/using_ephemeral_resources