如何使用 Boto 自行终止其 运行 上的实例?
How to use Boto to self-terminate instance its running on?
我需要从 AutoScalingGroup 中终止一个实例,因为 ASG 的策略使横向扩展的实例 运行 比预期的要长。我需要在完成 运行 一个 python 进程后终止所述实例。
该代码已使用 Boto 访问其他 AWS 服务,因此我希望利用 Boto 进行自我终止。我被告知我需要在终止之前将实例与其 ASG 分离以避免副作用。
知道如何进行这种分离和自我终止吗?
可以使用 detach_instances()
:
从 Auto Scaling 组中删除实例
Removes one or more instances from the specified Auto Scaling group.
After the instances are detached, you can manage them independent of the Auto Scaling group.
If you do not specify the option to decrement the desired capacity, Amazon EC2 Auto Scaling launches instances to replace the ones that are detached.
response = client.detach_instances(
InstanceIds=[
'string',
],
AutoScalingGroupName='string',
ShouldDecrementDesiredCapacity=True|False
)
所以,步骤是:
- 获取要删除的实例ID
- 致电
detach_instances(InstanceIds=['i-xxx'], ShouldDecrementDesiredCapacity=True)
- 致电
terminate_instances(InstanceIds=['i-xxx'])
这可以是 运行 来自实例本身,也可以来自 Internet 上的任何地方。
我需要从 AutoScalingGroup 中终止一个实例,因为 ASG 的策略使横向扩展的实例 运行 比预期的要长。我需要在完成 运行 一个 python 进程后终止所述实例。
该代码已使用 Boto 访问其他 AWS 服务,因此我希望利用 Boto 进行自我终止。我被告知我需要在终止之前将实例与其 ASG 分离以避免副作用。
知道如何进行这种分离和自我终止吗?
可以使用 detach_instances()
:
Removes one or more instances from the specified Auto Scaling group.
After the instances are detached, you can manage them independent of the Auto Scaling group.
If you do not specify the option to decrement the desired capacity, Amazon EC2 Auto Scaling launches instances to replace the ones that are detached.
response = client.detach_instances(
InstanceIds=[
'string',
],
AutoScalingGroupName='string',
ShouldDecrementDesiredCapacity=True|False
)
所以,步骤是:
- 获取要删除的实例ID
- 致电
detach_instances(InstanceIds=['i-xxx'], ShouldDecrementDesiredCapacity=True)
- 致电
terminate_instances(InstanceIds=['i-xxx'])
这可以是 运行 来自实例本身,也可以来自 Internet 上的任何地方。