Search code examples
node.jsamazon-web-servicesaws-lambdaalexa-skills-kitaws-cloud9

Lambda function works in AWS console but not on Cloud9


I'm making an Alexa skill and need to include the rest module for nodejs so I've changed from the AWS Console to cloud 9. In the console, everything works fine but when I create the project with the exact same setup (Not including the new module) I get following syntax error:

{
    "errorMessage": "Unexpected token )",
    "errorType": "SyntaxError",
    "stackTrace": [
        "    ^",
        "SyntaxError: Unexpected token )",
        "createScript (vm.js:56:10)",
        "Object.runInThisContext (vm.js:97:10)",
        "Module._compile (module.js:542:28)",
        "Object.Module._extensions..js (module.js:579:10)",
        "Module.load (module.js:487:32)",
        "tryModuleLoad (module.js:446:12)",
        "Function.Module._load (module.js:438:3)",
        "Module.require (module.js:497:17)",
        "require (internal/module.js:20:19)"
    ]
}

It doesn't tell me in which line the syntax error occurs and in the console with the exact same input it works fine.

I've already tried reducing my code to only include a launchRequestHandler in my index.js as such:

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
    },
    handle(handlerInput) {
        var reprompt = '';
        const speakOutput = 'Start';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(reprompt)
            .withShouldEndSession(false)
            .getResponse();
    },
};
const skillBuilder = Alexa.SkillBuilders.custom();

exports.handler = skillBuilder
    .addRequestHandlers(
        LaunchRequestHandler,
    )
    .addErrorHandlers(ErrorHandler)
    .lambda();

I've also tried changing the package.json to what it was in the console and have created it new with npm init but neither makes a difference. What am I doing wrong? Is there something missing?

My template.yml looks like this:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
  protocollFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: protocollFunction/index.handler
      Runtime: nodejs6.10
      Description: ''
      MemorySize: 128
      Timeout: 15
      Events:
        LambdaMicroservice:
          Type: Api
          Properties:
            Path: /
            Method: ANY
  protocollFunctionPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName:
        'Fn::GetAtt':
          - protocollFunction
          - Arn
      Principal: apigateway.amazonaws.com
      SourceArn:
        'Fn::Sub': 'arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:*/*/*/*'

Solution

  • I found the error, apparently, the cloud9 compiler is more sensitive than the AWS console. The , behind LaunchRequestHandler in the exports.handler was interpreted as an error. After removing this it works. Hope this will help others that come across a problem like this.