chrisp
chrisp

Reputation: 2249

How can I setup sam-cli to launch an Alexa skills lambda for testing?

I can't seem to figure out how to get a functioning Alexa skills lambda function running locally. Using ngrok Amazon properly connects to my running sam container, but all I get is a POST / that results in a 404.

I copied a sample from the AWSLABS sam repo for Alexa:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Demonstrates a basic skill built with the Amazon Alexa Skills Kit.
Parameters: 
  TopicNameParameter: 
    Type: String
Resources:
  alexaskillskitcolorexpert:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10
      CodeUri: .
      Description: Demonstrates a basic skill built with the Amazon Alexa Skills Kit.
      MemorySize: 128
      Timeout: 3
      Policies:
        - SNSPublishMessagePolicy:
            TopicName: !Ref TopicNameParameter
      Events:
        AlexaSkillEvent:
          Type: AlexaSkill

Upvotes: 1

Views: 427

Answers (1)

Sébastien Stormacq
Sébastien Stormacq

Reputation: 14905

Alexa Skills can connect to two different types of backend. It can be an AWS Lambda Function or any HTTPS exposed web services. You are trying to mix both, this is why your sample code is not working.

When a skill is hosted as an HTTPS endpoint, it has to provide extra code that is not required when using AWS Lambda inetgration with Alexa Skills. For example, the Alexa service will send signed requests over HTTP POST that your skill must reply to. On the other hand, when hosted as an AWS Lambda function, there is no signature required, as the AWS Lambda trigger mechanism ensures that only the Alexa Service can call your skill code.

The example you downloaded exposes the code as an AWS Lambda function and creates a Lambda trigger to authorise the Alexa service to call your function. This is the line here :

Events:
        AlexaSkillEvent:
          Type: AlexaSkill

But when you are exposing your code running locally through ngrok, you are switching to the other model : custom HTTPS endpoints. So, the Alexa service expects your code to answer the POST requests it is sending over which the example does not do.

I would suggest to not use ngrok to test your Alexa skill exposed as a AWS Lambda function. Only use ngrok if you plan to test code to be deployed as an Alexa Skill custom HTTPS endpoint.

To test your code locally, you can use unit test frameworks, such as mocha for NodeJS. The idea is to capture the requests sent by Alexa in the Alexa developer console or your Lambda logs and to replay it locally on your laptop.

Here is an example I wrote to test my skill using this technique : https://github.com/sebsto/maxi80-alexa/tree/master/lambda/test

You also can use third-party testing frameworks, such as bespoken. Here is another example I wrote to show how to use bespoken for local unit test : https://github.com/alexa/skill-sample-nodejs-audio-player/tree/mainline/single-stream/test/unit

Upvotes: 2

Related Questions