Ghostly AMIs: How I (Efficiently) Haunted the Web with a Halloween Site on AWS

James Simpson III
7 min readAug 6, 2023

The werewolves that set up our Halloween site servers hate manual labor; they always look for ways to automate processes. In this write-up, I’ll show you how the werewolves went from a bone-chillingly repetitive server deployment to an (almost) one-click deployment using a custom AMI and some AWS magic.

Before we can dive into the gruesome details of Amazon Machine Images (AMIs), you’ll need first to experience the horrifying deployment of our web servers before the werewolves made the process as easy as flying a witch's broom. Only then can you appreciate AMIs and their witchcraft-like abilities.

Prerequisites:

  • An active AWS account
  • Source code for your custom website
  • A lit Jack-O-Lantern and some spooky jazz for ambiance 🎃

Deploy a Web Sever and Custom Website Using EC2 and Apache (The boring way)

  1. Log into the AWS console and navigate to EC2. You can search for “EC2” or choose it from the list of services.

2. Click “Launch instance.”

3. I’ll name our server “SpookyServer1,” you can name it whatever you like. For the Amazon Machine Image, select “Amazon Linux,” then scroll down.

4. Select an existing key pair or create a new one.

5. Create a new security group. For security, I only allowed SSH from my IP and then allowed any HTTP traffic so users could access my site.

6. Under Advanced details, scroll down to User data and paste the following script into the text box:

#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd

This script will install and start the Apache webserver to serve our site.

7. Click “Launch instance,” then wait for the instance to pass the health checks.

8. Once the checks have passed, select the new instance, then copy and paste the Public IP into a new browser tab to verify that the webserver is running.

9. Return to the tab with your instance details, and click “Connect.” On the SSH client tab, you’ll find the instructions for connecting to your instance via SSH.

10. On your local machine, open a terminal in the directory where your website source code lives.

11. SSH into the instance from your terminal by using the steps provided by AWS.

Before uploading the files to the EC2 instance, we need to give the ec2-user permissions to write the /var/www/html directory, where Apache will look for the index.html file

12. Run the following command:

cd /var/www/html/ && sudo chown ec2-user .

13. Now that ec2-user has permission to write to the html directory, we can end this SSH session and begin transferring the files.

14. Use the following command to upload your custom site to the instance:

scp -i path-to-key-pair -r ./ ec2-user@Public-IPv4-DNS:/var/www/html

Replace “path-to-key-pair” and “Public-IPv4-DNS” with the appropriate values.

15. Once the upload finishes, return to the browser and refresh the tab we opened to the web server’s public IP. If all goes well, you’ll see your custom site.

Whew, that was a lot to get through. I’m sure you can understand why the werewolves hated setting up our web servers by now. Imagine doing that three or even thirty times as we scale our site to meet traffic demands.

Well, there’s a better way!

Amazon Machine Images (AMIs)

AMIs are like snapshots of a specific virtual machine at a particular moment in time. They contain the operating system, application software, data, and necessary configurations. AMIs are the blueprint for creating new instances with the same setup and software as the original machine. There are Amazon-managed, 3rd party, and custom images like the one we’ll create in this tutorial.

Custom AMIs

A few benefits of creating custom AMIs are:

  1. Faster Setup: Custom images enable quick and efficient deployment of instances with pre-configured software and settings, saving valuable time.
  2. Consistent Environments: All instances launched from the same custom image have identical configurations, ensuring consistency and reducing compatibility issues.
  3. Enhanced Security: Custom images allow you to include security updates and hardened settings, providing a more secure starting point for your instances.

Create a Custom AMI

Now that we have a web server with the proper configurations to serve our website, we can use that server to create an AMI that will allow us to launch even more web servers in a fraction of the time.

  1. Return to the AWS console and navigate to the EC2 instance you created in the first part of this tutorial.

2. Click “Instance state,” then select “Stop instance.”

3. Once the instance has successfully stopped, click the Actions dropdown, select “Images and templates,” then “Create image.”

4. Give your new image a name and description, then scroll down and select “Create image.”

It may take a few minutes to create the image.

5. Click AMIs in the left-hand sidebar to check the status of your image.

6. Once the status changes to Available, navigate to Instances and click “Launch instance.”

7. This time, we’ll name the server and enter 2 in the Number of instances field.

8. Select My AMIs in the Amazon Machine Image section. Click “Owned by me,” then select the AMI you created from the dropdown.

9. Create a new key pair or assign an existing one to the new servers.

10. Instead of creating a new security group, put the new server in the security group we created in the earlier steps.

11. Click “Launch instance,” then wait for the instances to complete the status checks.

12. Choose one of the new servers and copy the Public IP into a new browser tab.

Now we have two more web servers fully configured with Apache and our website source code. No user-data script or SSH session is required! You can verify that the other server you created is working correctly by repeating step 12.

Clean-up is as simple as terminating the instances and deleting the AMI we created.

We have made it to the end of this project! In the next tutorial, I will show you how to make the application highly available using an Application Load Balancer and auto-scaling. I hope you enjoyed building with me! As always, keep it spooky! 👻

Follow me on LinkedIn: https://www.linkedin.com/in/jamessimpsoniii/

--

--

James Simpson III
James Simpson III

Written by James Simpson III

Infusing my love for Halloween 👻 and my aspirations of becoming a Cloud Engineer ☁️ to bring you the spookiest tech blog you'll ever encounter 🎃

No responses yet