Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The specified log group already exists error for second run #1

Open
alexlai86 opened this issue Oct 12, 2021 · 0 comments
Open

The specified log group already exists error for second run #1

alexlai86 opened this issue Oct 12, 2021 · 0 comments

Comments

@alexlai86
Copy link

alexlai86 commented Oct 12, 2021

Hi Mr. GeorgeDavis,

Thanks for the guide you provide. It is very useful. However, I face some issue for the lambda coding.

On the first run of a job, it creates the log stream and everything is fine. However, on subsequent jobs (update event log on next minutes) I get the error that the "The specified log group already exists", even though this job was the one that created it. The error as follows:

[ERROR] ResourceAlreadyExistsException: An error occurred (ResourceAlreadyExistsException) when calling the CreateLogGroup operation: The specified log group already exists
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 86, in lambda_handler
createLogGroup(logGroupName)
File "/var/task/lambda_function.py", line 12, in createLogGroup
createLogGroupResponse = cwlogs.create_log_group(
File "/var/runtime/botocore/client.py", line 386, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 705, in _make_api_call
raise error_class(parsed_response, operation_name)

The lambda code I used is as ur doc:

import os
import boto3
import string
import random
import json
import time

cwlogs = boto3.client('logs')

def createLogGroup(logGroupName):
createLogGroupResponse = cwlogs.create_log_group(
logGroupName=logGroupName,
tags={
"Owner": "TrendMicro",
"Product": "CloudOneWorkloadSecurity",
"Name": logGroupName
}
)

print("createLogGroupResponse - " + str(createLogGroupResponse))

def ifLogGroupExists(logGroupName):
listLogGroupsResponse = cwlogs.describe_log_groups()
for logGroup in listLogGroupsResponse["logGroups"]:
if logGroupName in logGroup["logGroupName"]:
return True
return False

def createLogStream(logGroupName, logStreamName):
createLogStreamResponse = cwlogs.create_log_stream(
logGroupName=logGroupName,
logStreamName=logStreamName
)

print("createLogStreamResponse - " + str(createLogStreamResponse))

if createLogStreamResponse["ResponseMetadata"]["HTTPStatusCode"] == 200:
    return logStreamName
return ""

def putLogEvents(logGroupName, logStreamName, logEvent, nextSequenceToken=None):

if nextSequenceToken:
    putLogEventsResponse = cwlogs.put_log_events(
        logGroupName=logGroupName,
        logStreamName=logStreamName,
        logEvents=logEvent,
        sequenceToken=nextSequenceToken
    )
else:
    putLogEventsResponse = cwlogs.put_log_events(
        logGroupName=logGroupName,
        logStreamName=logStreamName,
        logEvents=logEvent
    )

print("putLogEventsResponse - " + str(putLogEventsResponse))

if "rejectedLogEventsInfo" not in putLogEventsResponse:
    return putLogEventsResponse["nextSequenceToken"]
return None

def lambda_handler(event, context):

nextSequenceToken = None

logGroupName = str(os.environ.get("CloudWatchLogGroupName"))
logStreamNamePrefix = str(os.environ.get("CloudWatchLogStreamNamePrefix"))

nonce = ''.join(random.choices(
    string.ascii_letters + string.digits, k=8)).upper()

logEvents = json.loads(event["Records"][0]["Sns"]["Message"])

if len(logEvents) > 0:

    if ifLogGroupExists(logGroupName):
        logStreamName = createLogStream(
            logGroupName, logStreamNamePrefix + "-" + nonce)
    else:
        createLogGroup(logGroupName)
        logStreamName = createLogStream(
            logGroupName, logStreamNamePrefix + "-" + nonce)

    print(str(logGroupName) + " - " + str(logStreamName))

    for message in logEvents:

        epoch_time = int(round(time.time() * 1000))

        logEvent = []
        logEvent.append({"timestamp": epoch_time, "message": str(message)})

        nextSequenceToken = putLogEvents(
            logGroupName, logStreamName, logEvent, nextSequenceToken)

        if nextSequenceToken == None:
            return False

Regards,
Alex Lai (alex.lai@ecloudvalley.com)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant