AWS CloudFormation is a service that helps you model and set up your AWS resources so that you can spend less time managing those resources and more time focusing on your applications that run in AWS. You create a template that describes all the AWS resources that you want (like Amazon EC2 instances or Amazon RDS DB instances), and CloudFormation takes care of provisioning and configuring those resources for you. You don't need to individually create and configure AWS resources and figure out what's dependent on what; CloudFormation handles that.
Create or modify a CloudFormation template to describe all resources and their properties.
CloudFormation provisions resources as a stack, making them up and running upon stack creation.
Easily manage, update, or delete the stack and its resources as a single unit.
Quickly Replicate Your Infrastructure
Challenge:
Replicating an application in multiple regions for high availability requires replicating and configuring resources in each region.
Solution:
Reuse CloudFormation templates to create consistent and repeatable resource setups across multiple regions.
Define resources once and provision them consistently in different regions.
Easily Control and Track Changes to Your Infrastructure
Challenge:
Upgrading resources incrementally and rolling back changes manually if needed.
Tracking which resources were changed and knowing their original settings can be difficult.
Solution:
CloudFormation templates describe provisioned resources and their settings.
Track changes to infrastructure by tracking differences in templates, similar to version control for source code.
Use a version control system with templates to know what changes were made, who made them, and when.
Roll back to a previous version of the template if necessary.
Benefits of CloudFormation
Simplifies infrastructure management by handling resource provisioning and configuration.
Enables quick and consistent replication of resources across multiple regions.
Facilitates easy control and tracking of infrastructure changes, allowing for smooth rollbacks if needed.
Working with nested stacks
Nested stacks are stacks created as part of other stacks. You create a nested stack within another stack by using the AWS::CloudFormation::Stack resource.
As your infrastructure grows, common patterns can emerge in which you declare the same components in multiple templates. You can separate out these common components and create dedicated templates for them. Then use the resource in your template to reference other templates, creating nested stacks.
For example, assume that you have a load balancer configuration that you use for most of your stacks. Instead of copying and pasting the same configurations into your templates, you can create a dedicated template for the load balancer. Then, you just use the resource to reference that template from within other templates.
Nested stacks can themselves contain other nested stacks, resulting in a hierarchy of stacks, as in the diagram below.
The root stack is the top-level stack to which all the nested stacks ultimately belong. In addition, each nested stack has an immediate parent stack. For the first level of nested stacks, the root stack is also the parent stack. in the diagram below, for example:
Stack A is the root stack for all the other, nested, stacks in the hierarchy.
For stack B, stack A is both the parent stack, and the root stack.
For stack D, stack C is the parent stack; while for stack C, stack B is the parent stack.
CloudFormation helper scripts reference
AWS CloudFormation provides the following Python helper scripts that you can use to install software and start services on an Amazon EC2 instance that you create as part of your stack:
: Use to signal with a CreationPolicy or WaitCondition, so you can synchronize other resources in the stack when the prerequisite resource or application is ready.
It signals CloudFormation to indicate whether Amazon EC2 instances have been successfully created or updated.
: Use to retrieve metadata for a resource or path to a specific key.
You can use the cfn-get-metadata helper script to fetch a metadata block from CloudFormation and print it to standard out. You can also print a sub-tree of the metadata block if you specify a key. However, only top-level keys are supported.
: Use to check for updates to metadata and execute custom hooks when changes are detected.
The cfn-hup helper is a daemon that detects changes in resource metadata and runs user-specified actions when a change is detected. This allows you to make configuration updates on your running Amazon EC2 instances through the UpdateStack API action.
You call the scripts directly from your template. The scripts work in conjunction with resource metadata that's defined in the same template. The scripts run on the Amazon EC2 instance during the stack creation process.