【问题标题】:How do I find who created a CloudFormation stack?如何找到创建 CloudFormation 堆栈的人员?
【发布时间】:2016-07-28 21:25:31
【问题描述】:

如何找到创建 CloudFormation 堆栈的人员?

我正在使用boto3 列出状态为COMPLETE 的堆栈以及创建堆栈的用户。我可以获得堆栈的所有属性,但无法在 CloudFormation 仪表板或 boto3 CF API 中找到用户信息。知道如何获取创建堆栈的用户的 IAM 用户名吗?

谢谢

我的代码片段:

import boto3

cf  = boto3.client('cloudformation', region_name='us-east-1')
stacks = cf.list_stacks(StackStatusFilter=['CREATE_COMPLETE'])['StackSummaries']
names = [stack['StackName'] for stack in stacks]

for name in names:
  resources = cf.describe_stack_resources(StackName=name)['StackResources']
  ...
  ...

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation boto3


    【解决方案1】:

    您可以通过 CloudTrail 获取此信息。特别是在 CloudTrail 客户端上调用lookup_events()

    events = cloudtrail_client.lookup_events(LookupAttributes=[{'AttributeKey':'EventName', 'AttributeValue':'CreateStack'}])
    for event in events['Events']:
        event_detail = json.loads(event['CloudTrailEvent'])
        if event_detail['requestParameters']['stackName'] == myStackName:
            creator = event['Username']
    

    【讨论】:

    • 我考虑过使用 CloudTrail。但有些堆栈已有 2 年以上的历史,我们的 CloudTrail 生命周期策略为 6 个月。还是谢谢。
    • 不幸的是,我认为 IAM 创建者不会通过 CloudFormation API 暴露在任何地方。将它作为模板参数传递可能是一个很好的策略,但这对现有堆栈没有帮助。对不起。
    • 但如果我在 CloudFormation 中启动一个实例,该实例的 RunInstances() 具有 IAM 用户的属性。 AWS 必须在内部根据传递的凭证获取用户名。
    【解决方案2】:

    使用 Cloud Trail 服务 api 调用 lookup_events() 和 AttributeKey 'EventName', AttributeValue:'CreateStack'

    【讨论】:

    • 为答案添加一些解释。只讲 API 是没有用的。
    猜你喜欢
    • 1970-01-01
    • 2012-12-02
    • 2018-05-09
    • 2021-10-12
    • 2018-10-01
    • 2019-12-13
    • 2020-11-09
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多