How to Launch an EC2(Linux) Instance from a Custom AMI?

As the business grows, we keep managing our cloud infrastructure in agile way. Having said that, for increase in business demand, We keep adding more servers with similar configuration in the existing application architecture. So we need a copy of existing EC2 application’s software & configuration bundled package ready from which we could spin off a new server with similar software and configuration and We don’t need to install the software again when the server is up and running.

There are different ways where you could run your application on AWS EC2 instance. You could install the application and further manage your infrastructure using configuration management tool like CHEF,Puppet, Ansible etc. Also you could provide the user data information (shell script) during EC2 launch process. The other option is to create an Amazon Machine Image (AMI) of your existing EC2 application and launch N number of instances with similar configuration and application from that AMI.

In this blog, we would discuss below

  • Install an application in an existing EC2
  • Create an AMI of the EC2
  • Launch a new EC2 from AMI

Install an application in an existing EC2

Here, we would install Apache(Httpd) on the RHEL EC2. Also we could create some folder structure on it. The below demonstration is explained using ROOT

1. Login to your EC2 & verify the Apache package

[root@host ~]# httpd -v
[root@host ~]# service httpd status

If the Apache is not present in the server, then it would print Unit httpd.service could not be found.

2. Install Apache & Start the Service

The below command would start the Apache service and configure the Apache service in the Server. SO that during each boot, the service would be up automatically

[root@host ~]# yum install httpd -y
[root@host ~]# service httpd start
[root@host ~]# service httpd status
[root@host ~]# chkconfig httpd on
[root@host ~]# systemctl enable httpd.service

3. Verify the Apache in Web browser

Before you verify it in browser, make sure port HTTP 80 is opened in your inbound security group of your EC2 instance

Type http://<your-public-ip/public-DNS/Elastic-IP&gt;:80 in your browser. The Web server would look like below

4. Create a directory & file inside it

5. Create an AMI of the EC2

Select the EC2–> Go To Action –> Image–> Create Image

Once you initiated the Create Image action, the Image would be created and the status would be charged to Pending state. Once the AMI is in available state, then proceed for next step

6. Launch a new EC2 Instance

Select the AMI and then click on Action to launch a new Instance. Now follow the same process to launch a new EC2 instance. Provide a Meaningful Tag Name and select the same/required Security group & Key Pair to launch the Instance

7. Login to New EC2 and verify the Apache

[root@host ~]# service httpd status

It should return like below

httpd.service – The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2019-10-24 06:07:43 UTC; 21s ago
Docs: man:httpd.service(8)
Main PID: 1386 (httpd)
Status: “Running, listening on: port 80”
Tasks: 213 (limit: 4998)
Memory: 29.6M
CGroup: /system.slice/httpd.service
├─1386 /usr/sbin/httpd -DFOREGROUND
├─1387 /usr/sbin/httpd -DFOREGROUND
├─1388 /usr/sbin/httpd -DFOREGROUND
├─1389 /usr/sbin/httpd -DFOREGROUND
└─1390 /usr/sbin/httpd -DFOREGROUND

Oct 24 06:07:42 host systemd[1]: Starting The Apache HTTP Server…
Oct 24 06:07:43 host httpd[1386]: AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using fe80::1387:89ff:fe65:8>
Oct 24 06:07:43 host httpd[1386]: Server configured, listening on: port 80
Oct 24 06:07:43 host systemd[1]: Started The Apache HTTP Server.

8. verify the Apache in your Web Server

Once you verified, terminate the EC2 and then deregister the AMI. AMI can not be deregistered unless the EC2 is terminated.

I hope this blog helps. Please comment below for any questions or concerns related to this blog.

Leave a Reply

%d bloggers like this: