Skip to main content
In this lesson, you’ll learn how to mark variables and outputs as sensitive in Terraform, ensuring secure handling of critical information such as passwords, API keys, and other secrets. Terraform provides built-in mechanisms to safeguard sensitive data, preventing accidental exposure in logs or terminal outputs.
The image shows a HashiCorp Terraform interface for defining sensitive information, with options for passwords, API keys, and other data.

Marking a Variable as Sensitive

Designating a variable as sensitive is straightforward. Simply include the sensitive = true attribute within its declaration. Consider the example below:
With this configuration, Terraform treats the ami variable as sensitive. This causes Terraform to mask the actual value during both planning and apply phases, which prevents sensitive details from being displayed in logs or terminal outputs.

Demonstrating Sensitive Handling in Terraform Plan

When you run a plan, Terraform automatically hides the sensitive value for the AMI. Here’s an example of what you might see:
This output confirms that the ami value is redacted, maintaining confidentiality by preventing accidental data leaks.

Receiving Sensitive Inputs

If you leave a sensitive variable without a default value, Terraform prompts for the input during the plan or apply process. The input remains hidden as you type:
To streamline processes and avoid manual input each time, store the secret values in a separate .tfvars file and provide them via the -var-file parameter:
Storing sensitive values in a dedicated .tfvars file and using the -var-file option significantly reduces the risk of accidentally exposing secret information.
Alternatively, you can export sensitive values as environment variables. This approach is especially useful in CI/CD pipelines, where Terraform can securely access sensitive data without manual input.

Handling Errors When Exposing Sensitive Outputs

Terraform prevents sensitive information from being exposed in outputs. If you try to output sensitive details without explicitly marking them as such, Terraform will throw an error. For instance, the following output configuration attempts to expose the sensitive ami value:
When you run the apply command, Terraform redacts the sensitive output:
To view the actual value of a sensitive output variable, use the terraform output command followed by the variable name:
Remember that even if sensitive attributes are masked in terminal outputs, they are stored as plain text in the Terraform state file. Ensure that you manage access to your state file securely and consider using encryption to protect it.
That’s it for this lesson on marking variables as sensitive in Terraform. Continue exploring Terraform best practices to further enhance your infrastructure security and efficiency.
The image shows a selection interface for securing a state file, with options: "Sensitive attributes hidden," "Plain text in state file" (highlighted), and "Secure state file."

Watch Video