I have a lambda function that writes metrics to Cloudwatch. While, it writes metrics, It generates some logs in a log-group.
INFO:: username: simran+test@abc.co
You can achieve this with the cloudWatchlogs client and a little bit of coding. You can also customize the conditions or use JSON module for a precise result.
EDIT
You can use describe_log_streams to get the streams. If you want only the latest, just put limit 1, or if you want more than one, use for loop to iterate all streams while filtering as mentioned below.
import boto3
client = boto3.client('logs')
## For the latest
stream_response = client.describe_log_streams(
logGroupName="/aws/lambda/lambdaFnName", # Can be dynamic
orderBy='LastEventTime', # For the latest events
limit=1 # the last latest event, if you just want one
)
latestlogStreamName = stream_response["logStreams"]["logStreamName"]
response = client.get_log_events(
logGroupName="/aws/lambda/lambdaFnName",
logStreamName=latestlogStreamName,
startTime=12345678,
endTime=12345678,
)
for event in response["events"]:
if event["message"]["ClinicID"] == "7667":
print(event["message"])
elif event["message"]["username"] == "simran+test@abc.com":
print(event["message"])
#.
#.
# more if or else conditions
## For more than one Streams, e.g. latest 5
stream_response = client.describe_log_streams(
logGroupName="/aws/lambda/lambdaFnName", # Can be dynamic
orderBy='LastEventTime', # For the latest events
limit=5
)
for log_stream in stream_response["logStreams"]:
latestlogStreamName = log_stream["logStreamName"]
response = client.get_log_events(
logGroupName="/aws/lambda/lambdaFnName",
logStreamName=latestlogStreamName,
startTime=12345678,
endTime=12345678,
)
## For example, you want to search "ClinicID=7667", can be dynamic
for event in response["events"]:
if event["message"]["ClinicID"] == "7667":
print(event["message"])
elif event["message"]["username"] == "simran+test@abc.com":
print(event["message"])
#.
#.
# more if or else conditions
Let me know how it goes.