如何找到谁创建了 CloudFormation 堆栈?

hel*_*loV 4 amazon-web-services aws-cloudformation boto3

如何找到谁创建了 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']
  ...
  ...
Run Code Online (Sandbox Code Playgroud)

ata*_*lor 6

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

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']
Run Code Online (Sandbox Code Playgroud)