InterviewHubPRO
Section Overview
Q43

Question 43 of 50

Part 5: Infrastructure as Code, SRE Observability & Production Troubleshooting (43 - 50)

86%
DevOps & Kubernetes (50 Qs)•Part 5: Infrastructure as Code, SRE Observability & Production Troubleshooting (43 - 50)

Infrastructure as Code (IaC): Terraform state architecture, remote backends, and state locking mechanisms.

Declarative State Management

Terraform is a declarative Infrastructure as Code tool. It records the mapping between your HCL code definitions and real-world cloud resources in a state file (terraform.tfstate).

sql
+-----------------------+               +-------------------------------------+
| Developer / CI Runner |               | Remote Backend (AWS S3 Bucket)      |
| terraform apply       |               | - Encrypted terraform.tfstate       |
+-----------------------+               +-------------------------------------+
           |                                               ^
           | Acquire State Lock (atomic)                   | Read/Write State
           v                                               v
+-----------------------+               +-------------------------------------+
| AWS DynamoDB Table    |               | Real Cloud Infrastructure (AWS)     |
| LockID = "state-hash" |               | - VPC, EKS Cluster, RDS Database    |
+-----------------------+               +-------------------------------------+

Remote Backend Blueprint (S3 + DynamoDB State Locking)

In a production team environment, never keep state on a local laptop. Use S3 with versioning and DynamoDB for atomic locking to prevent concurrent runs from corrupting the state file:

hcl
terraform {
  required_version = ">= 1.6.0"

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

  backend "s3" {
    bucket         = "production-terraform-state-12345"
    key            = "core-infra/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-state-locks"
  }
}

Core Terraform Lifecycle Phases

  1. terraform init: Downloads provider plugins (AWS, Kubernetes, Helm) and connects to remote backend.
  2. terraform plan: Performs a 3-way merge between your desired code, current state file, and real-world infrastructure (via API calls) to calculate a deterministic execution plan.
  3. terraform apply: Executes only the required CRUD operations in parallel order based on dependency graphs (Directed Acyclic Graph - DAG).