This lambda function adds a retention policy to CloudWatch log group[s] that does not have an retention policy. You can use the override feature to set new values on existing policies, Say for example you have decided to retain logs longer now, from 14 to 21 days.
Follow this article in Youtube
- AWS CLI pre-configured
- Log Retention Days: Defaults to 14 days
-
git clone https://github.com/miztiik/serverless-cloudwatch-log-retention.git
-
Edit the
./helper_scripts/deploy.sh
to update your environment variables.AWS_PROFILE="default" BUCKET_NAME="sam-templates-011" # bucket must exist in the SAME region the deployment is taking place TEMPLATE_NAME="serverless-cloudwatch-log-retention-policy.yaml" STACK_NAME="set-cloudwatch-logs-retention" OUTPUT_DIR="./outputs/" PACKAGED_OUTPUT_TEMPLATE="${OUTPUT_DIR}${STACK_NAME}-packaged-template.yaml"
For the
RETENTION_IN_DAYS
, you can customize this in the code in./src/cw-log-retention.py
or post deployment in the Lambda environment variable. -
We will use the
deploy.sh
in thehelper_scripts
directory to deploy our AWS SAM templatechmod +x ./helper_scripts/deploy.sh ./helper_scripts/deploy.sh
-
The function will automatically trigger once every 24 hours via a CloudWatch Event. Here is an example of the output from the Lambda function,
{ "status": true, "logs_metadata": [ { "logGroupName": "/aws/lambda/set-cloudwatch-logs-reten-CloudWatchLogRetentionFu-1SABSGG9H6XV1", "retentionInDays": 5, "region": "us-east-1" } ], "total_policies_set": 1 }
Buy me a coffee ☕ here https://paypal.me/valaxy
, or You can reach out to get more details through here.
Level: 200