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



我们目前使用:

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堆栈,它们是:

  • 测试vpn
  • 测试应用程序

但不适用于我们的一些具有UUID的堆栈:

  • 测试-rds-CRC25DFADXZR

如何解决此问题?

我目前对云信息堆栈名称中的"唯一id"做了类似的处理,理论上你可以做的只是删除角色等,然后搜索"测试",这样你只会得到像"测试vpn"测试Active Directory"等的堆栈。

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,你可能需要进行调整。

最新更新