0

I implemented a lambda function on AWS to create an Alexa Skill. I would like to use a https endpoint on another server with node.js. I have no concept on how to approach such a port.

This is my example code of the aws lambda function:

const skillBuilder = Alexa.SkillBuilders.custom();

exports.handler = skillBuilder
  .addRequestHandlers(
    LaunchRequestHandler,
    MyIntentHandler,
    TestIntentHandler,
    HelpIntentHandler,
    CancelAndStopIntentHandler,
    SessionEndedRequestHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();

and as an example, one of the handlers:

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  handle(handlerInput) {
    const speechText = 'Hello';
    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(speechText)
      .withSimpleCard('Welcome', speechText)
      .getResponse();
  },
};

I would need help regarding the node.js app and how to translate this lambda function. Thank you in advance

Ipsider
  • 553
  • 1
  • 7
  • 20
  • 1
    What exactly is your requirement? do you want to expose this Lambda as an https endpoint or do you want to host your own https endpoint in another sever.? – johndoe Aug 15 '18 at 12:43
  • I want to host my own https endpoint on another server. I would like to reuse as much as I can from my lambda function code. – Ipsider Aug 15 '18 at 12:54

2 Answers2

2

There are a few options, considering your response in the comments section:

1 - You can create an HTTPS endpoint in API Gateway. The endpoint will get the data received in the https call and forward to lambda, then it will wait the function execution and forward the response. You can find more information here (http://docs.aws.amazon.com/apigateway/latest/developerguide/);

2 - You can provide an https endpoint in a server that you control and can use the aws-sdk (Javascript, Java, Python, etc.) to make a call to lambda function. Like a http service running inside EC2. You can find more information here for node.js (https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/lambda-examples.html);

3 - Now you can make a call to an SQS queue and configure it to fire the lambda execution. The same solution is available using AWS Kinesis. The SQS Queue receive messages as https calls. But this solution probably will need a third party to send the response for the original caller. You can find more information about this option here (http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/).

Gustavo Tavares
  • 2,579
  • 15
  • 29
  • Thanks for the information. I will look into the sources, that is very useful. Another approach would be to just completely scrape lambda function and write my own node app and create the same json responses as the lambda function would do, right? I would like to run everything in my own cloud service. I was just thinking about a module, wich could be used also outside the lambda function, where I used ask-sdk-core. I was not sure whether I could just use this in a standalone node app on my own cloud server. Thank you for your input. – Ipsider Aug 16 '18 at 09:53
  • Certainly you can provide the lambda functionality in you own node.js app in a server. This alternative has his own advantages. But you must have a more clear goal definition. Lambda is a event-driven service, that you pay only for the execution time, but have a lot of limitations. It is very good and cheap if your Alexa skill usage is not heavy. Even if it is heavy maybe it will cost you less than keep a server running at all times. On other side, if your lambda use external modules and your already running app use this modules maybe make more sense use it as an extra endpoint in your app. – Gustavo Tavares Aug 16 '18 at 11:13
  • I suggest you try to learn more about Lambda in general. Here a good (an very brief) summary of the differences in using lambda and using your own app. (https://stackoverflow.com/questions/44709406/when-and-when-not-to-use-aws-lambda-functions). When you fell more secure, try using one or two lambdas with your application at the same time to get used to his advantages and limitations. I made this a long time ago and now I have some applications that used lambda for all functions and some that do not use lambda. When you are more experienced you will know when to use it without need to think. – Gustavo Tavares Aug 16 '18 at 11:19
1

Ok, sorry about this extra answer, but when I remembered I could'n stop thinking about how fool I were. So....

All AWS services were designed to be used primary as an https endpoint. The AWS Console, the aws-cli and all AWS-SDK's are just proxies to https calls. Knowing this you can make a simple https post request to invoke the lambda function. The API documentation for this request is here (https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html).

But it only appears simple... the post request to be made must be signed with your access and secret keys. If it it not signed, it will not be accepted by AWS. It's not a simple process (but you must implement it only a single time in a util function). The console, aws-cli and aws-sdk automatically sign the requests for you, so they must be your primary option.

Gustavo Tavares
  • 2,579
  • 15
  • 29