Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

566
Visualizações
Set LogStreamName for AWS Lambda call

We deploy node.js functions onto AWS Lambda. When calling them, they auto-generate an AWS CloudWatch log. The log group is set to the name of the Lambda function, that´s helpful. But the log stream is named like this: 2018/02/14/[$LATEST]794dbaf40a7846c4984ad80ebf110544.

AWS CloudWatch Log Streams

That is not helpful when searching for errors since I need to check multiple log streams, because I do not know which one is the correct one.

Is there any way to define the log stream name, so that it is more readable for a human being?

The node.js code looks similar to this:

exports.handler = function (event, context) {
    console.log('Called "' + context.functionName + '" with AWS-Request-Id "' + context.awsRequestId + '"');
    // do sth. here
};
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Check out the context of your function. It has property context.logStreamName. You could change it to any unique name.

Keep in mind, if you want to append your logs to existed stream, you have to persist its token. It was the reason why I created new log stream for each lambda call. Also, I am using guid for creating stream name (like: reason why log created + guid(), timestamp is ok as well).

Check more details in the next article - The Context Object (Node.js)

EDIT: Don't pay attention to my previous answer. It was completely wrong.

I checked my code how I implemented the same functionality. It isn't possible to change group or stream name from the context. But you could use CloudWatchLogs in your code for putting logs in specific group and stream.

My code:

var AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    // TODO implement
    var cloudwatchlogs = new AWS.CloudWatchLogs({ apiVersion: '2014-03-28' });

    // create new stream name for each request
    // because I don't persist sequenceToken
    var logStreamName = "myStreamName" + Math.random()

    var params = {
        logGroupName: 'myLogGroup',
        logStreamName: logStreamName
    };
    cloudwatchlogs.createLogStream(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else {
            var params = {
                logEvents: [{
                        message: 'log message',
                        timestamp: new Date().getTime()
                    },
                    {
                        message: 'one more log message',
                        timestamp: new Date().getTime()
                    }
                ],
                logGroupName: 'myLogGroup',
                logStreamName: logStreamName
            };

            cloudwatchlogs.putLogEvents(params, function(err, data) {
                if (err) console.log(err, err.stack); // an error occurred
                else console.log(data); // successful response
            });
        }
    });
    callback(null, 'Hello from Lambda');
};

Disadvantages of this approach are:

  • Your lambda creates two logGroups and two logStreams in accordance. One of your lambda, another custom. console.log('') writes to lambda log, putLogEvents writes to your custom log (i.e: myStreamName0.10141409975385463). Be aware of this.
  • You have to create manually 'myLogGroup' (how I did) or implement code that will create a logGroup if it doesn't exist.

The result of this testing code looks like: enter image description here

My productions stream names have more sense rather name + random. And they are using guid or timestamp as a suffix. I don't think that it is safe to use random.

over 4 years ago · Santiago Trujillo Relatório

0

You cannot change the Log Stream name. Each running container for your Lambda function logs into a different Log Stream.

If you want to easily see the logs together as one, you can stream them to Amazon ElasticSearch Service so you can use Kibana to browse through your logs or you can use CLI tools like awscli to aggregate and browse them in your terminal.

over 4 years ago · Santiago Trujillo Relatório

0

If you want to keep track of the executions anyway like in my case there is also a less redundant approach:

Just create a dynamodb table "Executions" and make sure to push a new entry on every lambda execution with both the logStreamName (which can be accessed from context) and your custom name or other custom information that you need to look up this logStream.

Upsides:

  • no wrapper/custom logging logic necessary.
  • no duplicate entries in cloudwatch

Downsides:

  • you might need to do an additional call to dynamodb to find out the logStreamName as the logStreamName itself has no identifying value
  • no considerations about IAM and access so far. you have to take that in mind in your setup
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda