Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
π Terraform Environment Variables: Securely Passing Sensitive Data
When working with Terraform, we often deal with sensitive data like API keys, database passwords, or cloud provider credentials. Hardcoding these values in .tf
files is unsafe because:
- They may leak in Git repositories.
- They could be visible to team members unintentionally.
- They can expose security vulnerabilities.
π The solution? Terraform Environment Variables
Terraform allows us to inject values into variables through environment variables in a secure and flexible way.
This approach ensures that secrets never appear in the code itself but are instead supplied at runtime.
π What Are Terraform Environment Variables?
In Terraform, environment variables are OS-level variables that Terraform reads automatically. They can be used to:
- Pass sensitive data (like API keys, passwords).
- Set default values without modifying
.tf
files. - Configure providers securely.
Terraform recognizes variables with a specific prefix:
TF_VAR_name
β Maps directly to a Terraform variable.- Example:
TF_VAR_db_password=mysecretpassword
π₯οΈ 3 Example Programs for Terraform Environment Variables
β Example 1: Passing AWS Credentials Securely
Step 1: Define variables
variable "aws_access_key" { type = string sensitive = true}
variable "aws_secret_key" { type = string sensitive = true}
Step 2: Provider using variables
provider "aws" { region = "us-east-1" access_key = var.aws_access_key secret_key = var.aws_secret_key}
Step 3: Set environment variables
export TF_VAR_aws_access_key="AKIAXXXXXX"export TF_VAR_aws_secret_key="abcd1234secret"
π Now run terraform apply
without exposing keys in .tf
files.
β Example 2: Database Password with Environment Variables
Step 1: Define variable
variable "db_password" { type = string sensitive = true}
Step 2: Use in resource
resource "aws_db_instance" "mydb" { identifier = "mydb" engine = "mysql" username = "admin" password = var.db_password instance_class = "db.t3.micro" allocated_storage = 20}
Step 3: Export environment variable
export TF_VAR_db_password="StrongP@ssw0rd!"
π Database password never appears in code, only injected at runtime.
β Example 3: Using Environment Variables for Custom Tags
Step 1: Define variable
variable "environment" { type = string}
Step 2: Apply to resource
resource "aws_s3_bucket" "bucket" { bucket = "env-variable-example" tags = { Environment = var.environment }}
Step 3: Export variable
export TF_VAR_environment="production"
π The S3 bucket gets a tag Environment=production
without modifying .tf
files.
π§© Advantages of Terraform Environment Variables
- Security First β Secrets are not stored in
.tf
files or Git repos. - Flexibility β Easily switch between environments (dev, test, prod).
- Automation β Works seamlessly in CI/CD pipelines.
- Best Practice β Industry standard for secret management.
- Reduced Errors β Avoids accidental exposure of sensitive data.
π Real-World Use Cases
- Cloud Provider Authentication β AWS, Azure, GCP credentials.
- Database Configurations β Securely pass passwords.
- Application Secrets β API keys, tokens, OAuth credentials.
- Environment-Specific Configs β dev vs prod differences.
- CI/CD Pipelines β Store environment variables in GitHub Actions, Jenkins, GitLab.
π§ How to Remember Environment Variables (Exam & Interview Prep)
π Use this Mnemonic: βTF_VAR = Terraform Variableβ
- Prefix every variable with
TF_VAR_
. - Example:
TF_VAR_password=secret123
β maps tovariable "password" {}
.
Another trick:
- ENV = External, Not Visible β Think environment variables are external inputs, not visible in code.
π― Why Is It Important to Learn This Concept?
- For Security β Never expose secrets in
.tf
files. - For Exams β Terraform Associate Certification often asks how to handle sensitive data.
- For Teams β Developers can collaborate without sharing sensitive credentials.
- For Real Projects β Essential in pipelines, where environment variables are injected dynamically.
Without environment variables:
- Passwords may leak in Git.
- Terraform code becomes less reusable.
- Security audits fail.
With environment variables:
- Secrets stay safe.
- Code is reusable.
- Industry best practices are followed.
π₯οΈ More Examples
β Example 4: Multiple Variables with One Export
export TF_VAR_region="us-west-2"export TF_VAR_instance_type="t2.micro"export TF_VAR_owner="DevTeam"
π Terraform automatically maps these to variables.
β Example 5: Using Environment Variables in CI/CD
# GitHub Actions Exampleenv: TF_VAR_db_password: ${{ secrets.DB_PASSWORD }} TF_VAR_api_key: ${{ secrets.API_KEY }}
steps: - name: Terraform Apply run: terraform apply -auto-approve
π Environment variables come directly from GitHub Secrets.
β Example 6: Overriding Defaults with Env Vars
variable "region" { type = string default = "us-east-1"}
If you run:
export TF_VAR_region="eu-west-1"terraform apply
π The default (us-east-1
) is overridden with "eu-west-1"
.
π Comparison: Hardcoded Secrets vs Environment Variables
Feature | Hardcoded Secrets | Environment Variables |
---|---|---|
Security Risk | High (secrets in code) | Low (secrets external) |
Flexibility | Low | High |
Version Control Safety | Unsafe (committed) | Safe (not in Git) |
Best Practice | β | β |
CI/CD Usage | Difficult | Standard |
π§ Interview & Exam Preparation
Typical Questions:
-
Q: How can you securely pass a database password to Terraform? A: By exporting
TF_VAR_db_password="mypassword"
as an environment variable. -
Q: What prefix must Terraform environment variables have? A:
TF_VAR_
followed by the variable name. -
Q: Why are environment variables preferred over hardcoding values? A: They protect sensitive data and improve reusability.
π Conclusion
Terraform Environment Variables are a secure, flexible way to pass sensitive data into Terraform configurations.
- They keep secrets out of code.
- They work seamlessly in local setups and CI/CD pipelines.
- They ensure security best practices are followed.
π Key Takeaway:
Always use environment variables for secrets. Never hardcode them in .tf
files.