对SQL Server的AWS Lambda NodeJS调用不返回任何数据且没有错误 [英] AWS Lambda NodeJS call to SQL Server returns no data and no errors

查看:104
本文介绍了对SQL Server的AWS Lambda NodeJS调用不返回任何数据且没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用lambda函数连接到AWS托管的SQL Server,但是当我在lambda管理控制台中或通过API调用对其进行测试时,我会一直通过该函数来获得的最终响应在这里测试

I am trying to connect to an AWS hosted SQL server with a lambda function, but when I test it in the lambda management console, or via an API call I get passed all the way through the function to get the final response of test here

这似乎表明呼叫未击中任何错误,也未返回任何记录.因为我知道表中有数据,所以我至少应该期望有一个错误,或者理想情况下是数据.我该如何解决这个问题?

This seems to indicate that the call hit no errors and returned no records. Since I know the table has data I would have at least expected an error or ideally, data. How can I troubleshoot this?

使用CloudWatch控制台输出,我看到称为rdsquery",但此后一无所获

using CloudWatch console output I see 'called rdsquery' but nothing after that point

var sql = require("mssql");

// config for your database
var config = {
    user: 'xxuser',
    password: 'xxxx',
    server: 'mydns', 
    database: 'Antonio' 
};


module.exports.rdsquery = async event => {

console.log('called rdsquery')
  try{
    // connect to your database
    await sql.connect(config, function (err) {

      console.log('connected')
        if (err) 
          console.log('rdsquery: '+err)
          //return {statusCode: 200, headers: {'Access-Control-Allow-Origin': '*'},body: JSON.stringify('sql connect err: '+err)};

        // create Request object
        var request = new sql.Request();
        console.log('rdsquery: request crated')   
        // query to the database and get the records
        request.query('select CustomerNo from FROMMAS_AR_Customer', function (err, recordset) {

            if (err)
            console.log('rdsquery-sql: '+err)   
            //return {statusCode: 200, headers: {'Access-Control-Allow-Origin': '*'},body: JSON.stringify('sql err: '+err)};

            // send records as a response
            console.log('logging recordset')
            console.log(recordset);
            return {statusCode: 200, headers: {'Access-Control-Allow-Origin': '*'},body: JSON.stringify(recordset)};
            //return {statusCode: 200, headers: {'Access-Control-Allow-Origin': '*'},body: JSON.stringify(recordset)};
        });
    });
  }
  catch(e)
  {
    console.log('rdsquery-catch: '+e)   
    return {statusCode: 200, headers: {'Access-Control-Allow-Origin': '*'},body: JSON.stringify('ERR: '+e)};
  }

  //return {statusCode: 200, headers: {'Access-Control-Allow-Origin': '*'},body: JSON.stringify('test here')};
};

推荐答案

请注意,我不是Lambda专家.这些信息中的任何一个都可能是完全错误的.如果您发现与我如何解决问题有关的问题,请在下面发表评论

因此,在进行大量的挖掘之后,我发现我的lambda配置存在一些问题:

So after a lot of digging I found that I had several problems with my lambda configuration:

  1. 默认情况下,我的Lambda函数超时太短.当我将超时时间增加到30秒时,我开始在lambda内运行测试时收到有用的错误,例如无法连接MYIP .

然后我在SO上找到了此答案,这使我向Lambda函数添加了VPC(因此函数可以访问RDS)

Then I found this answer on SO which had me add a VPC to my lambda function (so the function could access RDS)

最后,我必须将策略 AWSLambdaVPCAccessExecutionRole 添加到我的lambda用户.

Finally I had to add the policy AWSLambdaVPCAccessExecutionRole to my lambda user.

这篇关于对SQL Server的AWS Lambda NodeJS调用不返回任何数据且没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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