Cloud is all about agility. Quickly creating new servers of various sizes and deploying applications on them is one of them. Let’s take the example of Netflix, hosted on AWS. Whenever there is a popular show or a movie, Netflix adds more and more EC2s using autoscaling to meet customer demand. Depending on the number of users trying to access Netflix service, the autoscaling feature can automatically add or delete EC2 instances. Let us see how to launch an EC2 instance from a custom AMI.
You may also enjoy: Tutorial: Deploying an API on EC2 from AWS
What Are the Different Ways of Getting the Application onto The EC2 Instance?
How is the application installed on EC2 automatically? There are multiple ways to have the application along with the settings on the EC2 instance as discussed below.
- Using configuration management tools like Puppet and Chef to manage the application lifecycle. The application can be installed, upgraded and rolled back on thousands of machines using these configuration management tools.
- By passing the user data to the EC2 instance. The user data can be a shell script to install the application and will be executed when the EC2 starts.
The last option is to use an EC2 AMI (Amazon Machine Image), the AMI has all the information like the operating system, attached EBS disks, applications, and the corresponding settings. The AMI is all that is required to start an EC2 instance. Compared to the above two approaches, using the AMI is the fastest way to start an EC2 instance as AMI already has all the details to start the EC2 instance. This tutorial goes through the sequence of steps to create an AMI.
Creating a Custom AMI
AWS provides us with a set of AMIs for both Windows and Linux. Based on the requirement, it’s also possible to create customized AMI with additional software and configuration settings. Here is the high-level sequence of steps to create an AMI.
Step 1: Launch an EC2 instance from an existing AMI and login to it.
Step 2: Install the application and make the appropriate configuration changes.
Step 3: Create a new AMI.
Step 4: Launch additional EC2 instances by using the AMI created in Step 3.
Here are steps in detail:
Step 1: Launching EC2 instance
Use one of the existing AWS-provided AMIs (Windows or Linux) to launch an EC2 instance and log in to the instance as mentioned in the Edureka tutorial for EC2.
Step 2: Installing the Application on EC2
Once logged into the EC2 instance, install any application according to your requirements. These commands are for installing Apache Tomcat on Ubuntu EC2 instance. Apache Tomcat can be used to build dynamic web pages using JSP and Servlets. Likewise, any other software can be installed.
The Tomcat installation can be verified by going to (ec2-ip:8080) URL in the browser, the Tomcat home page should be shown as below. Make sure to replace the ec2-ip with the appropriate public IP of the EC2 instance. The port 8080 should be opened in the inbound rules of the Security Group along with port 22 as shown in the below “inbound rules” of the Security Group. Port 22 is used for SSH access and port 8080 is used for accessing Tomcat.
Step 3: Creating Custom AMI
Select the EC2 instance and go to “Actions -> Image -> Create Image.”
Next, specify the image name and description and click on “Create Image.” Note that before creating the image the EC2 instance is stopped, the AMI created and rebooted back. This is to ensure that the image created is in a consistent state. The EC2 is rebooted, so any Putty or other sessions to the EC2 will be terminated.
Click on the AMI tab in the left pane. Initially, the AMI would be in a “pending” state and then it would change to the “available” status. Depending on the size of the EC2 instance, the AMI creation might take some time. Note that the AMI has a visibility of Private by default and is only accessible to the user who created it. The AMI can be made public or accessible to a few users by going to “Actions -> Manage Image Permissions.”
Step 4: Creating EC2 from the New AMI
In the EC2 Management Console click on “Launch Instance,” then on “My AMI” where the private AMI created in Step 3 should be visible. Select the AMI and follow the EC2 creation process as usual. Once the EC2 instance has been created, get the public IP address of the EC2 and access the Tomcat homepage by going to (ec2-ip:8080) URL in the browser. This time there is no need to login to the EC2 instance and install Tomcat, as the AMI created in Step 3 has already got the Tomcat already installed in it.
Once the EC2 had been created, make sure to terminate the EC2 and deregister the AMI in the same order. If the EC2 instance is running, then the corresponding AMI cannot be deregistered. The AMI takes storage space and if it is not deregistered/deleted, there is a cost associated with it.
from DZone Cloud Zone