def lambda_handler()

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


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

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

    try:
        c_iot = boto3.client('iot')

        if event['NewImage']['eventType']['S'] == 'THING_GROUP_EVENT':
            thing_group_name = event['NewImage']['thingGroupName']['S']
            logger.info("operation: {} thing_group_name: {}".
                format(event['NewImage']['operation']['S'], thing_group_name))
            if event['NewImage']['operation']['S'] == 'CREATED':
                description = ""
                if 'S' in event['NewImage']['description']:
                    description = event['NewImage']['description']['S']

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

                merge = True
                if attrs:
                    merge = False
                logger.info('description: {} attrs: {}'.format(description, attrs))
                create_thing_group(c_iot, thing_group_name, description, attrs, merge)
            elif event['NewImage']['operation']['S'] == 'DELETED':
                delete_thing_group(c_iot, thing_group_name)
            elif event['NewImage']['operation']['S'] == 'UPDATED':
                description = ""
                if 'S' in event['NewImage']['description']:
                    description = event['NewImage']['description']['S']

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

                merge = True
                if attrs:
                    merge = False
                logger.info('description: {} attrs: {}'.format(description, attrs))
                update_thing_group(c_iot, thing_group_name, description, attrs, merge)
        elif event['NewImage']['eventType']['S'] == 'THING_GROUP_MEMBERSHIP_EVENT':
            group_arn = event['NewImage']['groupArn']['S']
            thing_arn = event['NewImage']['thingArn']['S']
            thing_group_name = group_arn.split('/')[-1]
            thing_name = thing_arn.split('/')[-1]
            logger.info("operation: {} group_arn: {} thing_arn: {} thing_group_name: {} thing_name: {}".
                format(event['NewImage']['operation']['S'], group_arn, thing_arn, thing_group_name, thing_name))
            if event['NewImage']['operation']['S'] == 'ADDED':
                add_thing_to_group(c_iot, thing_group_name, thing_name)
            elif event['NewImage']['operation']['S'] == 'REMOVED':
                remove_thing_from_group(c_iot, thing_group_name, thing_name)

    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 ThingGroupCrudException('{}'.format(error_message))

    return {"message": "success"}