AWS AutoScaling 是否有可能在计费小时边界之前永不终止实例?

Is it possible with AWS AutoScaling to never terminate instances until a billable hour boundary?

由于 AWS 实例按小时计费,如果您有可能再次需要它,终止 运行 少于一个小时的实例是没有意义的。

我想避免自动缩放的情况,即我添加一个实例,然后终止它,然后在同一小时内添加另一个实例。这将导致两个计费小时数。

我编写了自己的自动缩放器,它会跳过终止运行时间少于 55 分钟的任何实例,并且对于快速变化的负载,这为我们节省了大量费用。只是想知道 AWS 本身是否有能力。

Auto Scaling 在一小时结束前无法'wait' 终止实例。但是,有几个选项可供探索!

当触发指示 Auto Scaling 缩减(删除 Amazon EC2 实例)的扩展策略时,它首先选择具有最多实例的可用区,然后确定在该可用区内终止哪个实例。这个选择是由 Termination Policy 做出的,它的值可以是:

  • OldestInstance: Auto Scaling terminates the oldest instance in the group. This option is useful when you're upgrading the instances in the Auto Scaling group to a new EC2 instance type, and want to eventually replace instances with older instances with newer ones.
  • NewestInstance: Auto Scaling terminates the newest instance in the group. This policy is useful when you're testing a new launch configuration but don't want to keep it in production.
  • OldestLaunchConfiguration: Auto Scaling terminates instances that have the oldest launch configuration. This policy is useful when you're updating a group and phasing out the instances from a previous configuration.
  • ClosestToNextInstanceHour: Auto Scaling terminates instances that are closest to the next billing hour. This policy helps you maximize the use of your instances and manage costs.

最后一个选项 ClosestToNextInstanceHour 几乎就是您要找的,因为它将终止一个实例,该实例接下来将按小时收费。但是,要到一小时结束才会 'wait'。

一种选择是编写您自己的应用程序来确定何时缩减实例,等待实例提供其全部价值。然后应用可以调用 TerminateInstanceInAutoScalingGroup 来删除和终止实例。

另一个选项,如果实例被用作 'back-end'(不直接处理 Web 请求),则使用 Auto Scaling Lifecycle Hook 在实例从自动缩放中删除时发送信号组,但在它被终止之前。这通常用于为实例提供 "finish off" 工作的机会,例如复制日志文件和完成任务。如果实例用于处理长运行后台任务,生命周期挂钩可用于等待工作完成或计费时间几乎结束,然后才允许实例终止。

但是,所有这些都需要一些自定义脚本。开箱即用的自动缩放配置将立即终止实例以响应缩放策略(除非 Connection Draining 生效)。