如何创建 Python 方法来使用 Boto 查找 CloudFormation 堆栈的 UUID?

How can I create a Python method that finds the UUID of a CloudFormation stack with Boto?

我们目前使用:

cloudformation = environment+"-"+role
qa = QAWS()
qa.initialize(environ)
events = qa.cfn.describe_stack_events(StackName=sn)
"Some interesting stuff:
event["Timestamp"]
event["ResourceStatus"]
event["ResourceType"]
event["LogicalResourceId"]
if exists = event["ResourceStatusReason"]
"""
return events["StackEvents"]

这适用于我们的大多数 Cloudformation 堆栈,它们是:

但对于我们的一些具有 UUID 的堆栈则不然:

我该如何解决这个问题?

我目前对 cloudformation 堆栈名称中的 "unique-id" 做了类似的事情,理论上你可以做的只是删除角色等,然后​​以这种方式搜索 'test'你只会得到像 'test-vpn' 'test-activedirectory' 等

这样的堆栈
import boto3
import re
from sys import argv

"""
profile = AWS CLI profile you want to use, a.k.a what account you want to run this in.
region = Self explanatory, generally eu-west-1/2 etc.
unique_id = Your unique id for the CF stacks, i.e test, ppe or prod

Example usage: python delete_stacks.py test eu-west-1 test

"""
_, profile, region, unique_id  = argv

session = boto3.Session(profile_name=profile)
client = session.client('cloudformation', region_name=region)

response = client.describe_stacks().get('Stacks',[])
stacks = []
for r in response:
    ((stacks.append(r['StackName']) if unique_id in r['StackName'] else None))

print("These are the stacks that were found")
print(stacks)


for s in stacks:
    events = client.describe_stack_events(StackName=s)
    print(events)

这是在 python3 中编写的,我不确定您使用哪个 python 版本进行开发,因此如果使用 python2,您可能需要进行调整。