gpt4 book ai didi

python - boto3 cloudformation list_stacks() 函数未列出 cloudformation 中的所有堆栈

转载 作者:行者123 更新时间:2023-12-03 07:26:15 27 4
gpt4 key购买 nike

我在云形成中创建了一个堆栈。当尝试通过 aws cli 获取所有堆栈时,它可以工作,但我尝试通过 python 中的 boto3 API 获取所有堆栈。在这里,它没有收集所有堆栈信息。很少有堆栈信息被遗漏。我比较了 cli 和 boto3 api 结果。

我使用下面的命令列出所有堆栈

CLI

aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE

boto3 API

client = boto3.client('cloudformation')
stacks = client.list_stacks(StackStatusFilter=["CREATE_COMPLETE"])

最佳答案

ListStacks API是分页的,即它只返回固定的结果大小 (1MB),直到您需要检索结果的下一页。 CLI 会自动为您处理分页 - 在 boto3 中您必须自己实现。

在 boto3 中,您可以使用 paginator like this获取所有页面:

import boto3

client = boto3.client("cloudformation")

paginator = client.get_paginator("list_stacks")

response_iterator = paginator.paginate(
StackStatusFilter=["CREATE_COMPLETE"],
)

# This contains stack summaries of all stacks in CREATE_COMPLETE
stack_summaries = [
response["StackSummaries"] for response in response_iterator
]

# Instead of printing you can do something with the stacks :)
print(f"Found {len(stack_summaries)} stacks in CREATE_COMPLETE")

关于python - boto3 cloudformation list_stacks() 函数未列出 cloudformation 中的所有堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75066953/

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