Cloud Formation and YAML Resource Creation and Management
By Morgan Lucas
This is a post intended for this site, as a way to get a feel of using it consistently. Older posts are here.
A big shoutout to Pluralsight for their free weekend, and James Millar for a great introductory course.
Why did we make up so many programming languages that only work based upon the correct formatting of spaces and tabs? Readability is important, but when you're just beginning, how I learn, I'm interested in 'it works' first and 'it's pretty' second. Anyway, here's what I learned.
What is Cloud Formation?
AWS’s house-built language, build upon YAML, to deploy their infrastructure using Infrastructure As Code.
What Did I Do?
Create an Instance (Virtual Computer) that has basic security rules from a security group it's in.
I also created a KeyPair to access the Virtual Machines remotely, and placed security rules. The intention was with a Firewall, but...
The most difficult part was learning the tool and the formatting. This was much easier in Terraform for AWS, but this time we wanted to use CloudFormation.
But now I can do it!
I used two variables !ref!
Find my code here at GitHub.
Resources Created
VPC (A virtual private cloud — Your own personal network on AWS)
MacOSImageID*
MacOSInstanceKeyName*
InternetGateway
PublicInternetRoute — How will your resources in your VPC reach the internet? Through here.
VPCGatewayAttachment
PublicInternetRouteTable
PublicSubnet01
PublicSubnet01RouteTableAssociation
SecurityGroup
NetworkInterfaceAttachment
*The Mac Instances were placed within later.
The GUI Option
You can use a GUI to drag and drop elements; It will make a JSON file. It still has a learning curve; Just because you can visualize it doesn't mean things automatically connect.
You may also see a 'host' and an 'instance' mentioned - The difference is minutia; One is more expensive than the other to run.
YAML Spacing
It's picky!
This required me to make a Mac Virtual Machine. Only available in the East Virginia region through EC2; I ended up using a regular EC2 instance in its place for testing, so the client could upload the file and switch in their own Mac Instance.
You use !Ref to tie things to other elements. If I have an EC2 Instance, I'd use !Ref in the space where it asks about Firewalls to reference the one I have without hardcoding it in, very much like Variables in Terraform For Azure and AWS.
Firewalls
The intention was to have a firewalls with access rules in place. The firewall documentation was the clearest documentation in AWS Cloud Formation section;
Type: AWS::NetworkFirewall::Firewall
Properties:
DeleteProtection: Boolean
Description: String
FirewallName: String
FirewallPolicyArn: String
FirewallPolicyChangeProtection: Boolean
SubnetChangeProtection: Boolean
Tags:
- Tag
VpcId: String
A lot of people hadn't migrated to Firewalls as of this creation. Security Groups, at a basic level, get the job done, but I can work with firewalls in Cloud Formation.
There are quirks in the programming; Most of the time, I've found !Ref works. But when making a firewall, it preferred Fn::GetAtt [name,name].
KeyPairs
I found no page for the header type for integrating Key Pairs. The Key Pair parameter doesn't go where I thought, it goes under Parameters for the EC2 Instance.
Parameters:
macOSImageId:
Type: AWS::EC2::Image::Id
Default: ami-074f143fc810219ae
macOSInstanceKeyName:
Type: AWS::EC2::KeyPair::KeyName
Default: macos
Clarity
A lot of things became clear the more I learned - ex. hitting Template and Edit in Designer when a stack failed to launch.
The fact that it doesn't check the fields of things that must be specified until the end is a real pain. To its credit, it will throw all of the errors at you if they relate to other network elements - If there are errors in the EC2 instance, those are separate from those in a Firewall. It's easy to read.
I didn't have a lot of the Properties under my AWS::EC2::Route section; Remember the documentation says a lot of them aren't required. And yet;
So is there another part of my template making it necessary? Such as 'Oh, you have part A, now you need part B so it all works'?
I was eventually lead down the path of circular dependencies; Shout out to the 2nd piece of useful documentation I've come across about AWS Cloud Formation.
Once again, AWS Documentation was a little too high level for a beginner, so Cloud Academy came through with how to actually set it up after installation.
At least 3 times on Twitter, I had to ask the general population if it was intended to be this unclear, and 3 times the AWS team apologized and told me to submit feedback.
I thought it was just me, but no, their documentation confuses many people.
To their credit, AWS did come through with the actual commands; Well done! I connected via the console and bungled setting up the credentials through 'aws configuration' line - Which turned out to be a very insecure method of securing an EC2 instance.
Here's a better way that involves policies and IAMs. Their course on Udemy is great, and thank you again to a LI connection for the gifted course!
After lots of trial, error, and help, I managed to push an instance and some buckets - I can log into the instance!