forked from aws-samples/amazon-rds-init-cdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (63 loc) · 1.91 KB
/
index.js
File metadata and controls
72 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const mysql = require('mysql2')
const { SecretsManagerClient, GetSecretValueCommand } = require('@aws-sdk/client-secrets-manager')
require('dotenv').config();
// the env AWS_ENDPOINT_URL is automatically injected and available
const endpoint = process.env.AWS_ENDPOINT_URL;
const url = new URL(endpoint);
const hostname = url.hostname;
// configure the secretsmanager to connect to the running LocalStack instance
const secrets = new SecretsManagerClient({
endpoint: endpoint,
credentials: {
accessKeyId: 'test',
secretAccessKey: 'test'
},
region: 'us-east-1'
})
// the function expects "secretName" and "sqlQuery" as payload
// sample call using aws-cli:
// $ awslocal lambda invoke --function-name my-lambda-rds-query-helper --payload '{"sqlQuery": "select Author from books", "secretName":"/rdsinitexample/rds/creds/mysql-01"}' output
// the result is in the 'output' file:
// $ cat output
exports.handler = async (event, context) => {
try {
const { password, username, dbname, port } = await getSecretValue(event.secretName)
const connection = mysql.createConnection({
host: hostname,
user: username,
database: dbname,
port,
password,
multipleStatements: true
})
connection.connect()
const res = await query(connection, event.sqlQuery)
return {
status: 'SUCCESS',
results: res
}
} catch (err) {
return {
status: 'ERROR',
err,
message: err.message
}
}
}
function query (connection, sql) {
return new Promise((resolve, reject) => {
connection.query(sql, (error, res) => {
if (error) return reject(error)
return resolve(res)
})
})
}
async function getSecretValue (secretId) {
try {
const command = new GetSecretValueCommand({ SecretId: secretId })
const response = await secrets.send(command)
return JSON.parse(response.SecretString)
} catch (error) {
throw error
}
}