Search code examples
pythonamazon-web-servicesboto

Python/Boto - Writing to AWS CloudWatch Logs without sequence token


I am trying to send logs to AWS CloudWatch Logs using Python and Boto framework. I am doing this:

res=logs.put_log_events("FOO", "BAR",
     [{'timestamp':int(round(time.time() * 1000)),
       'message':time.strftime("%m/%d/%Y %H:%M:%S")+' Scheduled  monitoring check' }], 
     sequence_token=None)

I get an error each time I run:

boto.logs.exceptions.InvalidSequenceTokenException: InvalidSequenceTokenException: 400 Bad Request
{u'message': u'The given sequenceToken is invalid. The next expected sequenceToken is: 49540113336360065754596906019042392283494234157161146226', u'expectedSequenceToken': u'49540113336360065754596906019042392283494234157161146226', u'__type': u'InvalidSequenceTokenException'}

It is somewhat impractical for me to store that token. It makes no sense, why can't I just append to the log stream ?

How can I get around this ?


Solution

  • You can get around it by first looking up the uploadSequenceToken via describe_log_streams():

    Essentially, the process is that you use the logStreamNamePrefix to specifically identify the logstream that you want to append to. Then parse the uploadSequenceToken out of the response.

    Response Syntax

     {
         'logStreams': [
             {
                 'logStreamName': 'string',
                 'creationTime': 123,
                 'firstEventTimestamp': 123,
                 'lastEventTimestamp': 123,
                 'lastIngestionTime': 123,
                 'uploadSequenceToken': 'string',
                 'arn': 'string',
                 'storedBytes': 123
             },
         ],
         'nextToken': 'string'
     }
    

    Returns all the log streams that are associated with the specified log group. The list returned in the response is ASCII-sorted by log stream name.

    By default, this operation returns up to 50 log streams. If there are more log streams to list, the response would contain a nextToken value in the response body. You can also limit the number of log streams returned in the response by specifying the limit parameter in the request. This operation has a limit of five transactions per second, after which transactions are throttled.

    Request Syntax

    response = client.describe_log_streams(
        logGroupName='string',
        logStreamNamePrefix='string',
        orderBy='LogStreamName'|'LastEventTime',
        descending=True|False,
        nextToken='string',
        limit=123
    )