Replacing Auto Scaling instances based on maximum instance lifetime - Amazon EC2 Auto Scaling

Replacing Auto Scaling instances based on maximum instance lifetime

The maximum instance lifetime specifies the maximum amount of time (in seconds) that an instance can be in service before it is terminated and replaced. This feature supports common compliance use cases, such as being required to replace your instances on a schedule due to internal security policies or external compliance controls.

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. This setting applies to all current and future instances in your Auto Scaling group.

Setting this value too low can cause instances to be replaced faster than desired. In general, Amazon EC2 Auto Scaling replaces instances one at a time, with a pause in between replacements. However, more than one instance must be replaced when there is not enough time to replace each instance individually based on the maximum instance lifetime that you specified. In this case, Amazon EC2 Auto Scaling could replace several instances at once, by up to 10 percent of the current capacity of your Auto Scaling group at a time.

To manage the rate of replacement, you can do the following:

  • Set the maximum instance lifetime limit to a longer period of time to space out the replacements. This is helpful for groups that have a large number of instances to replace.

  • Add extra time between certain replacements by using instance protection to temporarily prevent individual instances in your Auto Scaling group from being replaced. When you're ready to replace these instances, remove instance protection from each individual instance. For more information, see Using instance scale-in protection.

Configure the maximum instance lifetime setting

When you create an Auto Scaling group in the console, you cannot configure the maximum instance lifetime setting. However, after the group is created, you can edit it to add, update, or clear the maximum instance lifetime setting.

To configure the maximum instance lifetime for a group (console)

  1. Open the Amazon EC2 Auto Scaling console at https://console.aws.amazon.com/ec2autoscaling/.

  2. Select check box next to the Auto Scaling group.

    A split pane opens up in the bottom part of the Auto Scaling groups page, showing information about the group that's selected.

  3. On the Details tab, choose Advanced configurations, Edit.

  4. For Maximum instance lifetime, enter the maximum number of seconds that an instance can be in service.

  5. Choose Update.

To configure maximum instance lifetime for a group (AWS CLI)

You can also use the AWS CLI to configure the maximum instance lifetime for new or existing Auto Scaling groups.

For new Auto Scaling groups, use the create-auto-scaling-group command.

aws autoscaling create-auto-scaling-group --cli-input-json file://~/config.json

The following is an example config.json file that shows a maximum instance lifetime of 2592000 seconds (30 days).

{ "AutoScalingGroupName": "my-asg", "LaunchTemplate": { "LaunchTemplateName": "my-launch-template", "Version": "$Latest" }, "MinSize": 1, "MaxSize": 5, "MaxInstanceLifetime": 2592000, "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782", "Tags": [] }

For existing Auto Scaling groups, use the update-auto-scaling-group command.

aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-existing-asg --max-instance-lifetime 2592000

To verify the maximum instance lifetime for an Auto Scaling group

Use the describe-auto-scaling-groups command.

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg

The following is an example response.

{ "AutoScalingGroups": [ { "AutoScalingGroupName": "my-asg", "AutoScalingGroupARN": "arn", "LaunchTemplate": { "LaunchTemplateId": "lt-0b97f1e282EXAMPLE", "LaunchTemplateName": "my-launch-template", "Version": "$Latest" }, "MinSize": 1, "MaxSize": 5, "DesiredCapacity": 1, "DefaultCooldown": 300, "AvailabilityZones": [ "us-west-2a", "us-west-2b", "us-west-2c" ], "LoadBalancerNames": [], "TargetGroupARNs": [], "HealthCheckType": "EC2", "HealthCheckGracePeriod": 0, "Instances": [ { "InstanceId": "i-04d180b9d5fc578fc", "InstanceType": "t2.small", "AvailabilityZone": "us-west-2b", "LifecycleState": "Pending", "HealthStatus": "Healthy", "LaunchTemplate": { "LaunchTemplateId": "lt-0b97f1e282EXAMPLE", "LaunchTemplateName": "my-launch-template", "Version": "7" }, "ProtectedFromScaleIn": false } ], "CreatedTime": "2019-11-14T22:56:15.487Z", "SuspendedProcesses": [], "VPCZoneIdentifier": "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782", "EnabledMetrics": [], "Tags": [], "TerminationPolicies": [ "Default" ], "NewInstancesProtectedFromScaleIn": false, "ServiceLinkedRoleARN": "arn", "MaxInstanceLifetime": 2592000 } ] }

Limitations

  • Maximum lifetime not guaranteed to be exact for every instance: Instances are not guaranteed to be replaced only at the end of their maximum duration. In some situations, Amazon EC2 Auto Scaling might need to start replacing instances immediately after you update the maximum instance lifetime parameter. The reason for this behavior is to avoid replacing all instances at the same time.

  • Instances terminated before launch: When there is only one instance in the Auto Scaling group, the maximum instance lifetime feature can result in an outage because Amazon EC2 Auto Scaling terminates an instance and then launches a new instance.