Inconsistent console logs for NodeJS worker threads in AWS Lambda

Viewed 52

I have an lambda function which spins up a worker thread.

hello-world.js:

const { Worker } = require('node:worker_threads')

export const hello = async (event, context) => {
  console.log('Hello World')
  const w = new Worker('./build/other-file.js')
  return new Promise(resolve => {
    w.on('exit', () => {
      console.log('Goodbye')
      resolve()
    })
  })
}

other-file.js:

console.log('Blah Blah')

Defined in serverless.yml as:

functions:
  helloWorld:
    handler: build/hello-world.hello
    maximumRetryAttempts: 0

If I invoke this locally with serverless, I get what I'd expect:

15:40:59 $ sls invoke local --function helloWorld
Using serverless-localstack
Hello World
Blah Blah
Goodbye

If I invoke it on AWS each message from the function itself is also prefixed with a timestamp and an execution ID. This is very nice. However, it doesn't do this for messages from the worker thread.

15:44:00 $ aws lambda invoke --function-name sheetbuilder-dev-helloWorld out --log-type Tail --query 'LogResult' --output text | base64 -d
START RequestId: ebf33295-c59e-4182-a5b0-d436210f5e6f Version: $LATEST
2022-09-05T14:51:48.833Z    ebf33295-c59e-4182-a5b0-d436210f5e6f    INFO    Hello World
Blah Blah
2022-09-05T14:51:48.973Z    ebf33295-c59e-4182-a5b0-d436210f5e6f    INFO    Goodbye
END RequestId: ebf33295-c59e-4182-a5b0-d436210f5e6f
REPORT RequestId: ebf33295-c59e-4182-a5b0-d436210f5e6f  Duration: 154.59 ms Billed Duration: 155 ms Memory Size: 1024 MB    Max Memory Used: 73 MB  Init Duration: 192.28 ms    
15:51:49 $ 

This also matches what I get if I look at the logs in Cloudwatch

Hence my questions are:

  • Why's there a difference between the default output and the worker thread output?
  • Is it possible / easy to make the worker thread output match the main output? (or at the very least include timestamp/execution ID)
  • Is it possible to also make the serverless local output consistent?
0 Answers
Related