willfarrell / middy-rds Goto Github PK
View Code? Open in Web Editor NEWMiddy middleware for creating a db connection using knex
License: MIT License
Middy middleware for creating a db connection using knex
License: MIT License
I'm trying to integrate this with @middy/secrets-manager, but setting internalData isn't working.
My DB conn is stored in secrets manager like:
{
"host": "***.rds.amazonaws.com",
"port": 3306,
"user": "***",
"password": "***",
"database": "***"
}
Handler is:
const handler = middy()
.use(
secretsManager({
fetchData: {
dbConn: 'SECRET_NAME',
},
}),
)
.use(
rdsMiddleware({
client: knex,
config: {
client: 'mysql2',
},
internalData: 'dbConn',
}),
)
.handler(async (event, context) => {
const { sql } = context;
// ...
});
Issue is options.config.connection ends up being:
{
"dbConn": {
"host": "***.rds.amazonaws.com",
"port": 3306,
"user": "***",
"password": "***",
"database": "***"
},
"ssl": { ... }
}
Expected:
{
"host": "***.rds.amazonaws.com",
"port": 3306,
"user": "***",
"password": "***",
"database": "***",
"ssl": { ... }
}
Lines 24 to 29 in 2e58349
If I change line 28 to this it works:
...values[options.internalData]
I'm getting the error ""Dynamic require of "crypto" is not supported" when I try to use the middy-rds middleware in a JS module file (".mjs" extension). Is there a way around this, or am I stuck having to use this middlware only in ".js" and ".cjs" files? Here's the full stack trace for context.
{
"errorType": "Error",
"errorMessage": "Dynamic require of \"crypto\" is not supported",
"stack": [
"Error: Dynamic require of \"crypto\" is not supported",
" at file:///var/task/node_modules/middy-rds/clients/pg.mjs:12:9",
" at node_modules/uuid/dist/rng.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:2451:42)",
" at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
" at node_modules/uuid/dist/v1.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:2536:39)",
" at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
" at node_modules/uuid/dist/index.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:2919:37)",
" at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
" at node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:3027:18)",
" at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
" at node_modules/@aws-sdk/middleware-retry/dist-cjs/AdaptiveRetryStrategy.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:3135:35)"
]
}
I think, it was a mistake in the code for the knex part.
At the example, all configuration (including database port) are inside options.config.connection
. But, in the knex file, We have this mistake :
Lines 30 to 32 in 2622737
To avoid that, I have to set the port
in options.config
and options.config.connection
In /var/runtime/bootstrap
it contains:
# If NODE_EXTRA_CA_CERTS is being set by the customer, don't override. Else, include RDS CA
if [ -z "${NODE_EXTRA_CA_CERTS+set}" ];
then
export NODE_EXTRA_CA_CERTS="/etc/pki/tls/certs/ca-bundle.crt"
fi
Changing the line to await request.context.db.destroy();
resolves the problem.
Setting cacheExpiry
to 0
triggers the error: "Cannot read property 'destroy' of undefined",
This occurs on line:
Line 46 in 88d755c
Although the before middleware copies the db in the context, the afterMiddleware does not seems to be able to contain this connection.
The beforeMiddleware code:
Line 42 in 88d755c
Below is the code I used for local testing (e.g. with lambda-node):
const mainHandler = middy(baseHandler).use(logger({
awsContext: true
})).use(normalizer({
canonical: true
})).use(jsonBodyParser())
.use(httpErrorHandler());
const connection = {
database: getEnvVar("dbDatabase"),
password: getEnvVar("dbPassword"),
host: getEnvVar("dbHost"),
user: getEnvVar("dbUsername"),
port: getEnvVar("dbPort") || 5432,
ssl: false
}
handler = mainHandler.use(rds({
cacheExpiry: 0,
client: knex,
config: {
client: "pg",
connection
}
}));
In the index file is a typo, where const sl
must be const ssl
Pull request created: #2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.