Hug*_*res 10 python boto amazon-web-services aws-cloudformation
使用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
Run Code Online (Sandbox Code Playgroud)
这很慢,因为我有很多堆栈.
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
Run Code Online (Sandbox Code Playgroud)
这需要永远,因为摘要保留了90天,我有很多堆栈.
问题:检查给定堆栈是否存在且未处于故障或回滚状态的理想解决方案是什么?
小智 9
我实现了以下工作:
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
Run Code Online (Sandbox Code Playgroud)
我没有发现任何以前的解决方案是完整的,也没有任何使用 boto3 快速完成的方法,所以我创建了上述解决方案。
来自 boto 文档:
\n\n\ndescribe_stacks(stack_name_or_id=无,next_token=无)
\n返回指定堆栈的描述;如果未指定堆栈名称,则返回所有创建的堆栈的描述。
\n参数: stack_name_or_id (字符串) \xe2\x80\x93 与堆栈关联的名称或唯一标识符。
\n
由于您知道堆栈名称,因此可以使用describe_stacks(stack_name_or_id=STACK_NAME). 这应该会加快你的速度。
| 归档时间: |
|
| 查看次数: |
10853 次 |
| 最近记录: |