Spinning up an EC2 Instance and VPC via CloudFormation

Toni Benn
6 min readJan 29, 2022

--

A beginners guide to creating stacks in CloudFormation

In this beginner friendly project we’re going to work with CloudFormation to: setup a VPC and spin up an EC2 instance within the VPC we created with Apache installed and enabled. But first, let’s cover the basics.

What is CloudFormation?

CloudFormation lets you lets you model, provision, and manage AWS and third-party resources by treating infrastructure as code (AWS). In simpler terms, CloudFormation allows you to deploy a handful of services or applications at once via templates. This method reduces the possibility of errors as a result of setting up everything manually. The great thing about CloudFormation is that there are many templates provided that can help you automate deployment.

For this project, I utilized two CloudFormation templates taken from Github that I forked and edited to fit my use case. I’d recommend you check it out and do the same.

Working with Cloud Formation has been a little difficult for me because of how tricky the syntax can be. The AWS documentation does its best with stating what goes where but customizing a template can pose as a challenge for a beginner. But nevertheless, this project was a step in the right direction of getting my hands dirty with this service. If you’re ready to do the same, then let’s get started 🚀!

Step 1 (VPC Setup)

Head over to Github and either fork this file into your own Github account or download the raw .yaml file onto your local environment.

I’d recommend that you download the raw file like this:

Then save the file onto your local environment. A .yaml file should be downloaded.

Currently my VPC is being created in the us-east-1 region. If you’d like your VPC and other services to be created in a different region, you’ll need to make that change.

Now head over to CloudFormation in the AWS console.

Select “Create Stack” and chose the option “With new resources”.

Select “Template is ready”, “Upload a template file” and grab the .yaml file we downloaded from Github.

Once the file has been uploaded, hit “Next”.

Give your stack a name, and chose the Availability Zone that aligns with what you put in the Default region of your VPC .yaml file. Click “Next”.

For now we can go ahead and skip past the “Configure Stack Options” page.

Once we’re on the review screen, review that everything looks okay and hit “Create Stack”.

You’ll be prompted back to your CloudFormation dashboard and will see that your stack is in progress of being created.

After a few minutes, you’ll see that your stack has been created.

Step 2 (EC2 creation)

First we’re going to create a Key 🔑 Pair. Later we will need to input the Key Pair information into our CloudFormation template.

Go to EC2 in the AWS console, scroll down and select Key Pair.

Now select “Create key pair”.

Select a Key Pair name, keep the default settings and click “Create key pair”. The console will automatically download the Key Pair but make sure you write it down somewhere because you’ll need it later.

Now let’s grab the EC2 CloudFormation template from Github and follow suit with what we did in step 1 (downloading the raw file onto your local environment). In this step, we will for sure need to edit the code. If you’re a Mac user like myself, I’d recommend BBedit .

Once you have the file downloaded onto your computer and open it with a code editor, input the name of the VPC stack created in Step 1 into the “Description”, and “Default” area of this parameter.

Input the name of the Key pair we just created into the “Description” area.

For this project we’re going to create a t3.nano instance.

In this final step of code edits, we’re going to head back to the EC2 dashboard, go to “Instances” and select “Launch Instance”.

For this project we’re going to use an Amazon Linux 2 AMI so we need to grab the appropriate AMI ID. Copy it onto your clipboard. Make sure that your region is currently set to the same region that your VPC was created in.

Now take this AMI ID and copy it into this part of the CloudFormation template.

Save the document so that all of the updates are included.

Head back to the CloudFormation dashboard and we’re going to repeat what we did in Step 1 by uploading our edited .yaml template. Once you’ve done that, you’ll notice that the “Specify stack details” look a bit different than they did before.

Our AMI ID is automatically added in addition to the instance type and VPCStackName we want our new stack to point to. Hit “Next” at the bottom.

Skip past the “Configure Stack Options” page and head to “Review Instance” page and click down to the bottom and click “Create”.

Your EC2 instance with Apache should be spinning up now.

After a few moments it should be ready.

Awesome 😎 ! Now let’s head back to the EC2 dashboard to confirm that everything is up and running as expected.

EC2 Instance up and running 🏃‍♀️

Click on the Instance ID and copy the Public IPv4 address and paste it into another tab.

Our Apache web server with a message has uploaded!

Congratulations for making it to the end of this walkthrough!

--

--