def lambda_handler()

in timestream/src-lambda-transform/app.py [0:0]


def lambda_handler(event, context):
    """ Transforms a binary payload by invoking "decode_{event.type}" function
        Parameters 
        ----------
        WirelessDeviceId : str
            Device Id
        WirelessMetadata : json
            Metadata
        PayloadData : str
            Base64 encoded input payload
        PayloadDecoderName : string
            The value of this attribute defines the name of a Python module which will be used to perform binary decoding. If value of "type" is for example "sample_device", then this function will perform an invocation of "sample_device.dict_from_payload" function. For this approach to work, you  have to import the necessary modules, e.g. by performing a "import sample_device01" command in the beginning of this file.

        Returns
        -------
        This function returns a JSON object with the following keys:

        - status: 200 or 500
        - transformed_payload: result of calling "decode_{event.type}"      (only if status == 200)
        - error_type                                                        (only if status == 500)
        - error_message                                                     (only if status == 500)
        - stackTrace                                                        (only if status == 500)


    """

    logger.info("Received event: %s" % json.dumps(event))

    # Store event input
    input_base64 = event.get("PayloadData")
    device_id = event.get("WirelessDeviceId")
    metadata = event.get("WirelessMetadata")["LoRaWAN"]
    payload_decoder_name = event.get("PayloadDecoderName")

    logger.info("Metadata: % s" % json.dumps(metadata))

    # Validate existence of payload type
    if (payload_decoder_name is None):
        raise InvalidInputException(
            "PayloadDecoderName is not specified")

    # Validate  if payload type is in the list of allowed values
    if (not payload_decoder_name in VALID_PAYLOAD_DECODER_NAMES):
        raise InvalidInputException(
            "PayloadDecoderName have one of the following values:"+(".".join(VALID_PAYLOAD_DECODER_NAMES)))

    logger.info(f"Base64 input={input_base64}, Type={payload_decoder_name}")

    # Derive a name of a payload conversion function based on the value of 'type' attribute
    conversion_function_name = f"{payload_decoder_name}.dict_from_payload"
    logger.info(f"Function name={conversion_function_name}")

    try:

        # Invoke a payload conversion function
        decoded_payload = eval(conversion_function_name)(input_base64)

        # Define the output of AWS Lambda function in case of successful decoding
        result = {
            "status": 200,
            "payload":  decoded_payload
        }
        logger.info(result)
        return result

    except Exception as exp:

        exception_type, exception_value, exception_traceback = sys.exc_info()
        traceback_string = traceback.format_exception(
            exception_type, exception_value, exception_traceback)

        # Define the output of AWS Lambda function in case of error

        exception_error_message = {
            "errorType": exception_type.__name__,
            "errorMessage": str(exception_value),
            "stackTrace": traceback_string
        }

        logger.error("Exception during execution, details :" %
                     json.dumps(exception_error_message))

        # Finish AWS lambda processing with an error
        raise exp