Protecting Sensitive Values and Secrets in Terraform

Terraform secrets flow through redacted outputs into secured encrypted state storage

What You’ll Learn

In this lesson, you will learn how to protect database passwords, API keys, and similar values from appearing in normal Terraform CLI output.

  • Mark input variables and outputs as sensitive.
  • Understand how sensitivity propagates through locals and objects.
  • Distinguish CLI redaction from protecting secrets in Terraform state.
  • Use practical patterns for passing secrets through environment variables.
Ad

The Concept

Terraform can label a value as sensitive. When a sensitive value is displayed in a plan, apply result, or output command, Terraform redacts it instead of printing the actual contents.

Mark an input variable as sensitive when it receives a secret:

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

Mark an output as sensitive when it returns a secret or contains a secret. Terraform requires this because an output is an intentional request to display a value. Without the declaration, Terraform reports an error rather than accidentally exposing the secret.

Sensitivity is metadata, not encryption. Terraform still usually stores the value in its state because it may need the value to compare configuration with the real infrastructure. Protecting the state file and its backend is therefore a separate security responsibility. For background on how plans, applies, and state work together, see the Terraform plan, apply, and state workflow.

Basic Example

This configuration represents connection details for an application database. The password enters Terraform through a sensitive variable, and the complete connection object is exposed only through a sensitive output.

variable "db_password" {
  description = "Password for the application database"
  type        = string
  sensitive   = true
}

locals {
  database_connection = {
    host     = "db.internal.example"
    port     = 5432
    username = "app_user"
    password = var.db_password
  }
}

output "database_connection" {
  description = "Connection details used by the application"
  value       = local.database_connection
  sensitive   = true
}

output "database_host" {
  description = "Non-secret database hostname"
  value       = local.database_connection.host
}

For local testing, provide the password through the TF_VAR_db_password environment variable rather than placing it in the configuration file:

export TF_VAR_db_password='replace-with-a-real-password'
terraform apply -auto-approve

Expected Output

The exact formatting can vary by Terraform version, but Terraform redacts the connection object while displaying the non-secret hostname:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

database_connection = <sensitive>
database_host = "db.internal.example"

How the Code Works

A sensitive password or API key enters Terraform through a protected environment variable and sensitive input variable, flows through locals into a secret-containing object, and branches into a redacted CLI output and a separately protected Terraform state path. A non-secret field such as a host or endpoint can be exposed normally, while state still requires encryption and access controls.
Terraform sensitivity propagates through secret-containing values and redacts CLI output, but protecting the Terraform state backend remains a separate responsibility.

The db_password variable has sensitive = true. Terraform will avoid displaying its value in normal plan and apply output. The environment variable supplies the value at runtime without committing it to the .tf file.

The local value contains the password along with ordinary connection information. Because one attribute is sensitive, the entire object is treated as sensitive when it is returned through database_connection. The output repeats sensitive = true to explicitly permit that secret-containing value to be exposed as an output.

database_host selects only the host attribute. It does not contain the password, so it can be a regular output. Keeping non-secret outputs separate makes logs and troubleshooting more useful without revealing credentials.

Do not interpret the redaction as removal from state. If this configuration were connected to a database resource or another provider, the password could still be recorded in the state snapshot. Use a remote backend with encryption at rest, restrict access to the state, secure state backups, and avoid committing local state files to version control.

Another Example

Secrets are not limited to database passwords. This example accepts an API token and a client secret together as one sensitive object. Terraform can use the values to construct an authorization header, while exposing only the public service endpoint through a normal output.

variable "api_credentials" {
  description = "Credentials for the billing API"
  type = object({
    token         = string
    client_secret = string
  })
  sensitive = true
}

locals {
  billing_api_request = {
    endpoint       = "https://billing.example.com/v1"
    authorization  = "Bearer ${var.api_credentials.token}"
    client_secret  = var.api_credentials.client_secret
  }
}

output "billing_api_request" {
  description = "Authentication data for the billing service"
  value       = local.billing_api_request
  sensitive   = true
}

output "billing_api_endpoint" {
  description = "Public billing API endpoint"
  value       = local.billing_api_request.endpoint
}

Terraform treats the input object as sensitive, and the local object remains sensitive because it includes both credentials. The endpoint is safe to expose separately, but the complete request object must remain redacted.

Common Mistakes

  • Marking only the input variable: A sensitive input protects normal displays of the variable, but an output that contains it must also be declared sensitive.
  • Assuming redaction encrypts state: Sensitive output displays hide values from routine CLI output. They do not remove values from state or automatically encrypt a backend.
  • Putting secrets in -var arguments: A command such as terraform apply -var="db_password=..." can expose the secret through shell history or process inspection. Prefer an environment variable, a protected variable file, or a secrets-management integration appropriate for your workflow.
  • Using a non-sensitive derived value: A value can still reveal a secret indirectly. For example, an output containing a full connection string should be sensitive even if it was assembled in a local value.
  • Checking only Terraform output: Review state access, backend permissions, CI logs, plan files, backups, and variable files. The entire delivery pipeline must protect the secret.

Try It Yourself

Create a configuration that accepts a sensitive database password and produces two outputs:

  • A sensitive connection description containing the database host, username, and password.
  • A normal output containing only the database port.

Set the password with TF_VAR_db_password, run terraform apply, and confirm that the password is replaced with <sensitive> in the connection output.

Challenge

Build a Terraform configuration for a reporting service with these requirements:

  • Accept reporting_api_key as a sensitive string variable.
  • Accept reporting_region as a normal string variable.
  • Create a local object containing the API endpoint, region, and API key.
  • Expose the complete object through a sensitive output named reporting_service_config.
  • Expose only the endpoint through a normal output named reporting_endpoint.

Do not put the API key directly in the Terraform configuration. Test it by setting TF_VAR_reporting_api_key in your shell.

Solution

variable "reporting_api_key" {
  description = "API key for the reporting service"
  type        = string
  sensitive   = true
}

variable "reporting_region" {
  description = "Region used by the reporting service"
  type        = string
}

locals {
  reporting_service_config = {
    endpoint = "https://reports.example.com/v2"
    region   = var.reporting_region
    api_key  = var.reporting_api_key
  }
}

output "reporting_service_config" {
  description = "Configuration for connecting to the reporting service"
  value       = local.reporting_service_config
  sensitive   = true
}

output "reporting_endpoint" {
  description = "Non-secret reporting service endpoint"
  value       = local.reporting_service_config.endpoint
}

The API key is marked sensitive at the variable boundary, and the complete local object is marked sensitive when returned as an output. The endpoint is selected independently, so Terraform can display it without exposing the key. The state backend still needs appropriate encryption and access controls because the key may remain in state.

Key Takeaways

  • Use sensitive = true on variables that receive passwords, tokens, and API keys.
  • Mark outputs sensitive when their values contain or could reveal secrets.
  • Sensitivity redacts CLI output; it does not encrypt or remove values from Terraform state.
  • Prefer protected environment variables or an appropriate secrets-management workflow over command-line secrets.
  • Secure the Terraform backend, state files, plan files, logs, and backups as part of the same secret-protection strategy.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad