PreBake AWS AMIs for Auto Scaling

Hands-On Lab

 

Photo of Craig Arcuri

Craig Arcuri

AWS Training Architect II in Content

Length

00:30:00

Difficulty

Intermediate

Welcome to this hands-on AWS Learning Activity in which you will learn how to create Pre-Baked AMIs for the purpose of simplifying EC2 instance provisioning in an Auto Scaling environment. When provisioning EC2 instances it is often necessary to bootstrap these instances to configure software on the instances at launch time. But this often can be a time consuming task and delays the instance being in a ready state. Pre-Baking AMIs with already installed software is a technique to ease the burden of bootstrapping and to decrease the time it takes to launch and provision EC2 instances. Pre-baking can be especially helpful in deploying EC2 instances in Auto Scaling groups. The Pre-Baked AMI can be part of the launch configuration for the Auto Scaling group.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Pre-Bake AWS AMIs for Auto Scaling

Introduction

When provisioning EC2 instances, it is often necessary to bootstrap the instances and configure software on the instances at launch time. But this can be a time-consuming process that delays the time to instance readiness. Pre-baking AMIs with already installed software is a technique that can ease the burden of bootstrapping and decrease the time it takes to launch and provision EC2 instances.

In this lab, we will learn how to create pre-baked AMIs for the purpose of simplifying EC2 instance provisioning in an Auto Scaling environment.

Log in to the AWS Management Console using the credentials provided on the lab instructions page.

Create an EC2 Instance

  1. From the AWS Management Console dashboard, navigate to the EC2 service.
  2. Click Launch Instance.
  3. Select Amazon Linux AMI.
  4. Select t2.micro, and click Next: Configure Instance Details.
  5. For Auto-assign Public IP, select Enable.
  6. For IAM role, select the provided CWatchInstanceProfile option, then click Next: Add Storage.
  7. Click Next: Add Tags.
  8. Click Add Tag.
  9. For Key, type "name", and for Value, type "web-build-app".
  10. Click Next: Configure Security Group.
  11. Leave the Create a new security group option selected, and click Add Rule.
  12. Select HTTP from the Type dropdown.
  13. Click Add Rule again, and select HTTPS from the Type dropdown.
  14. Click Review and Launch, then Launch.
  15. Select the Create a new key pair option, and type "PreBakeAMI" for the Key pair name. Click Download Key Pair.
  16. Click Launch Instances.
  17. Click View Instances.

Log In to the Instance

  1. When the instance is in a running state, select it, and click Connect.
  2. Copy the command under Example in the connection menu.
  3. Open your terminal application.
  4. Switch to your downloads directory.
    cd downloads
  5. From your downloads directory, change permissions on the key pair:
    chmod 400 PreBakeAMI.pem
  6. Now run the ssh command we copied earlier to log in to the instance. It will look something like this (make sure you replace the <PUBLIC_IP_ADDRESS> parameter):
    ssh -i "PreBakeAMI.pem" ec2-user@<PUBLIC IP ADDRESS>.compute-1.amazonaws.com
  7. Type yes at the prompt.
  8. Update the instance.
    sudo yum update
  9. Type y at the prompt.
  10. Install the Apache web server.
    sudo yum install httpd
  11. Type y at the prompt.
  12. Then start the web server.
    sudo service httpd start
  13. Ensure the service starts after reboots.
    sudo chkconfig httpd on
  14. Then verify that everything installed correctly.
    chkconfig --list httpd
  15. Go back to the connection menu in your AWS Management Console browser tab, and copy the public DNS name of our instance.
  16. Open a new browser tab, and paste the DNS name into the address bar. Press Enter to load the page.

Create an AMI from the Web Server

  1. Go back to the EC2 service, and close out of the connection menu.
  2. On the Instances page, click Actions, Image, then Create Image.
  3. Type "web-build" in the Image name and Image description fields, then click Create Image.
  4. Click View pending image in the success message menu.

Create a Launch Configuration

  1. When the status of the image is available, click Launch Configurations in the left sidebar.
  2. Click Create Auto Scaling group.
  3. Click Create launch configuration.
  4. Click My AMIs in the left sidebar, and select the web-build AMI we just created.
  5. Leave t2.micro selected, and click Next: Configure details.
  6. For Name, type "first-launch-config".
  7. For Iam role, select the option that contains CWatchInstanceProfile.
  8. Click the arrow next to Advanced Details. For IP Address Type, select Assign a public IP address to any instances.
  9. Click Skip Storage.
  10. Click Add Rule and set the Type to HTTP.
  11. Click Add Rule again and set the Type to HTTPS.
  12. Click Review.
  13. Click Create launch configuration.
  14. From the dropdown, select Create a new key pair. Name the key pair "Launch", and click Download Key Pair.
  15. Click Create launch configuration.

Create an Auto Scaling Group

  1. On the Create Auto Scaling Group page, configure the settings as follows:
    • Group name: AMILab
    • Group size: 4
    • Network: vpc-dc88bfa7 (10.0.0.0/16)
    • Subnet:
      • subnet-bac43e94(10.0.0.0/24) | us-east-1a
      • subnet-dqqa969b(10.0.1.0/24) | us-east-1b
  2. Click Next: Configure scaling policies.
  3. Select the Use scaling policies to adjust the capacity of this group option.
  4. Set it to scale between 4 and 8 instances.
  5. Click Scale the Auto Scaling group using step or simple scaling policies.
  6. Under the Increase Group Size header, click Add new alarm.
  7. Deselect the Send a notification to: option.
  8. For Is:, set it to >= 70 Percent.
  9. Click Create Alarm.
  10. For Take the action:, set it to Add 1 instances.
  11. Under the Decrease Group Size header, click Add new alarm.
  12. Deselect the Send a notification to: option.
  13. For Is:, set it to <= 40 Percent.
  14. Click Create Alarm.
  15. Click Review.
  16. Click Create Auto Scaling group.
  17. Click View your Auto Scaling groups.
  18. Click EC2 Dashboard in the left sidebar, and click 5 Running Instances.
  19. Select one of the instances, click Connect, and copy the public DNS.
  20. Paste the public DNS in a new browser tab, and you should be directed to our test web page.

Conclusion

Congratulations, you've successfully completed this lab!