Last active
August 24, 2022 17:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.