Skip to content

Issue with EventBridge Rule Triggering SSM Document

0

Title: Issue with EventBridge Rule Triggering SSM Document

Description:
I am trying to create an EventBridge rule that triggers whenever a new CloudWatch Log Group is created. The triggered rule should execute an SSM Document that updates the log group's retention policy to 5 years.

Problem:

Although the rule is being triggered, the SSM Document does not seem to receive the event properly. The automation output shows the following: { "ExecutionLog": "Received Event: {}", "Payload": { "event": {} } }

Eventbridge rule:

{ "source": ["aws.logs"], "detail-type": ["AWS API Call via CloudTrail"], "detail": { "eventName": ["CreateLogGroup"] } }

Target: Target Type: AWS Systems Manager Automation Document Action: Updates the retention policy of the newly created log group.

1 Answer
0

Have you thought about using lambda than SSM document?

import boto3
import json
import os

# Default retention period in days (e.g., 14 days)
DEFAULT_RETENTION_DAYS = int(os.getenv("RETENTION_DAYS", 14))

def lambda_handler(event, context):
    logs_client = boto3.client('logs')

    # Extract log group name from the event
    log_group_name = event['detail']['requestParameters']['logGroupName']

    try:
        # Set the retention policy
        logs_client.put_retention_policy(
            logGroupName=log_group_name,
            retentionInDays=DEFAULT_RETENTION_DAYS
        )
        print(f"Retention policy set to {DEFAULT_RETENTION_DAYS} days for log group: {log_group_name}")
    except Exception as e:
        print(f"Error setting retention policy for {log_group_name}: {str(e)}")

Environment Variables You can set an environment variable RETENTION_DAYS for the Lambda function to control the retention period dynamically.

Permissions for the Lambda Function Attach the following IAM permissions to the Lambda function's execution role:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:PutRetentionPolicy"
      ],
      "Resource": "*"
    }
  ]
}

Enable CloudTrail Logging (if Not Already Enabled)

Ensure that CloudTrail is enabled and configured to log CreateLogGroup API events.

Go to the CloudTrail console. Ensure you have a trail enabled that logs Management events. Verify that the Event read/write type is set to Write-only or All.

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Hi, given your description, I had the same thought as Gary ... before seeing his answer. So, yes, I'd also go for a Lambda if the SSM implementation has a bug. You can open a ticket on your current use case while implementing a Lambda in the meantime. Best, Didier

  • yes I did. I am already using more than 5000 Lambda functions, but the searching option is very slow. While this option works, it depends on the Python version. If something is deprecated, I have to update it.

    I have also used AWS Config Compliance, but it has a disadvantage: it always enforces compliance for CloudWatch log groups with a retention period set to "never expire." config rule + ssm document config custom rule is not in option(it also use lambda)

    Additionally, using cron with bash scripts is not an option.

  • Have you tried using a step function instead? I tested yesterday and I recieved all the details from eventbridge

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.