Skip to content

Instantly share code, notes, and snippets.

@olegon
Created February 19, 2024 00:04
Show Gist options
  • Save olegon/143ade2e03b9ddc9bd35c0450c70fc79 to your computer and use it in GitHub Desktop.
Save olegon/143ade2e03b9ddc9bd35c0450c70fc79 to your computer and use it in GitHub Desktop.
AWS Lambda that uses boto3 to send logs to many log groups.
import time
import boto3
# docs: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html
client = boto3.client('logs')
def get_current_timestamp():
return int(time.time() * 1000)
def lambda_handler(event, context):
print(f'event = {event}')
print(f'context = {context}')
logGroups = [
'/experiment/lambda-send-logs-to-many-cloud-watch-log-groups/group-01',
'/experiment/lambda-send-logs-to-many-cloud-watch-log-groups/group-02',
'/experiment/lambda-send-logs-to-many-cloud-watch-log-groups/group-03'
]
for logGroupName in logGroups:
logStreamName = f'{logGroupName}-{get_current_timestamp()}'
create_log_stream_response = client.create_log_stream(
logGroupName=logGroupName,
logStreamName=logStreamName
)
print(f'create_log_stream_response = {create_log_stream_response}')
put_logs_events_response = client.put_log_events(
logGroupName=logGroupName,
logStreamName=logStreamName,
logEvents=[
{
'timestamp': get_current_timestamp(),
'message': f'Log enviado para o log group {logGroupName}'
},
]
)
print(f'put_logs_events_response = {put_logs_events_response}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment