AWS Lambda无法对外部API进行REST调用

编程入门 行业动态 更新时间:2024-10-12 01:22:23
本文介绍了AWS Lambda无法对外部API进行REST调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用nodeJS代码使用请求模块进行rest调用。我还使用了回调函数,但请求函数未得到执行。

I am using the nodeJS code to make a rest call using request module. I have also used callback function but the request function is not getting executed.

我的流程转到了searchTSTData函数,但请求方法未得到执行。

My flow goes to function searchTSTData but the request method is not getting executed.

从回调函数中,我只得到了我在searchTSTData函数中初始化的responseString ='Yet to make query rest'。

From the callback function I am only getting responseString = 'Yet to make query rest' which I have initialized in searchTSTData function. It's not getting updated based on the response returned by API which should be either error or success response string.

我已经将模块包含在zip中,因为lambda不会引发错误,并且不会根据API返回的响应进行更新。

I have included the modules in zip as lambda is not throwing error and passes test. Also I am sure that request module is not working as in Cloudwatch logs I don't see any console.logs i wrote inside request.

请建议在哪里我做错了吗?我是NodeJS的新手。

Please suggest where did I go wrong. I am new to NodeJS.

这是代码-

'use strict'; const request = require('request'); const Alexa = require('alexa-sdk'); const APP_ID = 'amzn1.ask.skill.80a49cf5-254c-123a-a456-98745asd21456'; const languageStrings = { 'en': { translation: { TST: [ 'A year on Mercury is just 88 days long.', ], SKILL_NAME: 'TEST', GET_TST_MESSAGE: "Here's your TST: You searched for ", HELP_MESSAGE: 'You can say get me a TST, or, you can say exit... What can I help you with?', HELP_REPROMPT: 'What can I help you with?', STOP_MESSAGE: 'Goodbye!', }, }, }; const handlers = { 'LaunchRequest': function () { this.emit('GetTST'); }, 'GetNewTSTIntent': function () { this.emit('GetTST'); }, 'GetTST': function () { // Get a random space fact from the space facts list // Use this.t() to get corresponding language data const inputValue = this.event.request.intent.slots.Search.value; var finalResponse = "Some error occurred in code. Please try again later."; console.log('Input recieved as '+inputValue); searchTSTData(inputValue, function (response){ console.log('trying to call'); finalResponse = response; }); console.log("after function call"); // Create speech output const speechOutput = this.t('GET_TST_MESSAGE') + inputValue+". Here are the results " +finalResponse; this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), speechOutput); }, 'AMAZON.HelpIntent': function () { const speechOutput = this.t('HELP_MESSAGE'); const reprompt = this.t('HELP_MESSAGE'); this.emit(':ask', speechOutput, reprompt); }, 'AMAZON.CancelIntent': function () { this.emit(':tell', this.t('STOP_MESSAGE')); }, 'AMAZON.StopIntent': function () { this.emit(':tell', this.t('STOP_MESSAGE')); }, }; exports.handler = function (event, context) { const alexa = Alexa.handler(event, context); alexa.APP_ID = APP_ID; // To enable string internationalization (i18n) features, set a resources object. alexa.resources = languageStrings; alexa.registerHandlers(handlers); alexa.execute(); }; function searchTSTData(searchString,callback){ var responseString = 'Yet to make query rest'; request({ url: 'api.google/getresultsInJson', method: 'GET' }, function (error, response, body) { if (error) { responseString = 'Error received from rest api. Please try again after some time.'; } else if(response.statusCode===200){ responseString = 'Sucess Success'; }else{ responseString = 'Nothing is working'; } }); callback(responseString); }

推荐答案

是您的lambda方法VPC?检查此 docs.aws.amazon/lambda/latest /dg/vpc.html 您需要授予对其的外部访问权限

is your lambda method inside a VPC? check this docs.aws.amazon/lambda/latest/dg/vpc.html you need to give external access to it

更多推荐

AWS Lambda无法对外部API进行REST调用

本文发布于:2023-11-28 02:53:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Lambda   AWS   REST   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!