def lambda_handler()

in shd-notifier/Health-Event-Chat-Post-LambdaFn.py [0:0]


def lambda_handler(event, context):
    # read in the webhook to post too, since it is required error out if it is missing
    try:
        ENDPOINTARRAY = str(os.environ['ENDPOINTARRAY'])
        ENDPOINTARRAY = json.loads(ENDPOINTARRAY)
    except Exception as e:
        logger.error(e)
        eMessage = 'ERROR: Missing ENDPOINTARRAY Environment Variable for the Lambda Function!'
        logger.error(eMessage)
        raise Exception(eMessage)

    # read in the eventArn, error out if it is missing
    try:
        eventArn = event['eventArn']
    except Exception as e:
        logger.error(e)
        eMessage = 'ERROR: Invalid input, Event ARN Invalid!'
        logger.error(eMessage)
        raise Exception(eMessage)

    # read in the events name from the ARN
    # Health ARN Pattern: arn:aws:health:[^:]*:[^:]*:event/[\w-]+
    eventIDPos = eventArn.rfind('/')
    if (eventIDPos > 1): eventIDPos = eventIDPos + 1
    eventName = eventArn[eventIDPos:]
    logger.info("Event name: %s" % (eventName))

    # read in the lastUpdatedTime, if missing default to blank
    try:
        lastUpdatedTime = event['lastUpdatedTime']
    except Exception as e:
        eMessage = "WARN: Missing lastUpdatedTime defaulting to blank."
        logger.debug(eMessage)
        lastUpdatedTime = ''
    logger.debug("lastUpdatedTime: %s" % (lastUpdatedTime))

    # get the latest detailed description of the event
    detail = eventDetailedDesc(eventArn)
    # we want the events latest description
    latestDesc = detail['eventDescription']['latestDescription']
    # get this details last updated time
    curLastUpdatedTime = str(detail['event']['lastUpdatedTime'])
    logger.debug("curLastUpdatedTime: %s" % (curLastUpdatedTime))
    if (curLastUpdatedTime == lastUpdatedTime):
        if BAIL == 1:
            return curLastUpdatedTime
        latestDesc = "Event unchanged since the last update."

    # create the latest description posting
    message = ''
    eventNameStr = "[%s] " % eventName
    if (DEBUG):
        message = '[TESTING] PLEASE IGNORE DEBUGGING:\n'
    message = message + MESSAGEPREFIX
    if CHATCLIENT == 'sns':
        message = message + '\n\n'
    message = message + eventNameStr + latestDesc
    # trim messages larger than the maximum post size (4KB)
    if (len(message) > MAXCHIMEPOST):
        message = chimeTrimMessage(message)
    # post the message to the array of webhooks
    for webhook in ENDPOINTARRAY:
        chatMessage(message, eventNameStr, webhook, CHATCLIENT)
    # return the current updated time as the last updated time
    return curLastUpdatedTime