gpt4 book ai didi

amazon-web-services - 如何通过 boto3 更改终止保护

转载 作者:行者123 更新时间:2023-12-03 23:10:39 25 4
gpt4 key购买 nike

我想终止许多 AWS ec2 实例,然后我像这样使用 boto3:

#!/usr/bin/env python 
#coding=utf8

import boto3
ec2 = boto3.resource(
'ec2',
aws_access_key_id="<AK>",
aws_secret_access_key="<SK>",
region_name='eu-central-1'
)

instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])

ids = ['id1','id2'.....'idn']
status = ec2.instances.filter(InstanceIds=ids).terminate()
print(status)

但我得到了一个错误:

botocore.exceptions.ClientError: An error occurred (OperationNotPermitted) when calling the TerminateInstances operation: The instance 'i-0f06b49c1f16dcfde' may not be terminated. Modify its 'disableApiTermination' instance attribute and try again.

请告诉我如何修改disableApiTermination。

最佳答案

使用 modify_attribute .见 documentation .

import boto3
ec2 = boto3.resource('ec2')

instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}])

for instance in instances:
print(instance.id)

# after choosing the instances to terminate:
ids = ['id1', 'id2']
for id in ids:
ec2.Instance(id).modify_attribute(
DisableApiTermination={
'Value': False
})
ec2.Instance(id).terminate()

关于amazon-web-services - 如何通过 boto3 更改终止保护,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58024395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com