Skip to content

Instantly share code, notes, and snippets.

@mobilequickie
Last active August 24, 2022 17:51
Show Gist options
  • Save mobilequickie/c3d15acd186a7a19cc4e7b7cef4550d4 to your computer and use it in GitHub Desktop.
Save mobilequickie/c3d15acd186a7a19cc4e7b7cef4550d4 to your computer and use it in GitHub Desktop.
AWS Lambda function (Node 8) using the RDSDataService API to connect to an Aurora Serverless Data API enabled MySQL database
// As of April 25, 2019 --> Make sure to call $ 'npm install aws-sdk' to package this function before deploying to Lambda as the RDSDataService API is currently in BETA and
// therefore not available in the default aws-sdk included in the Node 8 engine built into Lambda.
// This code uses RDSDataService API for connecting to a Data API enabled Aurora Serverless database: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/RDSDataService.html
// Call this function with: { "sqlStatement": "SELECT * FROM <YOUR-TABLE-NAME"}
// Deploy this Lambda function via CloudFormation here: https://github.com/mobilequickie/rds-aurora-mysql-serverless
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
}
}
@PriyaJainDev
Copy link

Hi @mobilequickie. I am trying out a similar approach with the help of your code. But I am getting an error 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>)

To resolve the same, I tried using await rdsdataservice.executeStatement(params).promise(), but then adding the promise(), results in script execution failure. The test does not pass.

Any help to resolve the same?

@patrickagm
Copy link

patrickagm commented May 10, 2021

I'm with the same problem:

circular structure to JSON --> starting at object with constructor 'Request' | property 'response' -> object with constructor 'Response' --- property 'request' closes the circle at JSON.stringify ()

EDIT:

I got it.

  const result = await RDS.executeStatement(params, function (err, data) {
    if (err) {
      // error
      console.log(err)
      callback('Query Failed')
    } else {
      console.log("data :: ", data)
      console.log("data.records :: ", data.records)

      callback(null, data.records)
    }
  }).promise()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment