def lambda_handler()

in control-tower-account-factory/src/handler.py [0:0]


def lambda_handler(event, context):
    """lambda entry"""
    LOGGER.info(f'REQUEST RECEIVED: {json.dumps(event, default=str)}')

    # get current account
    execution_account_id = context.invoked_function_arn.split(':')[4]

    # check if lambda call by AWS CloudWatch Event in response to creation of new AWS Control Tower account
    if ('detail' in event) and ('eventName' in event['detail']) and (event['detail']['eventName'] == 'CreateManagedAccount'):
        service_detail = event['detail']['serviceEventDetails']
        status = service_detail['createManagedAccountStatus']
        LOGGER.info(
            'AWS Control Tower Event: CreateManagedAccount %s' % (status)
            )
        # get new account id and name
        account_id = status['account']['accountId']
        account_name = status['account']['accountName']
        # get organization unit where the new account was added
        ou_name = status['organizationalUnit']['organizationalUnitName']
        # if account creation completed, start baselien process
        if status['state'] == 'SUCCEEDED':
            LOGGER.info(f'Init Account Baseline. Account name: {account_name}, Account id: {account_id}, OU: {ou_name}')
            BaselineInit(account_id, ou_name, REGION, execution_account_id)
        else:
            LOGGER.info(f'Baseline skipped. Account status: {status["state"]}')
    elif 'Records' in event:
        update_file = os.environ['update_file']
        for record in event['Records']:
            if 's3' in record and record['s3']['object']['key'] == update_file:
                LOGGER.info('Init Update Products')
                BaselineUpdate(REGION, execution_account_id)

    # check if AWS Lambda call by state machine
    elif ('provision_products' in event and 'account' in event):

        deployed_products = (event['deployed_products'] if 'deployed_products' in event else [])
        failed_products = (event['failed_products'] if 'failed_products' in event else [])
        skipped_products = (event['skipped_products'] if 'skipped_products' in event else [])
        max_iterations = (event['max_iterations'] if 'max_iterations' in event and int(event['max_iterations']) > 0 else int(os.environ['max_iterations']))
        # increase how many time lambda was call be state machine
        iteration = (event['iteration'] if 'iteration' in event else 0)

        LOGGER.info(f'Init Product Baseline. Account id: {event["account"]}')
        # start/ contiune account baseline process
        baseline_account = BaselineAccount(event['account'], event['provision_products'], event['update_products'], deployed_products, failed_products, skipped_products, execution_account_id, iteration, max_iterations, REGION)
        # get baseline status
        stm_response = baseline_account.get_response()
        LOGGER.info(f'Response status: {stm_response["status"]}')
        # response status back to state machine
        return stm_response