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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…