One way to speed up the deployment process is to pre-bake AMIs. Pre-baked AMIs can be used with Auto Scaling in CloudFormation. These pre-baked AMIs can have a significant portion of the application already installed and ready to go. Even if we can’t include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.
For this guide, we will install an Apache web server on an AMI.
The first thing you need to do is log in to the AWS Console and navigate to the EC2 Dashboard.
1. Click Launch Instance and Select and Amazon Linux AMI. Leave the t2.micro option selected for the instance type, then click Next: Configure Instance Details.
2. Enable Auto-assign public IP, then click Next: Add Storage; then, click to Add Tags.
3. For the “Name” key add a Value, then click Next: Configure Security Group.
4. Leave Create a new security group selected, and click Add Rule. Select HTTP from the dropdown menu for Type. Click Add Rule again and select HTTPS. Since we are creating a web app, we need to make sure HTTP traffic can arrive and reach the Elastic Load Balancer and then be distributed to our EC2 instances; so, we also need to confirm port 80 is open. Now click Review and Launch.
5. Review the configurations, then click Launch. Click Create a new key pair, and give the key pair a name. Download the key pair, then click Launch Instances.
6. From the EC2 Dashboard Click View Instances. Once the Instance Status changes to running, select the Connect button. Copy the example provided in the popup.
You will use the instance you just provisioned to create an AMI. Think of an AMI as a snapshot of the instance in its current state. The AMI, and all new instances spun up using the AMI, will contain any modifications, installed packages, and configuration changes on this instance.
Open your terminal and navigate to the directory in which the .pem key file was saved.
1.Update the permissions:
[user@workstation] chmod 400 guidekey.pem
2.SSH into your instance:
[user@workstation] ssh -i “guidekey.pem” ec2-user@ip
3.Update your instance’s packages:
[ec2-user@ip-10-0-0-15 ~]$ sudo yum update
4.You will install an Apache web server on the instance, which is found under the httpd package. You could use any other web server, such as Nginx.
[ec2-user@ip-10-0-0-15 ~]$ sudo yum install httpd
5.Now start the web server:
[ec2-user@ip-10-0-0-15 ~]$ sudo service httpd start
Return to the EC2 Dashboard and copy the instance’s public IP address. Paste it into your web browser and you should see Apache’s default installation page.
6. One final step is needed to make sure the Apache web server will launch after reboot:
[ec2-user@ip-10-0-0-15 ~]$ sudo chkconfig httpd on
Your instance is now prepared to be used as a base for your AMI! It is best practice when creating AMIs to be cautious not to include API credentials baked into the image and instead give the instances an appropriate IAM role.
Return to the EC2 Dashboard. Select the Actions button above to select the Image menu, then click Create Image. Give the Image a name and description then click Create image.
Once the image has been created, return to the Instances page and Terminate the current instance. You can now use your AMI image to create instances.
Click Create Instance, and select My AMIs from the left menu. Now you can select the AMI you just created!