Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

javascript - Lambda将SQL传递到RDS数据API时出错(Error in Lambda Passing SQL to RDS Data API)

In following a tutorial, I am trying to set up an AWS Lambda function that will pass a SQL query to an AWS RDS Aurora Serverless MySQL database using the Data API and return the query results (presumably as a JSON).

(在遵循本教程的过程中,我尝试设置一个AWS Lambda函数,该函数将使用Data API将SQL查询传递给AWS RDS Aurora Serverless MySQL数据库,并返回查询结果(大概是JSON)。)

The code I used is below (where the params are stored as environment variables):

(我使用的代码如下(参数存储为环境变量):)

const AWS = require('aws-sdk')
const RDS = new AWS.RDSDataService()

exports.handler = async (event, context) => {
    console.log(JSON.stringify(event, null, 2))  // Log the entire event passed in

    // Get the sqlStatement string value
    // TODO: Implement a more secure way (e.g. "escaping") the string to avoid SQL injection
    var sqlStatement = event.sqlStatement;

    // The Lambda environment variables for the Aurora Cluster Arn, Database Name, and the AWS Secrets Arn hosting the master credentials of the serverless db
    var DBSecretsStoreArn = process.env.DBSecretsStoreArn;
    var DBAuroraClusterArn = process.env.DBAuroraClusterArn;
    var DatabaseName = process.env.DatabaseName;

    const params = {
      awsSecretStoreArn: DBSecretsStoreArn,
      dbClusterOrInstanceArn: DBAuroraClusterArn,
      sqlStatements: sqlStatement,
      database: DatabaseName
    }

    try {
      let dbResponse = await RDS.executeSql(params)
      console.log(JSON.stringify(dbResponse, null, 2))

      return JSON.stringify(dbResponse)

    } catch (error) {
        console.log(error)
      return error
    }
}

I run the following test from the Lambda console (where "Bonds" is the name of an existing table in my database):

(我从Lambda控制台(其中“ Bonds”是数据库中现有表的名称)运行以下测试:)

{
    "sqlStatement": "SELECT * FROM Bonds"
}

My test is logged as a success, with a blank output {} and the following error information logged:

(成功记录了我的测试,输出为{}并且记录了以下错误信息:)

INFO    TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Request'
    |     property 'response' -> object with constructor 'Response'
    --- property 'request' closes the circle
    at JSON.stringify (<anonymous>)
    at Runtime.exports.handler (/var/task/index.js:25:24)END

Does anyone know how I can successfully retrieve data with this method, and/or what the above error means?

(有谁知道我如何使用此方法成功检索数据,和/或上述错误是什么意思?)

  ask by OJT translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

RDS.executeSql(params) does not return a promise that you can await .

(RDS.executeSql(params)不会返回可以await的承诺。)

It simply constructs a request object for you.

(它只是为您构造一个请求对象。)

Replace it instead with await RDS.executeSql(params).promise() so you can get the value that you want.

(将其替换为await RDS.executeSql(params).promise()以便获得所需的值。)

References:

(参考文献:)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...