Terraform allows setting input variables using environment variables prefixed with TF_VAR_. What happens when you set TF_VAR_region=us-west-2 before running terraform apply?
Think about how environment variables can provide values to Terraform variables without editing files.
Terraform reads environment variables starting with TF_VAR_ and assigns their values to input variables matching the suffix. This allows dynamic configuration without changing code.
Given the Terraform variable declaration and environment variable below, what value will instance_type have during terraform apply?
variable "instance_type" {
type = string
default = "t2.micro"
}
Environment variable set before running Terraform:
export TF_VAR_instance_type="m5.large"
Environment variables override default values in Terraform variables.
The environment variable TF_VAR_instance_type overrides the default value of the variable instance_type. Terraform uses the environment variable value.
You want to pass a sensitive variable like a database password to Terraform without storing it in code or state files. Which approach using TF_VAR_ environment variables is best practice?
Think about avoiding storing secrets in code or version control.
Setting sensitive variables as environment variables locally and marking them sensitive in Terraform avoids storing secrets in code or state files. This is a secure practice.
Consider a Terraform variable declared as:
variable "region" {
type = string
}
No default is set. You do not set TF_VAR_region or provide the variable in any other way. What will happen when you run terraform apply?
Think about how Terraform handles required variables without defaults or environment variables.
Terraform requires values for variables without defaults. If no value is provided via environment variables or files, it prompts the user interactively.
In a CI/CD pipeline, you set sensitive secrets as TF_VAR_ environment variables to pass to Terraform. What is a key security risk of this approach?
Consider how environment variables might be exposed during pipeline execution.
Environment variables can be visible in logs, error messages, or process lists in CI/CD environments, which can expose secrets if not handled carefully.