July Release Confetti
150+ New Hands-on Training in Aws, Linux, Azure
Red Hat, Devops, and More
Learn More

Prebake AMI

Introduction 

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.


SETUP

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.


user_72928_5953c05a2f510.png_800.jpg




INSTALLATION

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 -iguidekey.pemec2-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.



user_72928_5953c0cce4227.png_800.jpg

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


CONCLUSION

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!





  • post-author-pic
    Kevin J
    06-28-2017

    Thank you Rachel. 

  • post-author-pic
    Jaleel Ahmed M
    07-18-2017

     As an AWS Engineer, I have pretty much opted the Pre-bake method with slight differences.
    First I used Ansible /CloudFormation to launch and EC2 uninstalled what is not required and installed only the components that is required to run the specific applications and then customized the application directory architecture to a Generic naming convention and then store the actual deployment scripts in S3 buckets to kick off using the USERDATA and those scripts will talk to respective deployment buckets after identifying which VPC the EC2 was launched and what tags used.

    I was able to leverage the above build process using Autoscaling and providing these ASG's  with the Common Base Build(Pre-baked/customized Image) reducing the whole setup environment to a substantial level.

    Adding my idea to forum just to help others and help build a stronger and better deployment process with AWS or any infra-services.

  • post-author-pic
    Michael N
    11-30-2017

    Please also include AWS CLI example to Create Image.  Thank you.

Looking For Team Training?

Learn More