使用Boto检查CloudFormation堆栈是否存在且未处于中断状态的最佳方法是什么?我指的是失败和回滚状态。
我不想使用try/except解决方案,因为boto将其记录为错误,在我的场景中,它将将异常日志发送到警报系统。
目前,我有以下解决方案:
1)使用boto.cloudformation.connection.CloudFormationConnection.describe_stacks()
valid_states = '''\
CREATE_IN_PROGRESS
CREATE_COMPLETE
UPDATE_IN_PROGRESS
UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
UPDATE_COMPLETE'''.splitlines()
def describe_stacks():
result = []
resp = cf_conn.describe_stacks()
result.extend(resp)
while resp.next_token:
resp = cf_conn.describe_stacks(next_token=resp.next_token)
result.extend(resp)
return result
stacks = [stack for stack in describe_stacks() if stack.stack_name == STACK_NAME and stack.stack_status in valid_states]
exists = len(stacks) >= 1这很慢,因为我有很多堆叠。
2)使用boto.cloudformation.connection.CloudFormationConnection.list_stacks()
def list_stacks(filters):
result = []
resp = cf_conn.list_stacks(filters)
result.extend(resp)
while resp.next_token:
resp = cf_conn.list_stacks(filters, next_token=resp.next_token)
result.extend(resp)
return result
stacks = [stack for stack in list_stacks(valid_states) if stack.stack_name == STACK_NAME]
exists = len(stacks) >= 1这要花费很长时间,因为总结要保存90天,而且我有很多堆叠。
问:什么是理想的解决方案来检查给定的堆栈是否存在并且没有处于失败或回滚状态?
发布于 2019-01-04 06:49:58
我实现了以下工作:
import boto3
from botocore.exceptions import ClientError
client = boto3.client('cloudformation')
def stack_exists(name, required_status = 'CREATE_COMPLETE'):
try:
data = client.describe_stacks(StackName = name)
except ClientError:
return False
return data['Stacks'][0]['StackStatus'] == required_status我没有发现任何以前的解决方案都没有完成,也没有任何快速的方法使用boto3,所以我创建了上面的。
发布于 2014-04-11 18:44:47
从boto医生那里:
describe_stacks(stack_name_or_id=None,next_token=None) 返回指定堆栈的说明;如果未指定堆栈名称,则返回创建的所有堆栈的说明。 参数: stack_name_or_id (string) -与堆栈关联的名称或唯一标识符。
因为您知道堆栈名称,所以可以使用describe_stacks(stack_name_or_id=STACK_NAME)。这会让你的事情更快。
发布于 2017-05-19 21:39:00
我知道这很古老,但有人问我几周前是否有解决办法,所以就这样说.
如果您阅读boto3文档,它会经常提到已删除的堆栈。为了做到这一点,有来使用完整的堆栈ID,不能使用堆栈名。这是因为堆栈唯一真正独特的地方是ID。
示例:
resource = boto3.resource('cloudformation')
status = resource.Stack('id:of:stack').stack_status只有当堆栈ID不存在时,才会返回异常。
干杯!
https://stackoverflow.com/questions/23019166
复制相似问题