DEV Community

Aisalkyn Aidarova
Aisalkyn Aidarova

Posted on

TERRAFORM PRODUCTION LAB (SENIOR LEVEL)

🎯 Objective

Build production-grade Terraform infrastructure with:

  • Remote state (S3 + DynamoDB)
  • Secure S3 bucket (encryption, versioning, blocking public access)
  • DynamoDB locking
  • ECR (container registry)
  • Secrets management (SSM + Secrets Manager)
  • Proper security practices

πŸ”· PART 1 β€” WHY THIS MATTERS (INTERVIEW ANSWER)

❓ Why S3 + DynamoDB?

Answer (short, interview-ready):

  • S3 β†’ stores Terraform state centrally
  • DynamoDB β†’ prevents concurrent runs (locking)
  • Prevents corruption and race conditions
  • Enables team collaboration

❓ Why ECR?

  • Store Docker images securely
  • Integrate with ECS/EKS
  • IAM-controlled access
  • Avoid public registries (security risk)

❓ Why Secrets Management?

❌ BAD:

password = "admin123"
Enter fullscreen mode Exit fullscreen mode

βœ… GOOD:

  • AWS SSM Parameter Store
  • AWS Secrets Manager
  • Avoid storing secrets in:

    • Terraform code
    • GitHub
    • state file (important!)

πŸ”· PART 2 β€” PROJECT STRUCTURE (PRODUCTION)

terraform-prod/
β”‚
β”œβ”€β”€ backend/
β”‚   └── main.tf          # S3 + DynamoDB (bootstrap)
β”‚
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ s3/
β”‚   β”œβ”€β”€ dynamodb/
β”‚   β”œβ”€β”€ ecr/
β”‚   └── secrets/
β”‚
β”œβ”€β”€ envs/
β”‚   └── prod/
β”‚       β”œβ”€β”€ main.tf
β”‚       β”œβ”€β”€ backend.tf
β”‚       β”œβ”€β”€ variables.tf
β”‚
└── README.md
Enter fullscreen mode Exit fullscreen mode

πŸ”· PART 3 β€” BOOTSTRAP (CREATE BACKEND FIRST)

backend/main.tf

provider "aws" {
  region = "us-east-2"
}

resource "aws_s3_bucket" "tf_state" {
  bucket = "jumptotech-tf-state-prod"

  lifecycle {
    prevent_destroy = true
  }
}

resource "aws_s3_bucket_versioning" "versioning" {
  bucket = aws_s3_bucket.tf_state.id

  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_server_side_encryption_configuration" "encryption" {
  bucket = aws_s3_bucket.tf_state.id

  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "AES256"
    }
  }
}

resource "aws_s3_bucket_public_access_block" "block" {
  bucket = aws_s3_bucket.tf_state.id

  block_public_acls   = true
  block_public_policy = true
  ignore_public_acls  = true
  restrict_public_buckets = true
}

resource "aws_dynamodb_table" "tf_lock" {
  name         = "terraform-lock"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ RUN FIRST (BOOTSTRAP)

cd backend
terraform init
terraform apply -auto-approve
Enter fullscreen mode Exit fullscreen mode

πŸ”· PART 4 β€” REMOTE BACKEND CONFIG

envs/prod/backend.tf

terraform {
  backend "s3" {
    bucket         = "jumptotech-tf-state-prod"
    key            = "prod/terraform.tfstate"
    region         = "us-east-2"
    dynamodb_table = "terraform-lock"
    encrypt        = true
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”· PART 5 β€” ECR MODULE (PRODUCTION)

modules/ecr/main.tf

resource "aws_ecr_repository" "repo" {
  name = var.name

  image_scanning_configuration {
    scan_on_push = true
  }

  encryption_configuration {
    encryption_type = "AES256"
  }
}
Enter fullscreen mode Exit fullscreen mode

modules/ecr/variables.tf

variable "name" {}
Enter fullscreen mode Exit fullscreen mode

πŸ”· PART 6 β€” SECRETS (CRITICAL PART)

OPTION 1 β€” SSM Parameter Store

resource "aws_ssm_parameter" "db_password" {
  name  = "/prod/db/password"
  type  = "SecureString"
  value = var.db_password
}
Enter fullscreen mode Exit fullscreen mode

⚠️ PROBLEM:

  • Stored in Terraform state β†’ still risky

βœ… BEST PRACTICE (SENIOR LEVEL)

OPTION 2 β€” Secrets Manager (recommended)

resource "aws_secretsmanager_secret" "db" {
  name = "prod-db-secret"
}

resource "aws_secretsmanager_secret_version" "db_value" {
  secret_id     = aws_secretsmanager_secret.db.id
  secret_string = jsonencode({
    username = "admin"
    password = var.db_password
  })
}
Enter fullscreen mode Exit fullscreen mode

πŸ”΄ CRITICAL KNOWLEDGE (INTERVIEW)

❗ Terraform STILL stores secrets in state!

πŸ‘‰ Solution:

  • Use:

    • External secret injection (CI/CD)
    • Vault
    • AWS IAM roles instead of passwords

πŸ”· PART 7 β€” USING ECR + SECRETS IN PROD

envs/prod/main.tf

provider "aws" {
  region = "us-east-2"
}

module "ecr" {
  source = "../../modules/ecr"
  name   = "prod-backend"
}

module "secrets" {
  source      = "../../modules/secrets"
  db_password = var.db_password
}
Enter fullscreen mode Exit fullscreen mode

πŸ”· PART 8 β€” VARIABLES

envs/prod/variables.tf

variable "db_password" {
  sensitive = true
}
Enter fullscreen mode Exit fullscreen mode

RUN

cd envs/prod

terraform init
terraform plan
terraform apply
Enter fullscreen mode Exit fullscreen mode

πŸ”· PART 9 β€” HOW TO PASS SECRETS (PRODUCTION)

❌ NEVER DO THIS

db_password = "mypassword"
Enter fullscreen mode Exit fullscreen mode

βœ… USE ENV VARIABLES

export TF_VAR_db_password="supersecure123"
terraform apply
Enter fullscreen mode Exit fullscreen mode

βœ… EVEN BETTER (CI/CD)

  • GitLab / GitHub Actions secrets
  • AWS IAM role (OIDC)
  • No hardcoded secrets

πŸ”· PART 10 β€” SECURITY BEST PRACTICES (MUST KNOW)

βœ… S3

  • Versioning ENABLED
  • Encryption ENABLED
  • Public access BLOCKED
  • Logging enabled (optional)

βœ… DynamoDB

  • Locking prevents corruption

βœ… Terraform

  • Use remote state
  • Never commit .tfstate
  • Use .gitignore

βœ… Secrets

  • Never in code
  • Never in GitHub
  • Prefer IAM roles over passwords

πŸ”· PART 11 β€” REAL INTERVIEW QUESTIONS

1. What is Terraform state?

β†’ Tracks real infrastructure vs code


2. What is state locking?

β†’ Prevents concurrent updates (DynamoDB)


3. What happens if two engineers run apply?

β†’ Without locking β†’ corruption
β†’ With DynamoDB β†’ one waits


4. Where is state stored in production?

β†’ S3 (remote backend)


5. Can Terraform manage secrets securely?

πŸ‘‰ BEST ANSWER:

  • Terraform can create secrets
  • BUT not ideal to store them
  • Use external secret systems (Vault / AWS Secrets Manager / CI/CD)

6. Why ECR instead of Docker Hub?

  • Private
  • IAM integrated
  • Secure
  • No rate limits

7. What is drift?

β†’ Infrastructure changed outside Terraform


πŸ”· PART 12 β€” ADVANCED (SENIOR LEVEL)

Must Know:

  • Remote backend
  • State locking
  • Modules (reusable)
  • Sensitive variables
  • IAM roles (instead of passwords)
  • CI/CD integration
  • OIDC (GitLab β†’ AWS)
  • Drift detection
  • Terraform plan in PR

πŸ”· FINAL REAL-WORLD FLOW

Developer β†’ Git push
        ↓
CI/CD Pipeline
        ↓
terraform plan (MR)
        ↓
Approval
        ↓
terraform apply (protected branch)
        ↓
State stored in S3
        ↓
Locking via DynamoDB
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ WHAT MAKES THIS β€œ6-YEAR ENGINEER LEVEL”

You are not just writing Terraform.

You understand:

  • Security
  • State management
  • Team workflows
  • CI/CD integration
  • Secrets handling
  • Production risks

Top comments (0)