PART -2 AWS Autoscaling using Dynamic scaling policies based on CPU and memory utilization simultaneously

Yes itโ€™s possible to do autoscaling simultaneously based on CPU Utilisation and memory Utilisation

Q. while using both CPU and memory in one autoscaling group is it a good practice or not?

Ans. It is good practice, the one downside is that when you scale down it will be slower. So to scale up when either CPU or Memory fires it will scale up. When it comes to scaling down it will need both memory and CPU to drop below the threshold in order to scale in.

The policy type Iโ€™m using in this blog is Target-tracking
Target-tracking โ€” This policy will increase or decrease the autoscaling groupโ€™s current desired capacity based on the target value for the specific metric. This policy maintains the capacity to match specified target metrics like CPU or memory utilization. Letโ€™s assume that you have set 60% utilization of your autoscaling group, the target-tracking policy will add or remove EC2 instances to meet the specified utilization.

FLOW OF AUTOSCALING

Dynamic scaling with Target tracking policy

It includes the following steps :

  1. Install the Cloudwatch agent as follows:
sudo yum install amazon-cloudwatch-agent -y

2. Create config.json as follows to be used by the CloudWatch Agent under the directory :

3. Create an IAM Role for each server to run the CloudWatch agent as follows and associate it to the servers: ++++++++++++++++++++++++++++

1. Sign in to the AWS Management Console and open the IAM console.

2. In the navigation pane, choose Roles, and then choose to Create role.

3. Under Select type of trusted entity, choose AWS service.

4. Under Common use cases, choose EC2 and Next: Permissions.

5. In the list of policies, select the check box next to CloudWatchAgentServerPolicy and CloudWatchLogsFullAccess. If necessary, use the search box to find the policy.

6. Choose Next: Tags.

7. (Optional) Add one or more tag-key value pairs to organize, track, or control access for this role, and then choose Next: Review.

8. For Role name, enter a name for your new role (for example, CloudWatchAgentServerRole).

9. (Optional) For Role description, enter a description.

10. Confirm that CloudWatchAgentServerPolicy appears next to Policies.

11. Choose to Create a role.

++++++++++++++++++++++++++++

4. Start the CloudWatch agent using the config file created under the directory /opt/aws/amazon-cloudwatch-agent/bin/ in Step 2 as follows:

++++++++++++++++++++++++++++

cd /opt/aws/amazon-cloudwatch-agent/bin/sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:config.json

++++++++++++++++++++++++++++

Validate the logs

tail -f /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log

++++++++++++++++++++++++++++
5. Create a target tracking Autoscaling policy using the MemoryUtilization metric being populated by the CloudWatch agent as follows :

++++++++++++++++++++++++++++

  • Create a JSON configuration file that uses a target value of 40% on average of the MemoryUtilization metric :

โ€” Run the put-scaling-policy CLI command using the config.json:

 aws autoscaling put-scaling-policy โ€” policy-name <Memory40-target-tracking-scaling-policy> โ€” auto-scaling-group-name <Your ASG Name> โ€” policy-type TargetTrackingScaling โ€” target-tracking-configuration file://config.json 
autoscaling policy for memory and cpu

Once the metrics and policy have been created as above, we could observe that the AutoScaling was working as expected. Further, as you wanted all the new instances to come up using a similar setup, we created an AMI out of this configured instance and updated it into the launch template. All the newly launched instances already came up with the CloudWatch agent configured.

--

--

๐’๐š๐ค๐ž๐ญ ๐‰๐š๐ข๐ง
๐’๐š๐ค๐ž๐ญ ๐‰๐š๐ข๐ง

Written by ๐’๐š๐ค๐ž๐ญ ๐‰๐š๐ข๐ง

๐ƒ๐ž๐ฏ๐Ž๐ฉ๐ฌ/๐’๐‘๐„/๐‚๐ฅ๐จ๐ฎ๐ /๐ˆ๐ง๐Ÿ๐ซ๐š๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐ฎ๐ซ๐ž /๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ

No responses yet