Creating a Custom Amazon Machine Image for a EC2 Instance Using the AWS Console or AWS CLI

Michael Johnson
9 min read2 days ago

--

Background Scenerio

For today’s demostration we will be using our good old example of Level-Up Bank. Level Up Bank wants to move its website and online banking platform to the cloud to improve scalability, reliability, and security. To do this we will create a new EC2 Instance with a Custom AMI that will have all packages already updated and Apache HTTP Server preinstalled and started. After doing so let’s discuss why a bank may want to move to the cloud a little more in-depth.

Pre-requisites

  • AWS Account
  • AWS CLI Installed
  • Acess and Secret Access Key if using CLI
  • Basic Understanding on EC2
  • Basic Bash Scripting
  • Knowledge of SSH and KeyPairs if using SSH

Log Into The AWS Mangement Console and Configure User Data

First we will go to EC2 using the management console. For this demonstration we will use an Amazon Linux 2023, and for the Instance type we will use a t2.micro to stay in free tier.

EC2 Instance Name
Default AMI
Instance type

Since I want to have SSH capabilities I select an existing Key-Pair I already have.

Next I select a security group I already have created that has rules to allow SSH and HTTP traffic.

Now here in the important part. In order to customize the AMI to come with updates all the packages, installs Apache, and starts the Apache service we need to add a bash script to the User Data field under the Advance details tab.

After scrolling to the bottom of the Advance details tab I add a bash script

Within this field I add the following code:

#!/bin/bash
# Update package repository
sudo dnf update -y
# Install Apache HTTP server
sudo dnf install -y httpd
# Start Apache
sudo systemctl start httpd
# Enable Apache to start on boot
sudo systemctl enable httpd

I will also copy this code to save as a file named user-data.sh for later use. That being said after typing in this code I launch the instance.

After a couple of minutes the instance should be read to view. If our User data script was written correctly when we go to the public IP of our website we should see an Apache test page.

Apache Webserver default test page when enabled on a Amazon Linux 2023 AMI

As we can see our custom User data script was successful. Next lets create a custom AMI using the management console, but before we do that I came going to do a bonus step. I am going to add a custom index.html to our Apache Webserver. So I’ll SSH into our instance and go to the var/www/html directory and add an index.html I add earlier. That way when I create an image of this EC2 it will include this change. Perfect now when I go to our Public IP for this EC2 instance it looks like this:

Using an Image of our EC2 to Create a Custom AMI

Now I go back to the AWS Management Console and select our EC2 instance and click on Actions

And I will then create an Image of this EC2

Okay let’s name it and then save this new Image, I will keep all the other settings as default

After our image registers we can now make a new EC2 Instance using our custom. To do this we will just follow the previous steps, or we can go to the AMI section of our management console lefthand toolbar and Launch Instance from AMI. That said I followed the previous steps I originally took except this time I selected our custom AMI and didn’t entire anything in the User data field.

So let’s test our new instance’s public IP address

And success!! To show these are indeed two seperate instances I will share the last digit of each of their public IP addresses:

Now let’s finish up by doing all of this with the AWS CLI.

Creating Custom AMI using the AWS CLI

Now let’s make an AMI using the CLI to do so we need to first run the command all in one line:

aws ec2 run-instances \
--image-id ami-<ami-id> \
--count 1 \
--instance-type <instance-type> \
--key-name <key-pair-name> \
--security-group-ids <security-group-id> \
--subnet-id <subnet-id> \
--user-data file://path/to/your/user-data-script.txt

I used the AMI ID for Amazon Linux 2023, my keypair from earlier, my security group from earlier, and I looked up my subnet using aws ec2 describe-subnets and for the user-data field I will use the user-data.sh file from earlier and provide it’s absolute path. For user-data.sh I made sure to change it’s permissions using chmod =x user-data.sh as well. With that all out of the way let’s create our new instance via CLI.

So let’s SSH into our new instance and comfirm everything was installed correctly using systemctl status httpd and by visting the public IP address.

As we can see Apache was preinstalled at launch and we can visit the public IP address. Now for a bonus step again I will add our custom index.html file for this instance before we make an Image for custom AMI via the AWS CLI.

Creating an Image of our Customize Instance Via CLI

Okay now let’s make an Image of this instance. To do that I will open a new terminal and type in the command:

aws ec2 create-image --instance-id i-1234567890abcdef0 --name MyEC2Instance-AMI --description "AMI created from EC2 instance"

To get the instance ID I type aws ec2 describe-instances so I go ahead fill in my necessary details and press enter. Next after the Image is created I run the command:

aws ec2 run-instances --image-id your-ami-id --instance-type your-instance-type --key-name your-key-pair --security-group-ids your-security-group-id --subnet-id your-subnet-id

I wait for the instance to be in a running state and visit the public IP address

As we can see Apache was installed and enabled. And we know this is a new instance thanks to the public IP address ending in .114 instead of ending in .212

And just like that we made a new AMI for our friends at Level Up Bank. In conclusion Lets now go over why a company like a bank may want to use a cloud server in the first place.

Conclusion

So why would a company such as a bank want to do this? Why would it want to move from a on-premise server to an EC2? Speaking as someone who worked 10 years in banks, a company such as bank may want to move its website and online banking application from on-premises to the EC2 instance on AWS for several reasons. As the bank grows, it can easily scale its EC2 instances up or down depending on demand. The bank can also use AWS services, such as Elastic Load Balancing and Auto Scaling, to automatically balance traffic and ensure high availability and performance. So in summary here are some possible benefits more in-depth:

Scalability:

● On-premises infrastructure can be difficult and expensive to scale. By moving its app toan EC2 instance on AWS, the bank can take advantage of the cloud’s scalability. EC2 instances can be easily scaled up or down depending on demand, allowing the bank to handle spikes in traffic without having to invest in additional hardware or infrastructure. This means that the bank can quickly respond to changes in customer demand and avoid the risk of underprovisioning or overprovisioning resources.

Reliability:

● AWS offers a highly reliable infrastructure with multiple availability zones and automatic failover capabilities. By moving its app to AWS, the bank can ensure that it is highly available and that customers can access it at all times. AWS’s infrastructure is designed to be fault-tolerant, which means that the bank’s app can continue to function even if there is an issue with a server or availability zone. This helps to minimize downtime and ensure that customers can access the bank’s services whenever they need them.

Security:

● Banks are a prime target for cyber attacks, and securing customer data is paramount. AWS provides robust security features, such as encryption, access controls, and network security, that can help the bank improve the security of its app and customer data. AWS also offers compliance certifications for various industry standards, which can help the bank meet regulatory requirements and protect against legal and reputational risks.

Cost savings:

● Maintaining on-premises infrastructure can be expensive, requiring significant upfront investment in hardware, software, and personnel. By using AWS, the bank can take advantage of pay-as-you-go pricing and avoid the upfront costs of purchasing and maintaining hardware. This means the bank can focus its resources on improving its app and services rather than managing and maintaining infrastructure. Additionally, AWS offers cost optimization tools and resources to help the bank optimize its infrastructure and reduce costs over time.

Flexibility:

● The banking industry is constantly evolving, and banks need to be able to adapt quickly to changing market conditions and customer needs. Moving its app to AWS gives the bank the flexibility to quickly spin up new instances, test new features, and experiment with different configurations. This can help the bank innovate and respond more quickly to changing market conditions, giving it a competitive advantage.

These are just a few reasons why a company may want to switch to the power of the cloud. Until next time, Bye!!

--

--