Intro to Cloud Formation - Infra as Code
CloudFormation is an Infrastructure as Code (IaC) service where we define the resources we need to deploy in AWS in a coding manner. Generally, CloudFormation uses JSON or YAML for declaring resources. This file is treated as a template, which is then uploaded to the CloudFormation "Stack" section for creation.
CloudFormation is a declarative language, so you don't have to worry about the sequence in which you list your resources—AWS automatically figures out the correct order for deployment.
How Stacks Work
When you create a stack, each resource is tagged with a unique identifier for better visibility. By default, deleting a stack results in the termination of all resources created by it. You can avoid this by using the Deletion Policy flag (setting it to Retain), which prevents specific resources from being deleted.
Note: If an S3 bucket is part of your stack and it isn't empty, the deletion will fail even if your policy allows it. AWS requires buckets to be empty before they can be removed - can be done using AWS Lambda.
Pros of Using CloudFormation
More control: Everything is configured from a YAML file, and AWS provides comprehensive documentation to guide you.
Ease of deployment: You can reuse the same template with small tweaks or use Parameters to make the infrastructure dynamic based on user input.
Infrastructure Composer: An additional benefit is viewing your architecture visually to see how services are interlinked—something not easily available when creating services manually.
Why use this instead of the GUI?
The main reason is automation across environments. Once your template is ready, you can deploy multiple linked services for different stages. For example, a Dev instance might only require 2 EC2 units, whereas PROD might require 4 to handle a much larger load. You can manage this difference easily within the code rather than clicking through the console every time.
Core Components
Template Sections: Beyond just resources, templates use "Parameters" for input and "Helper Functions" (like
Ref) to link items together.Logical Sequencing: AWS configures which step to run first and last by looking at dependencies, meaning your declaration order doesn't limit the deployment logic.



