In our previous AWS networking blog, we already created Elastic load balancer (ELB), now we want to automate the creation of Target instance using Auto Scaling Group (ASG).
Creating ASG has 2 phases. First, you need to create Launch Template where we define EC2 instance as well as Security Groups. The 2nd phase is where you create Auto Scaling Group as associate the the Launch Template you create. Below is the step by guide in creating both phases.
Creating Launch Template
- On the left pane, Click Lunch Templates under Instances
- On the main pane, click Create launch template
- Assign a launch template name and add description.
- Enable Provide guidance to help me set up a template that I can use with EC2 Auto Scaling
- Under Application and OS Image, click Browse more AMIs. This will take you to a page where you can select which image/OS that will be installed in your EC2 instances.
- Select radio button next to the 1st option Amazon Linux 2 AMI (HVM) – Kernel 5.10 for simplicity sake. Click Select
- Under Instance type, select t3.micro
- Under Key pair, select Don’t include in launch template
- Under Security Group, select the same Security Group you created or selected in your ELB (Refer to the ELB Blog Post).
- On the Advanced Settings, under IAM Instance Profile, select the existing Instance Profile.
- Under User data, add a custom shell script. This will run when the instance starts and the script installs the application required dependencies and launch the application.
- Finally, click Create launch template located on the buttom right of the page.
- Click View launch template to verify if template has been created.
Creating Auto Scaling Group (ASG)
- On the left page, click Auto Scaling Group under Auto Scaling
- Click Create Auto Scaling group located on the upper right of the page.
- Assign preferred Auto scaling group name.
- Select the Lauch template name you created from previous task.
- Verify if details matches such as AMI ID, Security Group, Instance type etc.
- Click Next, this will take you to the Network configuration page of your Auto Scaling Group.
- Select the correct VPC and Availability Zone (Refer to the ELB Blog Post) . Click Next
- Select Attach to an existing load balancer and select the target group name you created (Refer to the ELB Blog Post).
- Under Health checks, select ELB to inherit the health check settings of your ELB. Click Next.
- Under Group size, Enter value of 2 for Desired, 2 for Minimum Capacity and 4 for Maximum Capacity.
- Under Scaling Policies, Select Average CPU Utilization, Enter a value of 40 for Target Value. Click Next.
- We will now activate Notifications by clicking Add Notifications. Click Create Topic.
- Assign a topic name and make sure to add recipient email address as alarms/notifications will be sent to the email address you entered.
- Click Next, click Next as we will not be configuring Tags.
- Review Auto Scaling Group configuration and click Create Auto Scaling Group.
Click below to access the video version and consider subscribing.