Send EC2 Application Logs to AWS CloudWatch using Boto3 in Python

  • Last updated Apr 25, 2024

In this tutorial, we will show you how to send application logs to AWS CloudWatch using Boto3 in Python.

Follow these steps, to implement this solution:

  1. Installing Boto3:
  2. Boto3 is a Software Development Kit for Python, maintained by Amazon Web Services. Install the latest version of Boto3 S3 SDK using the following command:

    pip install boto3
  3. Sending Logs to AWS CloudWatch:
  4. The following is a Python code for sending application logs to AWS CloudWatch:

    import boto3
    import time
    import json
    
    client = boto3.client('logs', region_name='us-east-1', aws_access_key_id=ACCESS_KEY,
                                   aws_secret_access_key=ACCESS_SECRET)
    
    def send_log(log_group_name, log_stream_name, log_message):
        #Getting last sequence token
        response = client.describe_log_streams(logGroupName=log_group_name,
                                               logStreamNamePrefix=log_stream_name)
    
        log_event = {
            'logGroupName': log_group_name,
            'logStreamName': log_stream_name,
            'logEvents': [
                {
                    'timestamp': int(round(time.time() * 1000)),
                    'message': log_message
                },
            ],
        }
    
        #Adding last sequence token to log event before sending logs if it exists
        if 'uploadSequenceToken' in response['logStreams'][0]:
            log_event.update(
                {'sequenceToken': response['logStreams'][0]['uploadSequenceToken']})
    
        print("logs to send : ", log_event)
        response = client.put_log_events(**log_event)
        time.sleep(1)
        print("Response : ", response)
    
    #Create log group and log stream from AWS Console and replace the name below
    LOG_GROUP_NAME = "test-log-group"
    LOG_STREAM_NAME = "test-log-stream"
    
    log_message = json.dumps({"message": "This is a sample log"})
    
    send_log(LOG_GROUP_NAME, LOG_STREAM_NAME, log_message)