def lambda_handler()

in functions/source/c1c_controltower_lifecycle.py [0:0]


def lambda_handler(event, context):
    logger.info(f"Event received by handler: {event}")
    logger.info(
        f"function name: {context.function_name} "
        f"invoked arn: {context.invoked_function_arn}"
    )
    if "RequestType" in event:
        logger.info(f"Handling CloudFormation Request")
        if event["RequestType"] == "Create":
            logger.info(f"Received CloudFormation create")
            response = cfnhelper.cfnResponse(event, context)
            try:
                fresh_deploy(context.function_name)
            except Exception as e:
                logger.error(f"Failed to handle create event with exception: {e}")
                response.send(cfnhelper.responseCode.FAILED)
                return False
            response.send(cfnhelper.responseCode.SUCCESS)
        elif event["RequestType"] == "Update":
            logger.info(f"Received CloudFormation update")
            response = cfnhelper.cfnResponse(event, context)
            try:
                update_accounts(context.function_name)
            except Exception as e:
                logger.error(f"Failed to handle update event with exception: {e}")
                response.send(cfnhelper.responseCode.FAILED)
                return False
            response.send(cfnhelper.responseCode.SUCCESS)
        else:
            logger.warn(
                f"Ignoring unhandled CloudFormation request type: {event['RequestType']}"
            )
            response = cfnhelper.cfnResponse(event, context)
            response.send(cfnhelper.responseCode.SUCCESS)
    elif "InvokeAction" in event:
        try:
            if event["InvokeAction"] == "configure_account":
                configure_account(event["account_id"])
            elif event["InvokeAction"] == "update_account":
                update_policy(event["account_id"])
            elif event["InvokeAction"] == "remove_account_config":
                remove_account_config(event["account_id"])
            elif event["InvokeAction"] == "remove_all":
                remove_all(context.function_name)
            else:
                logger.warn(
                    f'Unrecognized InvokeAction {event["InvokeAction"]} -- try one of configure_account, update_account, remove_account_config, remove_all'
                )
            logger.info(f"Done")
        except Exception as e:
            logger.error(f"Failed to handle invoke action: {e}")
            return False
    else:
        try:
            life_cycle_event = ctlifecycleevent.LifeCycleEvent(event)
        except Exception as e:
            logger.warn(f"Did not find a supported event: {e}")
            return
        if life_cycle_event.create_account:
            try:
                configure_account(life_cycle_event.child_account_id)
            except Exception as e:
                logger.error(
                    f"Failed to handle create/update event from Control Tower: {e}"
                )
        elif life_cycle_event.remove_account:
            try:
                remove_account_config(life_cycle_event.child_account_id)
            except Exception as e:
                logger.error(f"Failed to handle remove event from Control Tower: {e}")
        else:
            logger.info(
                f"This is not an event handled by the integration. SKIPPING: {event}"
            )
            response = cfnhelper.cfnResponse(event, context)
            response.send(cfnhelper.responseCode.FAILED)
        return False