Auto Scale EC2 Instances using Lambda function/CloudWatch Metrics

0

Hello, I have a task that I have to do on my work and I would really appreciate if anyone can assist me on this.

The assignment that I have to implement, is to create an 'Auto scaling group' using a value that is returned from a 'lambda function'.

After an exhausting research, I have found that i have to Create an 'AutoSG', then select 'Predictive policy' and then choosing 'Custom cloudWatch Metric'. From their somehow i have to select the metric that came from lambda function.

My problem their is how to construct 'JSON-formatted payload that contains the custom metric definition on Predictive Policy.' Moreover, do I need to add some additional code in my lambda function to send the returned value to 'Auto scaling Group'?

Forgot to mention that my lambda function will executing a query on AWS RDS(mySQL) and return that value.

Relative resources: https://docs.aws.amazon.com/autoscaling/ec2/userguide/predictive-scaling-customized-metric-specification.html https://aws.amazon.com/blogs/containers/autoscaling-amazon-ecs-services-based-on-custom-cloudwatch-and-prometheus-metrics/

  • There's some conflicting information here. Are you creating a new ASG (AutoScaling Group) every time the Lambda runs? Are you creating a new scaling policy? Or is the Lambda function uploading metrics to CloudWatch, and you need a scaling policy which is reacting to those metrics?

3 Answers
0

Perhaps you could use AWS CLI to execute the creation. https://docs.aws.amazon.com/cli/latest/reference/autoscaling/index.html

profile picture
answered a year ago
0

What I want is the Lambda function uploading metrics to CloudWatch, and then a scaling policy which is reacting to those metrics. Particularly, I was clarifying my problem according to Shahad_C answer.

answered a year ago
0

Based on your followup comment, it sounds like you might be mentally combining a few different things which are separate processes

The lambda function is going to push metrics to CloudWatch, creating a Custom Metric. When pushing the values, you'll set the Namespace, MetricName, and optionally set a unit and dimension(s). That's one isolated process

Now in the AutoScaling Group (ASG), you can create one or more scaling policies which can react to those metrics, with a CloudWatch alarm acting as the glue to tie the two together for a dynamic scaling policy. You mentioned a Predictive Scaling Policy, but keep in mind those are running once a day, and then create capacity predictions for entire hours. So while you can use one if your load has a cyclical pattern, you should also use a dynamic scaling policy either way to react to real time changes in the metric.

The simplest Dynamic Scaling policy to setup is Target Tracking. Make sure the metric meets the Target Tracking requirements listed in that doc (changes proportionally to the load and instances, liner, etc). For both Target Tracking and Predictive Scaling, you'll define the Custom Metric Specification based on the Namespace, MetricName, and (if set when uploading the metric) a unit and dimension(s)

Predictive Scaling needs 2 metrics defined, the load metric (something representing the total load on the entire ASG), and the scaling metric (something representing the change as scaling happens). For example, if scaling on CPU, then the SUM of the CPU metric would be the Load metric (totaling the CPU usage of every instance int the ASG); and the AVERAGE of the CPU metric would be the Scaling metric (as more instances are added with a constant load, average CPU would go down as the load is spread between more instances)

AWS
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions