def lambda_handler()

in source/lambda/sfn-iot-mr-thing-crud/lambda_function.py [0:0]


def lambda_handler(event, context):
    global ERRORS
    ERRORS = []

    logger.info('event: {}'.format(event))

    try:
        event = ddb_json.loads(event)
        logger.info('cleaned event: {}'.format(event))

        boto3_config = Config(retries = {'max_attempts': 12, 'mode': 'standard'})

        c_iot = boto3.client('iot', config=boto3_config)
        c_dynamo = boto3.client('dynamodb')

        secondary_region = os.environ['AWS_REGION']
        logger.info('secondary_region: {}'.format(secondary_region))

        if event['NewImage']['operation'] == 'CREATED':
            logger.info('operation: {}'.format(event['NewImage']['operation']))
            thing_name = event['NewImage']['thingName']
            logger.info('thing_name: {}'.format(thing_name))
            attrs = {}
            if 'attributes' in event['NewImage'] and event['NewImage']['attributes']:
                attrs = {'attributes': {}}
                for key in event['NewImage']['attributes']:
                    attrs['attributes'][key] = event['NewImage']['attributes'][key]

            if 'attributes' in attrs:
                attrs['merge'] = False
            logger.info('attrs: {}'.format(attrs))

            thing_type_name = ""
            if 'thingTypeName' in event['NewImage']:
                thing_type_name = event['NewImage']['thingTypeName']
            logger.info('thing_type_name: {}'.format(thing_type_name))
            primary_region = event['NewImage']['aws:rep:updateregion']
            logger.info('primary_region: {}'.format(primary_region))
            logger.info('CREATE_MODE: {}'.format(CREATE_MODE))

            c_iot_p = boto3.client('iot', config=boto3_config, region_name = primary_region)

            start_time = int(time.time()*1000)
            if CREATE_MODE == 'thing_only':
                create_thing(c_iot, c_iot_p, thing_name, thing_type_name, attrs)
            else:
                create_thing_with_cert_and_policy(c_iot, c_iot_p, thing_name, thing_type_name, attrs, 3, 2)
            end_time = int(time.time()*1000)
            duration = end_time - start_time
            logger.info('thing created: thing_name: {}: duration: {}ms'.format(thing_name, duration))
            logger.info('thing created, deleting from dynamo if exists: thing_name: {}'.format(thing_name))
            delete_thing_create_error(c_dynamo, thing_name, DYNAMODB_ERROR_TABLE)

        if event['NewImage']['operation'] == 'UPDATED':
            logger.info('operation: {}'.format(event['NewImage']['operation']))
            thing_name = event['NewImage']['thingName']
            logger.info("thing_name: {}".format(thing_name))

            primary_region = event['NewImage']['aws:rep:updateregion']
            logger.info('primary_region: {}'.format(primary_region))

            c_iot_p = boto3.client('iot', config=boto3_config, region_name = primary_region)

            attrs = {}
            if 'attributes' in event['NewImage']:
                for key in event['NewImage']['attributes']:
                    attrs[key] = event['NewImage']['attributes'][key]

            merge = True
            if attrs:
                merge = False

            thing_type_name = ""
            if 'S' in event['NewImage']['thingTypeName']:
                thing_type_name = event['NewImage']['thingTypeName']['S']

            logger.info("thing_name: {} thing_type_name: {} attrs: {}".
                format(thing_name, thing_type_name, attrs))
            update_thing(c_iot, c_iot_p, thing_name, thing_type_name, attrs, merge)

        if event['NewImage']['operation'] == 'DELETED':
            logger.info('operation: {}'.format(event['NewImage']['operation']))
            thing_name = event['NewImage']['thingName']
            logger.info("thing_name: {}".format(thing_name))

            iot_data_endpoint = get_iot_data_endpoint(
                os.environ['AWS_REGION'],
                [IOT_ENDPOINT_PRIMARY, IOT_ENDPOINT_SECONDARY]
            )

            delete_thing(c_iot, thing_name, iot_data_endpoint)
            delete_thing_create_error(c_dynamo, thing_name, DYNAMODB_ERROR_TABLE)

    except device_replication.DeviceReplicationCreateThingException as e:
        logger.error(e)
        ERRORS.append("lambda_handler: {}".format(e))
        error_message = ', '.join(ERRORS)
        if event['NewImage']['operation'] == 'CREATED':
            update_table_create_thing_error(c_dynamo, thing_name, primary_region, error_message)

    except Exception as e:
        logger.error(e)
        ERRORS.append('lambda_handler: {}'.format(e))

    if ERRORS:
        error_message = ', '.join(ERRORS)
        logger.error('{}'.format(error_message))
        raise ThingCrudException('{}'.format(error_message))

    return {'message': 'success'}