Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we use it inside AWS Lambda? #193

Open
cheshu21 opened this issue Jun 22, 2022 · 8 comments
Open

Can we use it inside AWS Lambda? #193

cheshu21 opened this issue Jun 22, 2022 · 8 comments

Comments

@cheshu21
Copy link

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 ?

@lazywithclass
Copy link
Owner

lazywithclass commented Jun 27, 2022

Did you try running one of the examples?

This one has been specifically made fo AWS Lambda usage.

@harithzainudin
Copy link

harithzainudin commented Sep 11, 2022

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

@lazywithclass
Copy link
Owner

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!

@harithzainudin
Copy link

harithzainudin commented Sep 11, 2022

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 :)

@owaisdrabu
Copy link

@adamleemiller
Copy link

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.

@lazywithclass
Copy link
Owner

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

name: 'using-kthxbye',
), but I imagine something changed in the years and I don't have anymore time to work on this .

@GeorgeCr
Copy link

GeorgeCr commented May 29, 2024

@cheshu21
After days of crying over my lambda...
It's hacky, but it does the job. I've done this trying to build this transport from scratch, inspired by this one.
So you can override the lib with this hack or build it yourself and use it.

async log(info: any, callback: any) {
    setImmediate(() => {
      this.emit('logged', info);
    });
    const command3 = new PutLogEventsCommand({
     //config
    });
    console.log('command3', command3);
    try {
      await new Promise((resolve) => setTimeout(resolve, 0));
      const result = await client.send(command);
    } catch (err) {
      console.log('failed');
    }
    callback();
  }

This makes that the process doesn't exit and the logger does not finish the job. Adding a timeout to the event loop makes it so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants