AWS Lambda无法对外部API进行REST调用 [英] AWS Lambda not able to make REST call to external API

查看:153
本文介绍了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: 'https://api.google.com/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?检查此 http://docs.aws.amazon.com/lambda/latest /dg/vpc.html 您需要授予对其的外部访问权限

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

这篇关于AWS Lambda无法对外部API进行REST调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆