Practicing Infrastructure as Code with AWS CloudFormation - Template Design and Stack Management

Define infrastructure with templates and preview impact with change sets before deployment. Detect configuration drift with drift detection and deploy across your entire organization with StackSets.

Overview of CloudFormation

CloudFormation is an IaC service that defines AWS resources in templates and provisions them as stacks. Compared to manual resource creation, it delivers environment reproducibility, version control, and automation. SAM is an extension of CloudFormation that simplifies the definition of serverless applications.

Template Design and Stack Management

Templates define Parameters (up to 200) for deployment-time input values, Mappings for environment-specific settings, and Conditions for conditional logic. A single stack can manage up to 500 resources; when this limit is exceeded, nested stacks are used for partitioning. Change sets are created before stack updates to preview which resources will be added, modified, or deleted. Resources that trigger replacement cause temporary service interruption, making change set review critical. Nested stacks split components such as VPCs, security groups, and databases into separate templates, referenced from a parent template.

Drift Detection and StackSets

Drift detection identifies whether stack resources have deviated from their template definitions. Manual console operations or script-based changes are detected as drift, and you can review the differences from the template. Automate periodic drift detection with EventBridge schedules to catch configuration deviations early. StackSets deploy the same template across multiple accounts and regions in a single operation. With Organizations integration, deployments are automated at the OU level. When a new account is added to an OU, the StackSet is automatically deployed, preventing gaps in guardrail coverage. For a systematic study of IaC, related books on Amazon can also be a useful reference.

CloudFormation Pricing and Efficiency

CloudFormation itself incurs no additional charges. Costs are limited to the usage fees of the provisioned AWS resources. Managing third-party resources incurs per-handler-operation charges per resource. To improve template development efficiency, use CloudFormation Linter (cfn-lint) to catch syntax errors early, and review change sets to confirm the impact before deployment. Reuse common resources (VPCs, security groups) with nested stacks to eliminate template duplication. The Rain CLI can streamline template formatting and deployment.

Summary

CloudFormation is a template-based IaC service that delivers infrastructure reproducibility and automation. Change sets let you preview impact before deployment, and drift detection catches configuration deviations. StackSets deploy templates across your entire organization, automating multi-account, multi-region infrastructure management.