🎓 A1Learner
🔍
0 of 50 lessons completed

Secrets and Sensitive Values

A secret is a value that must never appear on screen or in a shared file. Terraform hides sensitive values, but hiding is not the same as protecting.

Mark a value sensitive

The sensitive flag tells Terraform to hide the value. Use it on every secret variable.

variable "api_token" {
  type      = string
  sensitive = true
}

Plan hides the value

Plan shows the marker instead of the real token. The real value never reaches the screen.

terraform plan -var="api_token=secret-token-123"

Ephemeral values and write-only arguments

An ephemeral value lives only during the run. A write-only argument takes the value without storing it in state.

ephemeral "random_password" "db" {
  length = 16
}

# Write-only argument (Terraform 1.11+):
# resource "aws_db_instance" "app" {
#   password_wo = ephemeral.random_password.db.result
# }

Example

# main.tf
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.0"
    }
  }
}

variable "api_token" {
  type      = string
  sensitive = true
}

resource "local_file" "token" {
  filename = "${path.module}/token.txt"
  content  = var.api_token
}

output "token" {
  value     = var.api_token
  sensitive = true
}

# terraform init
# terraform plan -var="api_token=secret-token-123"
# terraform apply -auto-approve -var="api_token=secret-token-123"
# terraform destroy -auto-approve -var="api_token=secret-token-123"

Key points

  • A password or token is a sensitive value.
  • sensitive = true hides the value in plan and output.
  • Hiding is not encrypting: the value still sits in state.
  • State is a secret, so it needs a safe backend and tight access.
  • Ephemeral values, from Terraform 1.10, never reach state at all.
  • Write-only arguments, from Terraform 1.11, accept a value without storing it.

The token is marked sensitive, so plan and output hide it. The file still holds the real value, which is why secrets belong in a vault or an environment variable, never in the config.

Note

Never put a real secret in a config file or in version control.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What happens to a value marked sensitive = true?