An Illusion
An Illusion

Reputation: 777

AWS Is it possible to automatically terminate and recreate new instances for an auto scaling group periodically?

We have an AWS scaling group that has 10-20 servers behind a load balancer. After running for a couple of weeks some these server go bad. We have no idea why the servers go bad and it will take some time for us to get to a stage where we can debug this issue.

In the interim is there a way to tell AWS to terminate all the instances in the scaling group in a controlled fashion (one by one) until all the instances are replaced by new ones every week or so?

Upvotes: 2

Views: 1826

Answers (4)

Mark Mullen
Mark Mullen

Reputation: 11

As of Nov 20, 2019, EC2 AutoScaling supports Max Instance Lifetime: https://aws.amazon.com/about-aws/whats-new/2019/11/amazon-ec2-auto-scaling-supports-max-instance-lifetime/

From:

The maximum instance lifetime specifies the maximum amount of time (in seconds) that an instance can be in service. The maximum duration applies to all current and future instances in the group. As an instance approaches its maximum duration, it is terminated and replaced, and cannot be used again.

When configuring the maximum instance lifetime for your Auto Scaling group, you must specify a value of at least 86,400 seconds (1 day). To clear a previously set value, specify a new value of 0.

Upvotes: 1

strongjz
strongjz

Reputation: 4491

There are two ways to achieve what you are looking for, Scheduled Auto Scaling Actions or take them one of the instances out of the ASG.

Scheduled Scaling

Scaling based on a schedule allows you to scale your application in response to predictable load changes. For example, every week the traffic to your web application starts to increase on Wednesday, remains high on Thursday, and starts to decrease on Friday. You can plan your scaling activities based on the predictable traffic patterns of your web application.

https://docs.aws.amazon.com/autoscaling/latest/userguide/schedule_time.html

You most likely want this.

Auto Scaling enables you to put an instance that is in the InService state into the Standby state, update or troubleshoot the instance, and then return the instance to service. Instances that are on standby are still part of the Auto Scaling group, but they do not actively handle application traffic.

https://docs.aws.amazon.com/autoscaling/latest/userguide/as-enter-exit-standby.html

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269191

There is no function in Auto Scaling to tell it to automatically terminate and replace instances. However, you could script such functionality.

Assumptions:

  • Terminate instances that are older than a certain number of hours old
  • Do them one-at-a-time to avoid impacting available capacity
  • You wish to replace them immediately

A suitable script would do the following:

  • Loop through all instances in a given Auto-Scaling Group using describe-auto-scaling-instances
  • If the instance belongs to the desired Auto Scaling group, retrieve its launch time via describe-instances
  • If the instance is older than the desired number of hours, terminate it using terminate-instance-in-auto-scaling-group with --no-should-decrement-desired-capacity so that it is automatically replaced
  • Then, wait a few minutes to allow it to be replaced and continue the loop

The script could be created by using the AWS Command-Line Interface (CLI) or a programming language such as Python.

Alternatively, you could program the instances to self-destruct after a given period of time (eg 72 hours) by simply calling the operating system to shut-down the instance. This would cause auto-scaling to terminate the instance and replace it.

Upvotes: 0

Piyush Patil
Piyush Patil

Reputation: 14523

You can achieve this very effectively using Data Pipeline.

This is the developer guide for How do I stop and start Amazon EC2 Instances at scheduled intervals with AWS Data Pipeline?

Upvotes: 1

Related Questions