OpsCanary
Back to daily brief
terraformstatePractitioner

Mastering Sensitive Data Management in Terraform State

5 min read HashiCorp DocsApr 22, 2026
PractitionerHands-on experience recommended

In today's cloud-native world, managing sensitive data in your infrastructure as code is not just a best practice—it's a necessity. Terraform state and plan files can inadvertently expose sensitive information like passwords and API tokens, leading to potential security breaches. To mitigate this risk, Terraform provides mechanisms to redact sensitive values from CLI outputs and state files, ensuring that your infrastructure remains secure.

The 'sensitive' argument can be added to variable and output blocks. This redacts values from CLI output and the HCP Terraform UI, making it harder for unauthorized users to access sensitive information. For example, you can define a variable for your database password as follows:

HCL
variable "database_password" {
  description = "Password for the database instance"
  type        = string
  sensitive   = true
}

Additionally, the 'ephemeral' argument allows you to omit values from state and plan files entirely. This is particularly useful for short-lived credentials or tokens that don't need to persist between runs. For instance, you might define an API token like this:

HCL
1variable "api_token" {
2  description = "Short-lived API token for provider authentication"
3  type        = string
4  sensitive   = true
5  ephemeral   = true
6}

In production, using these features effectively can save you from exposing sensitive data. However, you need to be aware of version requirements: use Terraform 0.15 or later for the 'sensitive' argument and Terraform 1.10 or later for 'ephemeral'. Always ensure you are using the correct versions to leverage these capabilities fully.

Key takeaways

  • Use the 'sensitive' argument to redact sensitive values from CLI output and the HCP Terraform UI.
  • Implement the 'ephemeral' argument to omit sensitive values from state and plan files entirely.
  • Define sensitive variables clearly to prevent accidental exposure in logs and outputs.
  • Ensure you are using Terraform 0.15 or later for 'sensitive' and 1.10 or later for 'ephemeral' features.

Why it matters

In production environments, exposing sensitive data can lead to severe security incidents. Properly managing sensitive information in Terraform helps maintain compliance and protects your infrastructure from unauthorized access.

Code examples

HCL
variable "database_password" { description = "Password for the database instance" type = string, sensitive = true }
HCL
variable "api_token" { description = "Short-lived API token for provider authentication" type = string, sensitive = true, ephemeral = true }
HCL
output "connection_string" { description = "Database connection string" value = "postgresql://${var.db_username}:${var.database_password}@${aws_db_instance.main.endpoint}/mydb", sensitive = true }

When NOT to use this

The official docs don't call out specific anti-patterns here. Use your judgment based on your scale and requirements.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.