【问题标题】:AWS Lambda and SQL Server Integration with node.js codeAWS Lambda 和 SQL Server 与 node.js 代码的集成
【发布时间】:2019-08-01 19:11:59
【问题描述】:

您能告诉我,我使用此代码时遇到连接错误

aws lambda - 使用 nodejs 连接到 mssql

const sql = require('mssql');
exports.handler = (event, context, callback) => {
sql.connect({
    user: 'xxxxx',
    password: 'xxxx',
    server: 'xxxxxxxx.amazonaws.com',
    database: 'xxxxx',
    port: 1433,
},
    err => {
        // ... error checks
        if (err) {
            callback('connect failed', err);
        } else {
            callback(null,'connected');
        }
    })
}

【问题讨论】:

  • 您检查或尝试了什么?

标签: node.js sql-server amazon-ec2 aws-lambda aws-sdk


【解决方案1】:

试试这个:

const sql = require('mssql');

exports.handler = async event => {
  try {
    await sql.connect('mssql://username:password@server:port/database');
    return 'connected';

  } catch (err) {
    console.error('cannot connect', err.message);
  }
  return 'connect failed';
}

如果您看到错误,请执行 lambda 并检查 CloudWatch 日志。

【讨论】:

  • 代码没问题,但是当我在 awslambda 中托管这段代码时,每次它显示连接失败START RequestId: 25eb1c7b-79d4-4dbd-b869-03d167e08190 Version: $LATEST 2019-03-14T08:59:33.556Z 25eb1c7b-79d4-4dbd-b869-03d167e08190 cannot connect Failed to connect to xxxxxx.ap-south-1.rds.amazonaws.com:1433 in 15000ms END RequestId: 25eb1c7b-79d4-4dbd-b869-03d167e08190 REPORT RequestId: 25eb1c7b-79d4-4dbd-b869-03d167e08190 Duration: 15525.80 ms Billed Duration: 15600 ms Memory Size: 128 MB Max Memory Used: 47 MB
  • 我也有同样的错误,并尝试了 iam 策略和安全组。 Lambda 代码在本地运行 lambda 的同一个数据库上成功运行,但不是作为部署的函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 2023-01-05
  • 1970-01-01
  • 2020-06-14
相关资源
最近更新 更多