• Classes
  • 11 - Logging

AWS Logs

In AWS Lambda, the logs generated by your Lambda functions are stored in Amazon CloudWatch Logs.

Info!

CloudWatch Logs is a fully managed service provided by AWS that allows you to collect, monitor, and store logs from various AWS services, including Lambda.

Question 1

Use the codes from previous classes to search for Lambda functions available in our AWS account.

Choose a function created by you and write the name of the function!

Then, to search and display the lambda function logs, use:

Provide function name

Change the function_name variable in the following source code.

import os
import boto3
from dotenv import load_dotenv

load_dotenv()

# Provide any function name.
function_name = ""
lambda_group_name = f"/aws/lambda/{function_name}"

# Initialize the AWS SDK
session = boto3.Session(
    aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
    aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
    region_name=os.getenv("AWS_REGION"),
)

# Create a CloudWatch Logs client
logs_client = session.client("logs")

# Retrieve the log streams for the Lambda function
response = logs_client.describe_log_streams(
    logGroupName=lambda_group_name
)

# Select the desired log stream (e.g., the latest stream)
log_stream_name = response["logStreams"][0]["logStreamName"]

# Retrieve the log events from the selected log stream
log_events = logs_client.get_log_events(
    logGroupName=lambda_group_name,
    logStreamName=log_stream_name,
)

# Process the log events
for i, event in enumerate(log_events["events"]):
    # Print the log event
    print(event["message"])

    # Print a separator between log events
    if event != len(log_events["events"]) - 1:
        print("-" * 60)
        print(f"LOG {i+1}:")

    # Print the log message
    print(event["message"])

Warning!

You will probably see some Exceptions, but they are just text messages of exceptions that occurred when executing the lambda function in the past, not from the log query code!