How to run my Alexa skill build Python code into aws Lambda as a container?

0

I successfully launched an Alexa Smart Home Skill within an AWS Python Lambda in a container. https://github.com/MelleD/haaska-tailscale

A json response was always returned in the evenHandler method. This works so far.

def event_handler(event, context):
...
ha = HomeAssistant(config)

return ha.post('alexa/smart_home', event, wait=True)

See entry point and docker file https://github.com/MelleD/haaska-tailscale/blob/main/Dockerfile#L32 https://github.com/MelleD/haaska-tailscale/blob/main/haaska/haaska.py#L113

Now I'm trying the same thing with the Skill Builder as in the instructions here: I found an example for Python here. https://developer.amazon.com/en-US/docs/alexa/custom-skills/handle-requests-sent-by-alexa.html#do-skill-id-verification-in-code

Here is the SkillBuilder: https://github.com/keatontaylor/alexa-actions/blob/master/lambda/lambda_function.py#L646

However, I don't understand how I should design the even handler method now?

I tried: I successfully launched an Alexa Smart Home Skill within an AWS Python Lambda in a container. https://github.com/MelleD/haaska-tailscale

A json response was always returned in the evenHandler method. This works so far.

def event_handler(event, context): ... ha = HomeAssistant(config)

return ha.post('alexa/smart_home', event, wait=True) See entry point and docker file https://github.com/MelleD/haaska-tailscale/blob/main/Dockerfile#L32 https://github.com/MelleD/haaska-tailscale/blob/main/haaska/haaska.py#L113

Now I'm trying the same thing with the Skill Builder as in the instructions here: I found an example for Python here. https://developer.amazon.com/en-US/docs/alexa/custom-skills/handle-requests-sent-by-alexa.html#do-skill-id-verification-in-code

Here is the SkillBuilder: https://github.com/keatontaylor/alexa-actions/blob/master/lambda/lambda_function.py#L646

However, I don't understand how I should design the even handler method now?

I tried:

    def event_handler(event, context):
    """
    The SkillBuilder object acts as the entry point for your skill, routing all request and response
    payloads to the handlers above. Make sure any new handlers or interceptors you've
    defined are included below.
    The order matters - they're processed top to bottom.
    """
    sb = SkillBuilder()
    logger.info("Add SkillBuilder to the lambda handler.")
    # register request / intent handlers
    sb.add_request_handler(LaunchRequestHandler())
    sb.add_request_handler(YesIntentHandler())
    sb.add_request_handler(NoIntentHandler())
    sb.add_request_handler(StringIntentHandler())
    sb.add_request_handler(SelectIntentHandler())
    sb.add_request_handler(NumericIntentHandler())
    sb.add_request_handler(DurationIntentHandler())
    sb.add_request_handler(DateTimeIntentHandler())
    sb.add_request_handler(CancelOrStopIntentHandler())
    sb.add_request_handler(SessionEndedRequestHandler())
    sb.add_request_handler(IntentReflectorHandler())

    # register exception handlers
    sb.add_exception_handler(CatchAllExceptionHandler())

    # register response interceptors
    sb.add_global_request_interceptor(LocalizationInterceptor())

    lambda_handler = sb.lambda_handler()
    logger.info("End SkillBuilder to the lambda handler.")
    return lambda_handler

Error is:

2024-04-24T20:54:17.255+02:00
LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
[ERROR] Runtime.MarshalError: Unable to marshal response: Object of type function is not JSON serializable
Traceback (most recent call last):

Is this still needed or do I not need an evenHandler function or what does the entry point look like in Docker and what is the response?

EntryPoint is here: https://github.com/MelleD/alexa-actions-tailscale/blob/main/Dockerfile#L39

Since I'm not a expert with AWS Lambda with Alexa Skills, maybe someone can tell me what I did wrong with the handler. I guess I need a different return value.

Also tried to add just a lambda_function.py with directly the SkillBuilder like in the example but then the error is

[ERROR] Runtime.MalformedHandlerName: Bad handler 'lambda_function': not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
MelleD
asked 10 days ago86 views
3 Answers
0
Accepted Answer

I think you should write all this code in the top level and do not declare a function and do not return anything Try the following:

"""
The SkillBuilder object acts as the entry point for your skill, routing all request and response
payloads to the handlers above. Make sure any new handlers or interceptors you've
defined are included below.
The order matters - they're processed top to bottom.
"""
sb = SkillBuilder()
logger.info("Add SkillBuilder to the lambda handler.")
# register request / intent handlers
sb.add_request_handler(LaunchRequestHandler())
sb.add_request_handler(YesIntentHandler())
sb.add_request_handler(NoIntentHandler())
sb.add_request_handler(StringIntentHandler())
sb.add_request_handler(SelectIntentHandler())
sb.add_request_handler(NumericIntentHandler())
sb.add_request_handler(DurationIntentHandler())
sb.add_request_handler(DateTimeIntentHandler())
sb.add_request_handler(CancelOrStopIntentHandler())
sb.add_request_handler(SessionEndedRequestHandler())
sb.add_request_handler(IntentReflectorHandler())

# register exception handlers
sb.add_exception_handler(CatchAllExceptionHandler())

# register response interceptors
sb.add_global_request_interceptor(LocalizationInterceptor())

lambda_handler = sb.lambda_handler()
logger.info("End SkillBuilder to the lambda handler.")

The above code will define the handler that will be invoked when needed.

profile pictureAWS
EXPERT
Uri
answered 9 days ago
  • Your CMD should be ["lambda_function.lambda_handler"] where lambda_function is the name of the py file that contains the above code.

0

Hey,

thanks for the quick answer. I tried it saw my last comment. Sorry was a little bit confusing. If I do it on top level I get this error and also renamed the file directly to lambda_function.py

[ERROR] Runtime.MalformedHandlerName: Bad handler 'lambda_function': not enough values to unpack (expected 2, got 1) Traceback (most recent call last):

Entry point is: CMD [ "lambda_function" ]

MelleD
answered 9 days ago
0

@Uri you are right it works with top level and ["lambda_function.lambda_handler"].

Thanks a lot :)

MelleD
answered 9 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions