Breaking Down an AWS CloudFormation Template While Juggling!
After allocating a few hours in my day to create a template for an AWS CloudFormation project, it turned into a few days, before I finally was able to complete a template to input into an AWS CloudFormation service! I hope what I share in this article will help you create a template in a few hours or less!
Creating a template is much like juggling. The resources are the balls and making sure you define your parameters, input your properties properly, and have the exact indentation(if using a yaml-format which I did) are the skills necessary to put all the resources you need in an infrastructure you can run as code!
This article highlights the main steps I took to create a cloud infrastructure to host a website. You can refer to my code here for the final product:
https://github.com/shelleymartinez/VPC-Cloudformation-Template/blob/main/Project8template.yaml
Let’s start juggling:
First: JSON or Yaml
The First Step in creating your template is to choose whether you will use JSON or Yaml format for your template. The differences are arguable and in my opinion it comes down to personal preference. Personally, I prefer yaml as the readability was more of my speed, however, keeping track of the indentations will make or break your code. So this is why I used a separate text editor and not Cloud Design, because Cloud Design didn’t have a feature to track where the indentations where (this will make more sense once you start writing your template).
Second: AWS CloudFormation Template Structure
A template consists of the following format:
Third: Choose Your Resources
For this project the task was to create an infrastructure to be able to host a website using NGINX as the web server. The specific requirements for this was to use a VPC with a specific cidr: 10.10.0,0/16, with an auto scaling group using t2.micro instances across two public subnets. Install NGINX on each instance, and each instance should have an IP address that will contain a NGINX test page of course! The resources I used were a VPC, Internet Gateway, Internet Gateway Attachment, Public Route Table, Public Route, Instance Security Group, Launch Template, Auto Scaling Group, and a Tracking Policy for the Auto Scaling Group to make sure the auto scaling group actually works.
Fourth: Copy and Paste Your Templates for Each Resource
You can google or navigate to AWS to search for each template, but I found it easier to google, i.e. “vpc template CloudFormation”. Choose the link that will take you directly to the AWS whitepaper for that template:
Next Copy the snippet and past it in your editor. It should look like this:
Remember! This is just a template, so read through the properties in the AWS whitepaper to familiarize yourself with what properties you need and how to define them. You will not need all properties in the snippet and you have to define the properties that you will use, i.e. I used a specific cidr block for my VPC, and since I needed to specify it in other resources, I used the “Parameters” section to reference it in my code.:
Fifth: Repeat
Continue to do the same process with each Resource. If a property contains multiple information or you will use it in other resources, it will be helpful to create a parameter for it in the parameter section. I had a parameter for: the CIDR of my VPC, Subnets, CPU Target Value, Launch Template Version, and SSH Information.
Once you have completed your template you can run it in CloudDesign so you can feel confident your code will run smoothly without errors. Here is a link to an article I wrote about how to upload your code to create a stack in AWS CloudFormation: