"""Lambda sync_account function module.""" import json import sentry_sdk from kafka_utils.consumer.source.mapping import EventSourceMessage from lambdacommon.common_config import logger from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration import config from sync_account.constants.constants import MSK_KAFKA_SOURCE, SELF_MANAGED_KAFKA_SOURCE from sync_account.processor import SyncAccountProcessor if config.SENTRY_DSN: sentry_sdk.init( dsn=config.SENTRY_DSN, environment=config.ENVIRONMENT, integrations=[AwsLambdaIntegration(timeout_warning=True)], ) def handler(event, context): """Lambda entry point.""" try: event_value = event event_source = event.get('eventSource') # Check this is a Kafka event. The event source will be 'aws:kafka' if triggered # by an MSK cluster in the same AWS account, or 'SelfManagedKafka' if triggered # by a cluster in another AWS account. if event_source in [MSK_KAFKA_SOURCE, SELF_MANAGED_KAFKA_SOURCE]: event_messages = EventSourceMessage(event) for __, event_message in event_messages: event_value = json.loads(event_message.value) event_value.update({'topic': event_message.topic}) SyncAccountProcessor(event=event_value).process() except Exception as e: logger.exception(str(e)) sentry_sdk.capture_exception() return {'status': 'OK'}