AWS Auto Scaling Lifecycle Hooks

Hands-On Lab

 

Photo of Craig Arcuri

Craig Arcuri

AWS Training Architect II in Content

Length

01:00:00

Difficulty

Intermediate

Welcome to this hands-on AWS Learning Activity in which you will learn how to manage the deployment of EC2 instances in an Auto Scaling Group using Lifecycle Hooks. When deploying instances in an Auto Scaling Group it is often desired to bootstrap the instances with scripts that will deploy software onto the instance at creation time. But problems can arise if the instance spins up and comes to a ready state before software deployment has completed. When an instance spins up its lifecycle progresses through a lifecycle until it reaches an inservice state. In this Learning Activity we will use Lifecycle Hooks to hold the EC2 instance in a pending:Wait state to ensure that a Bash script, which will be used to deploy web application software, has completed before allowing the EC2 instance to progress to the inservice state.

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.

AWS Auto Scaling Lifecycle Hooks

Introduction

In this hands-on lab, we will learn how to manage the deployment of EC2 instances in an Auto Scaling Group using lifecycle hooks.

Solution

Log in to the AWS environment using the cloud_user credentials provided on the lab instructions page.

Once you're logged in to the AWS account, make sure you are using us-east-1 (N. Virginia) as the selected region.

Note: The login credentials will be different for the final task.

Create One Auto Scaling Launch Configuration

  1. Click EC2 on the AWS Management Console Dashboard.
  2. On the left-hand side task pane, scroll down and click Launch Configurations.
  3. Click Create Auto Scaling Group.
  4. Click Create Launch Configuration.
  5. Click the Select button for Amazon Linux AMI 2 (HVM), SSD Volume Type.
  6. Ensure that the radio button for t2.micro is selected.
  7. Name the Launch Configuration lifecycle.
  8. For the IAM Role choose the Auto Scaling Role profile.
  9. Click the arrow of Advanced Details.
  10. Copy/paste the Bash script into the User Data box as text from Choose AMI > Choose Instance Type > Configure details > Create Launch Configuration section of the EC2 Management Console.
  11. For IP Address Type, click the radio button for Assign a public IP address to every instance.
  12. Click Skip Storage.
  13. Click the radio button for Select an existing security group.
  14. Select the button for the security group that says Enable access to the EC2 host in the description.
  15. Click Review.
  16. Review the details of your Launch Configuration, and then if everything is correct, click Create Launch Configuration.
  17. From the dropdown select Create a new key pair, name the key pair lifecycle.
  18. Click Download Key Pair.
  19. Click Create Launch Configuration.

Create Auto Scaling Group

  1. On the Launch configuration creation status page, click Create an Auto Scaling group using this launch configuration.

    Note: We can disregard the warnings at the top of the Create Auto Scaling Group page.

  2. For the Group name enter devopsASG.

  3. Change the Group size to 0.

  4. Select the VPC for the Network.

  5. Select the subnet, and leave the defaults for the Advanced Details section.

  6. Click Next: Configure scaling policies.

  7. Ensure that the radio button for Keep this group at its initial size is selected.

  8. Click Review.

  9. Click Create Auto Scaling Group.

  10. Click View your Auto Scaling groups.

    Note: We can disregard the error or load balancer messages.

Create Lifecycle Hook

  1. Run the following command:

     ssh linuxacademy@ the public IP address here

    Note: Do not use the login credentials provided for EC2 on the lab page. Use the credentials on the video to SSH into the instance.

  2. Type y for Yes when prompted to continue connecting.

  3. Enter the password.

    Note: Disregard a warning message for SNIMissingWarning.

  4. Use the following for configuration:

    aws configure
  5. Accept the [None] default for AWS Access Key ID and AWS Secret Access Key.

  6. For the Default region name [None]: type us-east-1.

  7. Accept the [None] default for output format.

  8. From the AWS CLI execute the command:

    aws autoscaling put-lifecycle-hook -- lifecycle-hook-name lifecycle --auto-scaling-group-name devopsASG -- lifecycle-transition autoscaling: EC2_INSTANCE_LAUNCHING
  9. Describe the lifecycle-hooks with the following command:

    aws autoscaling describe-lifecycle-hooks --auto-scaling-group name _devopsASG_

    Note: We should see the GlobalTimeout, Heartbeat Timeout, AutoScalingGroupName, Lifecycle Hookname, DefaultResult, and LifecycleTransition`.

  10. Navigate back to the EC2 Management Console Auto Scaling Group: devopsASG section Details tab, and then click Edit.

  11. Enter 1 for the Desired, Min, and Max fields.

  12. Click the Save button.

  13. Click the Instances tab.

  14. Click EC2 Dashboard on the left-hand side navigation menu.

  15. Under the Resources section, click Running instances.

  16. Click the radio button to select the instance ID in a running pending state without a name.

  17. Click the Connect button at the top of the page.

  18. Copy/paste the public DNS string in a web browser.

    Note: We should see our message or "Welcome to your application".

  19. We can manually end the pending wait state from the CLI with the following format:

    aws autoscaling complete-lifecycle-action --lifecycle-hook-name lifecycle -auto-scaling-group-name devopsASG -- lifecycle-action-result CONTINUE -- instance id i -xxxxxxxxxxxxxxxx

Conclusion

Congratulations - you've completed this hands-on lab!