Cloud Formation Workshop
Cloud Formation Workshop
CloudFormation allows you to model your infrastructure in a text file. You can
use JSON or YAML to describe what AWS resources you want to create and
configure.
CloudFormation automates the provisioning and updating of your
infrastructure in a safe and controlled manner. There are no manual steps or
controls that can lead to errors.
CloudFormation is available at no additional charge. You pay only for the AWS
resources needed to run your applications.
Description:
String
Metadata:
template metadata
Parameters:
set of parameters
Rules:
set of rules
Mappings:
set of mappings
Conditions:
set of conditions
Transform:
set of transforms
Resources:
set of resources
Outputs:
set of outputs
Templates include several major sections. The Resources section is the only required
section. Some sections in a template can be in any order. However, as you build
your template, it can be helpful to use the logical order shown in the following list
because values in one section might refer to values from a previous section.
AWSTemplateFormatVersion: 'version date' (optional) # version of the
CloudFormation template. Only accepted value is '2010-09-09'
The only required top-level object is the Resources object, which must declare at
least one resource. The definition of each of these objects can be found in the
online Template Anatomy documentation.
Stack:
A stack is a deployment of a CloudFormation template. You can create multiple
stacks from a single CloudFormation template. A stack contains a collection of AWS
resources that you can manage as a single unit. All the resources in a stack are
defined by the stack's AWS CloudFormation template.
AWS CloudFormation will create, update or delete a stack in its entirety:
If a stack cannot be created or updated in its entirety, AWS CloudFormation
will roll it back, and automatically delete any resources that were created.
If a resource cannot be deleted, any remaining resources are retained until
the stack can be successfully deleted.
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
The Description section enables you to include comments about your template.
Description: AWS CloudFormation workshop - Resources (uksb-1q9p31idr)
(tag:resources).
You can use the Metadata section to include arbitrary JSON or YAML objects. This
section is useful for providing information to other tools that interact with your
CloudFormation template. For example, when deploying CloudFormation templates
via the AWS console, you can improve the experience of users deploying your
templates by specify how to order, label and group parameters. This can be done
with the AWS::CloudFormation::Interface key.
# Add Metadata section here.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: 'Amazon EC2 Configuration'
Parameters:
- InstanceType
ParameterLabels:
InstanceType:
default: 'Type of EC2 Instance'
Parameters enable you to input custom values to your template each time you
create or update a stack.
AWS CloudFormation supports the following parameter types: String, Number, List,
CommaDelimitedList, AWS-Specific Parameter Types( AWS::EC2::VPC::Id) , SSM
Parameter Types( SSM parameter types correspond to existing parameters in
Systems Manager Parameter Store.)
The required Resources section declares the AWS resources that you want to include
in the stack. Let's add the EC2 resource to your stack. The only required property of
the EC2 resource type is ImageId.
Resources:
WebServerInstance:
Type: 'AWS::EC2::Instance'
Properties:
InstanceType: !Ref InstanceType
ImageId: <replace with AMI ID ami-xxxxx>
Deploying an EC2 Instance using CloudFormation:
# Add Format Version section here.
AWSTemplateFormatVersion: "2010-09-09"
Intrinsic Functions:
Intrinsic functions are built-in functions that help you manage your stacks. Without
them, you will be limited to very basic templates
Use the Ref function to dynamically assign parameter values to a resource
property.
Tag an instance with Fn::Join function.
Add a tag to the instance using Fn::Sub function.
In the last lab you have "hard coded" an AMI ID directly into the EC2 Resource
property. You will now amend this to make your template more flexible. Let's
convert AmiID to variable and pass it to resource property at the runtime.
First, create a new parameter called AmiID and put it in the Parameters section of
your template.
AmiID:
Type: AWS::EC2::Image::Id
Description: 'The ID of the AMI.'
Use the intrinsic function Ref to pass the AmiID parameter input to the EC2 resource
property.
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Properties:
# Use !Ref function in ImageId property
ImageId: !Ref AmiID
InstanceType: !Ref InstanceType
To help you manage your AWS resources, you can optionally assign your own
metadata to each resource in the form of tags. Each tag is a simple label consisting
of a customer-defined key, and an optional value that can help you to categorize
resources by purpose, owner, environment, or other criteria. Let's use the intrinsic
function Fn::Join to name your instance.
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref AmiID
InstanceType: !Ref InstanceType
Tags:
- Key: Name
Value: !Join [ '-', [ !Ref InstanceType, webserver ] ]
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Amazon EC2 Configuration
Parameters:
- InstanceType
- AmiID
ParameterLabels:
InstanceType:
default: Type of EC2 Instance
AmiID:
default: Amazon Machine Image ID
Parameters:
InstanceType:
Description: Enter t2.micro or t2.small. Default is t2.micro.
Type: String
AllowedValues:
- t2.micro
- t2.small
Default: t2.micro
AmiID:
Description: The ID of the AMI.
Type: AWS::EC2::Image::Id
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !Ref AmiID
InstanceType: !Ref InstanceType
Tags:
- Key: Name
Value: !Join ['-', [!Ref InstanceType, webserver]]
- Key: InstanceType
Value: !Sub ${InstanceType}