def get_caller_arn()

in functions/source/GetCallerArn/lambda_function.py [0:0]


def get_caller_arn(stack_id):
    try:
        root_id = cfn_client.describe_stacks(StackName=stack_id)['Stacks'][0]['RootId']
    except ValueError:
        traceback.print_exc()
        return "NotFound"
    except IndexError:
        traceback.print_exc()
        return "NotFound"
    create_time = cfn_client.describe_stacks(StackName=root_id)['Stacks'][0]['CreationTime']
    retries = 50
    while True:
        retries -= 1
        try:
            response = ct_client.lookup_events(
                LookupAttributes=[
                    {'AttributeKey': 'ResourceName', 'AttributeValue': root_id},
                    {'AttributeKey': 'EventName', 'AttributeValue': 'CreateStack'}
                ],
                StartTime=create_time - timedelta(minutes=15),
                EndTime=create_time + timedelta(minutes=15)
            )
            if len(response['Events']) > 0:
                return sts_to_role(json.loads(response['Events'][0]['CloudTrailEvent'])['userIdentity']['arn'])
            logger.info('Event not in cloudtrail yet, %s retries left' % str(retries))
        except Exception as e:
            logger.error(str(e), exc_info=True)
        if retries == 0:
            print("Ran out of retries!")
            return "NotFound"
        sleep(15)