in source/lambda/sfn-iot-mr-thing-type-crud/lambda_function.py [0:0]
def lambda_handler(event, context):
logger.info('event: {}'.format(event))
try:
c_iot = boto3.client('iot')
if event['NewImage']['eventType']['S'] == 'THING_TYPE_EVENT':
if event['NewImage']['operation']['S'] == 'CREATED':
thing_type_name = event['NewImage']['thingTypeName']['S']
logger.info("thing_type_name: {}".format(thing_type_name))
create_thing_type(c_iot, thing_type_name)
elif event['NewImage']['operation']['S'] == 'UPDATED':
if 'isDeprecated' in event['NewImage'] and event['NewImage']['isDeprecated']['BOOL'] is True:
thing_type_name = event['NewImage']['thingTypeName']['S']
deprecate_type(c_iot, thing_type_name, False)
elif 'isDeprecated' in event['NewImage'] and event['NewImage']['isDeprecated']['BOOL'] is False:
thing_type_name = event['NewImage']['thingTypeName']['S']
deprecate_type(c_iot, thing_type_name, True)
elif event['NewImage']['operation']['S'] == 'DELETED':
thing_type_name = event['NewImage']['thingTypeName']['S']
logger.info("thing_type_name: {}".format(thing_type_name))
delete_type(c_iot, thing_type_name)
elif event['NewImage']['eventType']['S'] == 'THING_TYPE_ASSOCIATION_EVENT':
if event['NewImage']['operation']['S'] == 'ADDED':
thing_name = event['NewImage']['thingName']['S']
thing_type_name = event['NewImage']['thingTypeName']['S']
logger.info("ADDED: thing_name: {} thing_type_name: {}".format(thing_name, thing_type_name))
update_thing_type(c_iot, thing_name, thing_type_name)
elif event['NewImage']['operation']['S'] == 'REMOVED':
thing_name = event['NewImage']['thingName']['S']
thing_type_name = event['NewImage']['thingTypeName']['S']
logger.info("REMOVED: thing_name: {} thing_type_name: {}".format(thing_name, thing_type_name))
update_thing_type(c_iot, thing_name, None)
except Exception as e:
logger.error(e)
raise ThingTypeCrudException('{}'.format(e))
return {"message": "success"}