Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find who created a CloudFormation stack?

How do I find who created a CloudFormation stack?

I am using boto3 to list the stacks whose status is COMPLETE along with the user who created the stack. I can get all the attributes of the stack but I am unable to find the user information in CloudFormation dashboard or in boto3 CF APIs. Any idea how to get the IAM username of the user that created the stack?

Thanks

Snippet of my code:

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']
  ...
  ...
like image 633
helloV Avatar asked Feb 07 '23 07:02

helloV


1 Answers

You can get this information through CloudTrail. In particular, call lookup_events() on the CloudTrail client:

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']
like image 81
ataylor Avatar answered Feb 09 '23 22:02

ataylor