-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat - implemented retrieve cloudwatch logs function
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import boto3 | ||
|
||
|
||
# Initialize the CloudWatch Logs client | ||
client = boto3.client("logs") | ||
|
||
|
||
def retrieve_cloudwatch_logs(function_app_id: str): | ||
log_group_name = f"/aws/lambda/app-{function_app_id}" | ||
|
||
# Describe log streams to get the newest log stream | ||
response = client.describe_log_streams( | ||
logGroupName=log_group_name, orderBy="lastEventTimestamp", descending=True, limit=1 | ||
) | ||
|
||
if "logStreams" in response and len(response["logStreams"]) > 0: | ||
newest_log_stream = response["logStreams"][0]["logStreamName"] | ||
|
||
# Get log events of the newest log stream | ||
log_events_response = client.get_log_events( | ||
logGroupName=log_group_name, logStreamName=newest_log_stream | ||
) | ||
if "events" in log_events_response: | ||
response_data = {} | ||
list_of_events_messages = [] | ||
for event in log_events_response["events"]: | ||
list_of_events_messages.append(event["message"]) | ||
|
||
response_data["message"] = "Log events found in the newest log stream" | ||
response_data["data"] = list_of_events_messages | ||
return response_data | ||
else: | ||
return "No log events found in the newest log stream" | ||
else: | ||
return "No log streams found in the log group." |