Module 14 - CloudFormation
AWS CloudFormation
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.
Benefits
Deployment speed
Scaling up
Service integration
Consistency
Security
Easy updates
CloudFormation Concepts
When you use AWS CloudFormation, you work with templates and stacks. You create templates to describe your AWS resources and their properties.
Templates
Stacks
Change sets
Templates
A CloudFormation template is a JSON or YAML formatted text file.
You can save these files with any extension, such as .json, .yaml, .template, or .txt.
CloudFormation uses these templates as blueprints for building your AWS resources. For example, in a template, you can describe an Amazon EC2 instance, such as the instance type, the AMI ID, block device mappings, and its Amazon EC2 key pair name.
Whenever you create a stack, you also specify a template that CloudFormation uses to create whatever you described in the template.
Example Template format
Stacks
When you use CloudFormation, you manage related resources as a single unit called a stack.
You create, update, and delete a collection of resources by creating, updating, and deleting stacks.
All the resources in a stack are defined by the stack's CloudFormation template.
Suppose you created a template that includes an Auto Scaling group, Elastic Load Balancing load balancer, and an Amazon Relational Database Service (Amazon RDS) database instance.
To create those resources, you create a stack by submitting the template that you created, and CloudFormation provisions all those resources for you.
Change sets
If you need to make changes to the running resources in a stack, you update the stack.
Before making changes to your resources, you can generate a change set, which is a summary of your proposed changes.
Change sets allow you to see how your changes might impact your running resources, especially for critical resources, before implementing them.
Example
if you change the name of an Amazon RDS database instance, CloudFormation will create a new database and delete the old one.
You will lose the data in the old database unless you've already backed it up.
If you generate a change set, you will see that your change will cause your database to be replaced, and you will be able to plan accordingly before you update your stack.
How does cloudformation works
When creating a stack, AWS CloudFormation makes underlying service calls to AWS to provision and configure your resources.
CloudFormation can only perform actions that you have permission to do.
For example, to create EC2 instances by using CloudFormation, you need permissions to create instances.
You'll need similar permissions to terminate instances when you delete stacks with instances.
You use AWS Identity and Access Management (IAM) to manage permissions.
The calls that CloudFormation makes are all declared by your template.
Template Anatomy
A template is a JSON- or YAML-formatted text file that describes your AWS infrastructure.
Format Version
The AWSTemplateFormatVersion section (optional) identifies the capabilities of the template. The latest template format version is 2010-09-09 and is currently the only valid value.
JSON
YAML
Description
The Description section (optional) enables you to include comments about your template.
The value for the description declaration must be a literal string that is between 0 and 1024 bytes in length
JSON
YAML
Metadata
You can use the optional Metadata section to include arbitrary JSON or YAML objects that provide details about the template.
JSON
YAML
Meta keys
AWS::CloudFormation::Init
AWS::CloudFormation::Interface
AWS::CloudFormation::Designer
Parameters
Use the optional Parameters section to customize your templates. Parameters enable you to input custom values to your template each time you create or update a stack.
Example
declare a parameter named InstanceTypeParameter. This parameter lets you specify the Amazon EC2 instance type for the stack to use when you create or update the stack. Note that InstanceTypeParameter has a default value of t2.micro.
JSON
YAML
Rules
The optional Rules section validates a parameter or a combination of parameters passed to a template during a stack creation or stack update. To use template rules, explicitly declare Rules in your template followed by an assertion.
A rule can include a RuleCondition property and must include an Assertions property.
Rule specific intrinsic functions
To define a rule condition and assertions, use rule-specific intrinsic functions, which are functions that can only be used in the Rules section of a template. You can nest functions, but the final result of a rule condition or assertion must be either true or false.
Fn::And
Fn::Contains
Fn::EachMemberEquals
Fn::EachMemberIn
Fn::Equals
Fn::If
Fn::Not
Fn::Or
Fn::RefAll
Fn::ValueOf
Fn::ValueOfAll
Mappings
The optional Mappings section matches a key to a corresponding set of named values.
For example, if you want to set values based on a region, you can create a mapping that uses the region name as a key and contains the values you want to specify for each specific region. You use the Fn::FindInMap intrinsic function to retrieve values in a map.
JSON
YAML
Conditions
The optional Conditions section contains statements that define the circumstances under which entities are created or configured.
For example, you can create a condition and then associate it with a resource or output so that AWS CloudFormation only creates the resource or output if the condition is true.
JSON
YAML
Transform
The optional Transform section specifies one or more macros that AWS CloudFormation uses to process your template. The Transform section builds on the simple, declarative language of AWS CloudFormation with a powerful macro system.
You can declare one or more macros within a template. AWS CloudFormation executes macros in the order that they're specified.
When you create a change set, AWS CloudFormation generates a change set that includes the processed template content. You can then review the changes and execute the change set.
Resources
The required Resources section declares the AWS resources that you want to include in the stack, such as an Amazon EC2 instance or an Amazon S3 bucket.
JSON
YAML
Output
The optional Outputs section declares output values that you can import into other stacks (to create cross-stack references), return in response (to describe stack calls), or view on the AWS CloudFormation console.
JSON
YAML
AWS Cloudformation Designer
AWS CloudFormation Designer (Designer) is a graphic tool for creating, viewing, and modifying AWS CloudFormation templates.
With Designer, you can diagram your template resources using a drag-and-drop interface, and then edit their details using the integrated JSON and YAML editor.
Whether you are a new or an experienced AWS CloudFormation user, AWS CloudFormation Designer can help you quickly see the interrelationship between a template's resources and easily modify templates.
Benefits
Visualize template resources
Simplify template authoring
Simplify editing with the integrated JSON and YAML editor
Desginer Overview
How to create Cloudformation
step 1: Login into AWS management console and serach cloudformation service
Step 2: In this step we can give stack name and check all our configuration and click next
Step 3: Specify stack details
Step 4: Review our configuration and click create.
+ All of our resources are created as a stack we can change and delete using the stack.
Pricing
There is no additional charge for using AWS CloudFormation with resource providers in the following namespaces: AWS::, Alexa::, and Custom::*. In these cases, you pay for AWS resources such as Amazon Elastic Compute Cloud (EC2) instances, Elastic Load Balancing load balancers, etc.
created using AWS CloudFormation the same as if you had created them manually.
You only pay for what you use, with no minimum fees and no required upfront commitments.
Last updated