
Marking a Variable as Sensitive
Designating a variable as sensitive is straightforward. Simply include thesensitive = true attribute within its declaration. Consider the example below:
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: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:.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.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 sensitiveami value:
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.
