terraform certificate #6

Source: Dev.to | Dec 18, 2025 (UTC)

Terraform Multiple Provider Configuration (Alias)

1. Problem Statement (Why we need this)

By default:

  • Terraform uses one provider
  • All resources are created in one region

❌ Default behavior

provider "aws" { region = "ap-southeast-1" }

Enter fullscreen mode

Exit fullscreen mode

➡️ ALL resources go to us-east-2

Real-world requirement

In production, this happens often:

Resource

Region

Security Group

Tokio (to-west-1)

EC2 Instance

USA (us-east-1)

But Terraform does not allow multiple providers of same type unless we use alias.

2. What is Provider Alias?

Alias allows you to:

3. Rule You Must Remember

❌ This is NOT allowed

provider "aws" { region = "ap-south-1" } provider "aws" { region = "us-east-1" }

Enter fullscreen mode

Exit fullscreen mode

Terraform error:

Duplicate provider configuration

Enter fullscreen mode

Exit fullscreen mode

4. Correct Way: Using Alias

Step 1: Define Multiple Providers with Alias

provider "aws" { region = "ap-southeast-1" # default provider } provider "aws" { alias = "Tokio" region = "to-west-1" } provider "aws" { alias = "usa" region = "us-east-1" }

Enter fullscreen mode

Exit fullscreen mode

Meaning

Provider Reference

Region

aws (default)

Singapore

aws.tokio

Tokio

aws.usa

USA

5. Important Rule (Very Common Exam Question)

If you do NOT specify provider inside a resource, Terraform uses default provider

6. Simplest Practical Example (Security Groups)

Goal

Resource

Region

prod_firewall

USA

staging_firewall

Tokio

Full Working Code

provider "aws" { region = "ap-southeast-1" } provider "aws" { alias = "mumbai" region = "ap-south-1" } provider "aws" { alias = "usa" region = "us-east-1" } resource "aws_security_group" "prod_firewall" { provider = aws.usa name = "prod_firewall" description = "Production firewall" } resource "aws_security_group" "staging_firewall" { provider = aws.mumbai name = "staging_firewall" description = "Staging firewall" }

Enter fullscreen mode

Exit fullscreen mode

7. Key Line to Understand (Most Important)

provider = aws.usa

Enter fullscreen mode

Exit fullscreen mode

Meaning:

➡️ Create this resource using AWS provider configured for us-east-2

8. Execution Commands

terraform init terraform validate terraform apply -auto-approve

Enter fullscreen mode

Exit fullscreen mode

9. Verification in AWS Console

Region

Expected Resource

us-east-1

prod_firewall

to -west-1

staging_firewall

10. Common Mistakes (Interview + Exams)

❌ Same resource name

resource "aws_security_group" "sg" {} resource "aws_security_group" "sg" {}

Enter fullscreen mode

Exit fullscreen mode

Terraform error:

Duplicate resource configuration

Enter fullscreen mode

Exit fullscreen mode

✔️ Solution: unique local names

❌ Alias defined but not used

provider "aws" { alias = "mumbai" }

Enter fullscreen mode

Exit fullscreen mode

But resource has no provider = aws.mumbai

➡️ Resource still goes to default region

11. When Alias is Used in Real Production

12. Exam-Ready Summary (Memorize)

provider = aws.<alias_name>

Enter fullscreen mode

Exit fullscreen mode

Terraform Sensitive Parameters (Security Topic)

1. Why This Topic Is Important

By default, Terraform prints values in plain text during:

This can leak secrets such as:

👉 In production, this is unacceptable.

2. Problem Demonstration (Default Behavior)

Example: Local File with Password

resource "local_file" "password_file" { filename = "password.txt" content = "supersecretpassw0rd" }

Enter fullscreen mode

Exit fullscreen mode

Terraform Plan Output (❌ Bad)

content = "supersecretpassw0rd"

Enter fullscreen mode

Exit fullscreen mode

✔ Password is visible
❌ Security risk

3. Common Mistake (Using Variable Only)

Many beginners think variables hide secrets.

variable "password" { default = "supersecretpassw0rd" } resource "local_file" "password_file" { filename = "password.txt" content = var.password }

Enter fullscreen mode

Exit fullscreen mode

Result (❌ Still visible)

content = "supersecretpassw0rd"

Enter fullscreen mode

Exit fullscreen mode

👉 Terraform evaluates variables before showing output.

4. Correct Solution: sensitive = true

Step 1: Mark Variable as Sensitive

variable "password" { default = "supersecretpassw0rd" sensitive = true }

Enter fullscreen mode

Exit fullscreen mode

Step 2: Use Variable in Resource

resource "local_file" "password_file" { filename = "password.txt" content = var.password }

Enter fullscreen mode

Exit fullscreen mode

Terraform Plan Output (✔ Secure)

content = (sensitive value)

Enter fullscreen mode

Exit fullscreen mode

✔ Secret hidden in CLI
✔ Still written to file

5. Important Rule (EXAM QUESTION)

Sensitive values are hidden only from CLI & logs
❌ They are NOT hidden from the state file

6. Alternative: local_sensitive_file Resource

Terraform provides special resource types for sensitive data.

Example

resource "local_sensitive_file" "password_file" { filename = "password.txt" content = "supersecretpassw0rd" }

Enter fullscreen mode

Exit fullscreen mode

Result

content = (sensitive value)

Enter fullscreen mode

Exit fullscreen mode

When to Use

If documentation says:
“Use **sensitive resource when content is sensitive”**

7. Sensitive Values and Outputs (Very Important)

❌ This Will FAIL

output "password" { value = var.password }

Enter fullscreen mode

Exit fullscreen mode

Terraform error:

Sensitive value cannot be displayed in output

Enter fullscreen mode

Exit fullscreen mode

8. Correct Way to Use Sensitive Outputs

output "password" { value = var.password sensitive = true }

Enter fullscreen mode

Exit fullscreen mode

Result

password = (sensitive value)

Enter fullscreen mode

Exit fullscreen mode

9. Why Sensitive Outputs Exist (Real Production Use)

Sensitive outputs are used because:

📌 CLI hides it, state keeps it

10. Sensitive Data in State File (Critical Concept)

Even with sensitive = true:

terraform.tfstate

Enter fullscreen mode

Exit fullscreen mode

❌ Still contains real values:

"password": "supersecretpassw0rd"

Enter fullscreen mode

Exit fullscreen mode

Conclusion

Sensitive flag does NOT encrypt state

State protection is a separate topic:

11. Provider-Level Automatic Sensitivity (AWS Example)

Terraform providers may auto-detect secrets.

Example: AWS RDS

resource "aws_db_instance" "db" { allocated_storage = 20 engine = "mysql" username = "admin" password = "mypassword123" }

Enter fullscreen mode

Exit fullscreen mode

Terraform Plan Output

username = "admin" password = (sensitive value)

Enter fullscreen mode

Exit fullscreen mode

✔ Password hidden automatically
✔ Username visible

12. Making Username Sensitive Too

variable "db_user" { default = "admin" sensitive = true } resource "aws_db_instance" "db" { username = var.db_user password = "mypassword123" }

Enter fullscreen mode

Exit fullscreen mode

Result

username = (sensitive value) password = (sensitive value)

Enter fullscreen mode

Exit fullscreen mode

13. Exam & Interview Summary (Memorize)

14. Real Production Best Practice

✔ Use:

❌ Never:

Part 1: HashiCorp Vault – High-Level Overview

1. What Is HashiCorp Vault?

HashiCorp Vault is a secrets management system used to:

Examples of Secrets

2. The Core Problem Vault Solves

❌ Common (Bad) Practice

This leads to:

3. Vault’s Biggest Feature: Dynamic Secrets

What Are Dynamic Secrets?

Secrets that are:

Example 1: Database Credentials

✅ Reduced blast radius
✅ No long-lived credentials
✅ High security

Example 2: AWS IAM Credentials

Example 3: Linux SSH Access

4. Vault GUI (Why It Matters)

Initially:

Now:

This improves:

5. Vault Encryption as a Service

Vault can:

Use Case

Instead of writing encryption logic in your application:

6. Organizational Benefit of Vault

Imagine:

❌ Without Vault:

✅ With Vault:

Part 2: Terraform Vault Provider – High-Level Overview

7. What Is the Vault Provider in Terraform?

The Vault provider allows Terraform to:

8. Typical Use Case

Scenario

secret/db_creds ├── username = admin └── password = password123

Enter fullscreen mode

Exit fullscreen mode

9. Vault Provider Configuration (Basic)

provider "vault" { address = "http://127.0.0.1:8200" }

Enter fullscreen mode

Exit fullscreen mode

This tells Terraform:

10. Reading Secrets from Vault in Terraform

Vault Secret Path

secret/db_creds

Enter fullscreen mode

Exit fullscreen mode

Terraform Code

data "vault_generic_secret" "db" { path = "secret/db_creds" }

Enter fullscreen mode

Exit fullscreen mode

11. Accessing Vault Data in Terraform

output "vault_secret" { value = data.vault_generic_secret.db.data_json sensitive = true }

Enter fullscreen mode

Exit fullscreen mode

Result

12. Important Behavior (Very Important for Exam)

Any secret read from Vault using Terraform is stored in:
terraform.tfstate

Sensitive flag:

13. Terraform State File Risk

Example:

"outputs": { "vault_secret": { "value": { "username": "admin", "password": "password123" } } }

Enter fullscreen mode

Exit fullscreen mode

👉 This is why:

14. Vault AWS Secrets Engine + Terraform

Vault can generate:

Terraform can:

Result

Terraform dynamically authenticates via Vault.

15. Real-World Terraform + Vault Flow

  1. Terraform connects to Vault
  2. Vault generates AWS credentials
  3. Terraform uses credentials
  4. Credentials expire automatically
  5. No hardcoded secrets anywhere

16. Exam-Ready Summary (Memorize This)

HashiCorp Vault

Vault Provider in Terraform

17. Important Exam Warning

Using Vault with Terraform does NOT eliminate secrets from state files.

You must:

Terraform Dependency Lock File (.terraform.lock.hcl)

1. What Is the Dependency Lock File?

When you run:

terraform init

Enter fullscreen mode

Exit fullscreen mode

Terraform automatically creates a file called:

.terraform.lock.hcl

Enter fullscreen mode

Exit fullscreen mode

This file is called the dependency lock file.

2. Why This File Exists (Core Problem)

Key Reality

Example Scenario

Component

Version

Terraform

1.2

AWS Provider

v1 → v2 → v3 (released later)

Your code:

Weeks later:

❌ Code may break
❌ Unexpected behavior
❌ Production risk

3. First Line of Defense: Version Constraints

Inside Terraform code, you define version constraints.

Example

terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.0" } } }

Enter fullscreen mode

Exit fullscreen mode

Meaning of ~> 4.0

But this alone is not enough.

4. What Happens During First terraform init

  1. Terraform reads version constraints
  2. Terraform selects one exact provider version
  3. Provider is downloaded
  4. Terraform locks that version into:

.terraform.lock.hcl

Enter fullscreen mode

Exit fullscreen mode

5. What Is Stored in .terraform.lock.hcl

Example:

provider "registry.terraform.io/hashicorp/aws" { version = "4.62.0" constraints = "~> 4.0" hashes = [ "h1:abcd...", "zh:1234..." ] }

Enter fullscreen mode

Exit fullscreen mode

Important Details

6. Why the Lock File Is Critical (Main Benefit)

Without Lock File

With Lock File

7. Real-World Team Scenario

  1. Developer A:
  1. Developer B (months later):

✔ Same behavior
✔ Same results
✔ No surprises

8. What If Code Version Changes but Lock File Doesn’t?

Scenario

version = 4.62.0

Enter fullscreen mode

Exit fullscreen mode

version = "= 4.60.0"

Enter fullscreen mode

Exit fullscreen mode

Now versions do not match.

What Happens?

terraform init

Enter fullscreen mode

Exit fullscreen mode

❌ Terraform fails with error
Because:

9. Correct Way to Upgrade or Downgrade Provider

You must explicitly tell Terraform to update providers.

Command

terraform init -upgrade

Enter fullscreen mode

Exit fullscreen mode

What This Does

10. Very Important Security Aspect (Checksums)

The lock file also contains hashes:

✔ Supply-chain security
✔ Verified binaries only

11. Important Limitation (Exam Question)

.terraform.lock.hcl ONLY tracks provider dependencies

It does NOT lock:

Modules still follow:

12. Best Practices (Production)

✔ Always commit .terraform.lock.hcl
✔ Never delete it casually
✔ Use terraform init -upgrade intentionally
✔ Review provider changelogs before upgrade

❌ Do not rely only on version constraints
❌ Do not ignore lock file errors

13. Exam-Ready Summary (Memorize)

For further actions, you may consider blocking this person and/or reporting abuse

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *

Читайте також