[英]Unable to iterate through all the alarms on cloudwatch using boto3 paginate
[英]Deleting all the CloudWatch rules using boto3
我们有大量需要删除的 cloudwatch 规则,我正在编写 python 脚本,以删除所有 CloudWatch 规则,但我只能在 boto3 网站上找到特定规则的删除规则,但我想删除我们拥有的所有规则。
import boto3
client = boto3.client('events')
response = client.delete_rule(
Name='string'
)
您必须分两个阶段进行。
Name
列表client = boto3.client('events')
rule_names = [rule['Name'] for rule in client.list_rules()['Rules']]
for rule_name in rule_names:
response = client.delete_rule(Name=rule_name)
print(response)
根据您实际拥有的规则数量,您可能需要使用NextToken
多次运行list_rules
。
由于 CloudWatch 事件规则通常至少有一个目标,因此您需要:
因此生成的脚本将如下所示:
import boto3
client = boto3.client('events')
rules = client.list_rules()['Rules']
for rule in rules:
rule_targets = client.list_targets_by_rule(
Rule=rule['Name']
)['Targets']
target_ids = [target['Id'] for target in rule_targets]
remove_targets_response = client.remove_targets(
Rule=rule['Name'],
Ids=target_ids
)
print(remove_targets_response)
delete_rule_response = client.delete_rule(
Name=rule['Name']
)
print(delete_rule_response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.