1 Answer
- Newest
- Most votes
- Most comments
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.
Relevant content
asked 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