Infrastructure as Codecritical

CloudFormation

CloudFormation provisions AWS infrastructure as code using JSON or YAML templates. Resources declared in a template are created, updated, and deleted together as a stack, maintaining the desired state automatically.

Memory anchor

CloudFormation is IKEA instructions for AWS — the template is the instruction manual, the stack is the assembled furniture. Change sets are checking the instructions before picking up the screwdriver. DeletionPolicy: Retain means 'if I return the furniture, keep the important screws.'

Expected depth

Template sections: Parameters (inputs), Mappings (static lookup tables), Conditions (conditional resource creation), Resources (required, the actual infrastructure), Outputs (values exported from the stack). Change sets preview what changes a template update will make before executing. Stack policies protect critical resources from accidental updates or deletion. Drift detection identifies manually changed resources that no longer match the template. Cross-stack references: Outputs exported from one stack imported by another with !ImportValue.

Deep — senior internals

CloudFormation has a 500-resource limit per stack — use nested stacks or stack sets for larger architectures. Stack Sets deploy stacks across multiple accounts and regions simultaneously. CloudFormation custom resources use Lambda to manage resources not natively supported. The DeletionPolicy attribute (Retain, Snapshot, Delete) controls what happens to resources when the stack is deleted — Retain is critical for production databases. UPDATE_ROLLBACK_FAILED is a dreaded state: the stack failed to update AND failed to roll back. Resolution: continue-update-rollback command, skipping problematic resources. CloudFormation Hooks run validations before and after resource changes for compliance enforcement.

🎤Interview-ready answer

CloudFormation manages infrastructure as declarative YAML/JSON templates. I use change sets to preview updates, stack policies to protect production resources from deletion, and DeletionPolicy: Retain on databases. For multi-account deployments, StackSets deploy consistently across accounts. I prefer CDK over raw CloudFormation for complex infrastructure because it provides loops, conditions, and type safety in real programming languages.

Common trap

Not setting DeletionPolicy: Retain on production databases. If someone deletes the CloudFormation stack (accidentally or during cleanup), the database is deleted with it. Retain keeps the resource even after the stack is gone.