I have some Lambda functions that use console.log
for logging. I want to modify the logging functionality without changing the Lambda code. The idea is to intercept messages sent to the console with console.log()
.
If I use the Lambda layer with the code below, I can add require('/opt/logging.js');
at the beginning of the Lambda index file. It works, but the problem is that I still should modify the Lambda code.
const originalConsole = console.log;
console.log('init start');
console.log = function (msg) {
originalConsole.apply(this, [`Test Logging Layer ${Date.now()}: ${msg}`]);
};
console.log('init executed');
I've decided to use Lambda Internal Extension. The documentation says that an internal extension runs as part of the runtime process.
So, I've added NODE_OPTIONS: --require /opt/logging.js
environment variable to the Lambda. The logs I've got during the testing process you can see below:
As I see it, the extension works, but the extension and Lambda have different console objects.
The question is, why the same code works okay with require('/opt/logging.js');
but doesn't work with NODE_OPTIONS
. Maybe you can help me with this and propose some other options that I can use.
Lambda internal extensions run in the same process but in different threads ( here: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-extensions-api.html )
the console log theoretically is thread-safe.
To clarify your example situation, what is happening is that you are extending the log method in a single thread and this will not be reflected to other threads except when you add require.
In the same intentional scenario, I propose the use of a node package instead of using layers. this is hard to trace and local debugging will be hard for a simple scenario, also compared to a packaging solution there is no real advantage.