winston-cloudwatch icon indicating copy to clipboard operation
winston-cloudwatch copied to clipboard

Can we use it inside AWS Lambda?

Open cheshu21 opened this issue 3 years ago • 9 comments
trafficstars

I am trying to use Winston Cloudwatch inside my Aws Lambda function but My log stream and Log groups are not created. Could you please help me with this ?

cheshu21 avatar Jun 22 '22 12:06 cheshu21

Did you try running one of the examples?

This one has been specifically made fo AWS Lambda usage.

lazywithclass avatar Jun 27 '22 15:06 lazywithclass

hi @lazywithclass, im really new to winston

I'm really not sure what Ive been doing wrong. I've tried to follow the example that you provided. but I'm still unable to see the error log inside the cloudwatch. Am I missing something?

I really need your advice on this :( cause I don't see the error 1 or bye logging in cloudwatch

the context is coming from the lambda context, and the region is coming from the lambda env variable I import and use winston and winston-cloudwatch inside a file actually, and this is the code looks like. So I just call the info() function in my main file.

image

and this is what my main lambda file looks like image

and this is what Ive got inside cloudwatch image

using the example you provided from this one

harithzainudin avatar Sep 11 '22 10:09 harithzainudin

Hello, it's been years since I've used this module in production, expecially in Lambda: to reproduce the problem you're facing I should just write what's in your screenshots, right?

Thanks!

lazywithclass avatar Sep 11 '22 14:09 lazywithclass

Hello, it's been years since I've used this module in production, expecially in Lambda: to reproduce the problem you're facing I should just write what's in your screenshots, right?

Thanks!

Hi @lazywithclass , yup sure. thanks for the reply :) to make it easier.. let me copy and paste the code here, instead of you writing it back

and yes, to reproduce, you can just copy and paste the code back oh yes, to add on, I think you need to update the readme to include the correct roles as when I add in the error handler, I've got no enough permission

just need to add in this line of tag (I'm using serverless framework btw)

- Effect: "Allow"
  Action: "logs:DescribeLogStreams"
  Resource: "*"

the main lambda handler (async operation)

require("util").inspect.defaultOptions.depth = null;
const { info } = require("../utils/logger");

module.exports.lambda_handler = async (event, context) => {
  console.log("event", event);
  console.log("context", context);

  console.log("using winston - started");
  info(context);
  console.log("using winston - ended");
};

the info logger file

const util = require("util");
const winston = require("winston");
const WinstonCloudWatch = require("winston-cloudwatch");

function info(context) {
  const self = winston.add(
    new WinstonCloudWatch({
      name: "cloudwatch-testing",
      logGroupName: context.logGroupName,
      logStreamName: context.logStreamName,
      awsRegion: process.env.AWS_REGION,
      awsOptions: {
        awsRegion: process.env.AWS_REGION,
      },
    })
  );

  winston.error("1");
  const transport = self.transports.find(
    (t) => t.name === "cloudwatch-testing"
  );

  transport.kthxbye(function () {
    console.log("bye");
  });
}

from what Ive encounter also, because of lambda async process, the winston logger don't have enough time to finish and exit the process properly, hence it doesn't get call the callback properly. Ive tried to use promisify to promisify the kthxbye but no luck :/

Ive also have try to use const { once } = require("events"); and do await once(logger, "finish") but no luck also hm

do let me know if you need me to try anything or test anything. I'll surely try and help you with what I can and know :)

harithzainudin avatar Sep 11 '22 14:09 harithzainudin

@lazywithclass https://github.com/lazywithclass/winston-cloudwatch/issues/73#issuecomment-1247853468

owaisdrabu avatar Sep 15 '22 09:09 owaisdrabu

I can confirm that this does not work when deploying via Lambda. Unfortunately I deploy via Docker containers to Lambda therefore I am not able to see if there are any errors unless they are console logged which I have tried to do but have not seen any.

adamleemiller avatar Mar 26 '24 18:03 adamleemiller

It definitely could be used inside AWS Lambda, at some point I wrote a function to be used inside such a service (see an example https://github.com/lazywithclass/winston-cloudwatch/blob/09f8d4d3bfcef59703608e3cddfba0d6b039fd01/examples/flush-and-exit.js#L5), but I imagine something changed in the years and I don't have anymore time to work on this .

lazywithclass avatar Apr 26 '24 18:04 lazywithclass