How do you implement infrastructure as code for reproducible cloud environments?
Infrastructure as Code (IaC) manages cloud infrastructure through version-controlled configuration files. Tools: Terraform (multi-cloud, HCL language, state management), Pulumi (real programming languages: TypeScript, Python, Go), CloudFormation (AWS-native, JSON/YAML), and CDK (Cloud Development Kit, generate CloudFormation from code). Terraform implementation: organize code into modules (VPC module, database module, application module), use workspaces for environment separation (dev, staging, production), implement state management with remote backend (S3 + DynamoDB for locking), and use data sources to reference existing resources. Best practices: use consistent naming conventions with variables (${project}-${environment}-${resource}), implement tagging standards for cost allocation and ownership, use modules for reusable infrastructure patterns, pin provider and module versions for reproducibility, and implement code review for infrastructure changes. CI/CD for infrastructure: plan on PR (terraform plan as PR comment), apply on merge to main (automated with approval gates), implement drift detection (scheduled plan to detect manual changes), and maintain separate pipelines per environment. State management: never commit state files to version control, use remote state with locking to prevent concurrent modifications, implement state backup and recovery procedures, and use state import for adopting existing resources. Security: scan IaC for misconfigurations (Checkov, tfsec, Bridgecrew), implement least-privilege IAM policies, encrypt sensitive values in state, and use dynamic secrets from Vault rather than hardcoded credentials. Testing: use Terratest for integration testing, implement policy-as-code (OPA, Sentinel) for compliance enforcement, and validate changes in a staging environment before production.