Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you create your own EC2 autoscaling triggers?

When using an auto-scaling group on EC2 the documentation says you can trigger new servers to come up based on cloudwatch metrics. Is it possible to launch new instances on your own?

For example an app has an internal queue of items and once that queue hits a threshold it sends a notice to EC2 to add more servers to the group.

Is that possible?

like image 769
James Avatar asked Jun 06 '12 18:06

James


1 Answers

For the benefit of users visiting this answer in future, here is a more elaborate explanation:

EC2 allows you to set/change the capacity of your auto scaling group manually, based on schedule or based on demand, as outlined in the docs .

However the term manual scaling can be somewhat misleading, because like almost every aspect of AWS, everything that you can do manually can be scripted either through the CLI or through SDKs.

I case of EC2 auto scaling group the capacity is configurable and it can be changed dynamically in the runtime - the change in capacity will cause instances to be added or removed automatically.

So in this case a solution would be to detect lifecycle events specific to your application in your application code, and in response to those events use the relevant AWS SDK to change the capacity of your autoscaling group.

In ruby this can be done as follows (Examples taken from AWS API documentation):

autoscaling = Aws::AutoScaling::Client.new(
  region: region_name,
  credentials: credentials
)

resp = autoscaling.set_desired_capacity(
  # required
  auto_scaling_group_name: "ResourceName",
  # required
  desired_capacity: 1,
  honor_cooldown: true,
)
like image 163
lorefnon Avatar answered Sep 23 '22 14:09

lorefnon