Reputation: 384
I have some Nodejs functions which contain duplicated code blocks. I know that we can use lambda layer for shared libraries but i wanted to know that, is it possible to share codes too? for example one of duplicated code is shown below
const connection = mysql.createPool({
host: process.env.DB_HOST,
database: process.env.DB_NAME,
user: process.env.DB_USER,
port: process.env.DB_PORT,
password: process.env.DB_PASSWORD
});
i want to import this block into multiple functions without writing it over and over
Upvotes: 2
Views: 1100
Reputation: 482
One option is to create a separate Lambda with all of the shared code. Then you can invoke the shared Lambda from within all the other lambdas, or use a "Main" lambda to handle the back/forth.
Upvotes: 2
Reputation: 1973
In addition to using layers (which is probably an overkill for sharing code only), you also have two other routers you can take:
lambda1.js
, lambda2,js
and shared.js
). You will then need to package your lambda1
as a zip file containing lambda1.js
and shared.js
for example.alllambda.js
) and have different handler functions configured (exports.handler1
, exports.handler2
) - when you configure your AWS Lambda functions, Lambda1
has Handler
configured as handler1
and Lambda2
has handler2
. This obviously does not scale well (and has the extra downside of sharing the initialization code).Upvotes: 2