python - 如何使用 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
我该如何解决这个问题?
解决方案
我目前使用 cloudformation 堆栈名称中的“唯一 ID”做了类似的事情,理论上你可以做的只是删除角色等并搜索“测试”,这样你只会得到堆栈像'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,您可能需要进行调整。
推荐阅读
- sqlite - 从具有多个插入值的单元格中获取单个值乘以来自具有多个插入值的另一个单元格中的单个值
- rubygems - gem yank (<3.1.0) 返回“您已启用多因素身份验证,但未提供 OTP 代码”但您无法在命令行上提供
- python - 如何从具有根据日期更改的动态 URL 的多个 CSV 文件下载数据?
- rust - 无法使用 rusted_cypher 连接到 Neo4j
- stata - 无效组规范 - center 定义的组都不是控件
- arrays - 数组的 rank 属性如何与其数学相关
- asp.net - 多个国家/地区的西班牙资源 RESX 文件
- javascript - 我收到此错误 TS2554: Expected 3-4 arguments, but got 5
- android - 使用 Jetpack Compose 从 android 图库中选择多个图像
- docker - Docker 3.5.1 安装错误:(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))