CloudFormation 101
Cloud Formation is a service that allows you to manage, configure and provision your AWS infrastructure as code. Resources are defined using a template that can be written in YAML or JSON format. CloudFormation interprets the template and makes the necessary API calls to create the resources you have defined.
Benefits
Avoid human errors and provision resources safely through automation
Less time and effort than configuring resources manually
You can reuse your templates to replicate your infrastructure and replicate environments in different regions within minutes
Use Case
We will create a template that will provision an EC2 instance through CloudFormation
Prerequisites
AWS account
Text editor
CloudFormation documentation
Walkthrough
Start by creating a file in your text editor of choice, we will use YAML as our language of choice for this template
A template has many optional sections, we will use a few of those along with the required ones
- Format Version:
An optional section that identifies the capabilities of the template
If you don’t specify a value, CF assumes the latest template format version is being used
The current Format Version is “2010–09–09”
AWSTemplateFormatVersion: "2010-09-09"
2. Description:
An optional section that is used to add information about what the template attempts to accomplish
Description: Launch an EC2 Instance with a choice of three instance types
3. Parameters:
An optional section that is useful for passing values in runtime while creating and updating stacks
Increases reusability of the template
We will provide three choices for InstanceType
- The type of parameter is String, we will also provide a description along with the values for our instance type
Formatting of text is very important on YAML templates, indentation will make or break your template
Parameters:
InstanceType:
Type: String
Description: EC2 Instance Type Options
AllowedValues:
- t2.nano
- t2.micro
- t2.small
4. Resources:
A required section that is used to define the AWS Resources and their properties launched by the template
You’re able to name your resources as you like, in this case, we’ll name the server — WebServerInstance
- Provide a type that describes what resource is being provisioned
- We’ll keep the properties section brief, copy an ImageId from the EC2 console in your particular region
- Use the !Ref intrinsic function to reference our InstanceType parameter created in the previous section
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: ami-026b57f3c383c2eec
InstanceType: !Ref InstanceType
5. Outputs:
An optional section in a template to define the output values for the stacks created from it
After the creation of your stack is finished, you can view the outputs on AWS CloudFormation Console
Our output for this template will be the WebServerInstanceId
- We’ll get the value for it from the WebServerInstance reference
Outputs:
WebServerInstanceId:
Value: !Ref WebServerInstance
This is how the template should look like:
6. Once the template has been completed in the text editor, save it and head to your AWS console and search for AWS CloudFormation
Click on the Create Stack button
Choose the “Template is ready” option since we have already prepared our configuration file
Pick the “Upload a template file” option
Navigate to the directory that holds our file and upload the EC2 template
Next, Provide a stack name — ec2instance
Choose an Instance Type from the parameters we provided on the template, we’ll choose t2.nano for this demo, click next
Scroll down on the Configure stack options page and click next
This is the page where you can add tags to your template, attach an IAM role to it as well as some more advanced options
The Review page for our particular template gives us additional information about the resources we will be creating and the configuration of the template, scroll down and click on the Create Stack button
7. CloudFormation will move you to the events page where you will be able to see the progress of the creation of the resources you provisioned through the template
Once the resources have been provisioned, the events page will show a CREATE_COMPLETE message
If you move over to the Outputs Tab, you’ll be able to see the Instance Id for our Web Server
When you navigate to the EC2 Console, you can see that the instance type of our WebServer is the one we chose — t2.nano.
This completes our walkthrough of CloudFormation, I’ll be back to share more of the capabilities of this service next week.