AWS Serverless Lambda function that sends log data from CloudWatch Logs and S3.
Follow this article in Youtube
-
We will need the following pre-requisites to successfully complete this activity,
S3 Bucket
- Update the bucket with the policy mentioned below- IAM Role
serverless-cw-to-s3-exporter-role
- Get Help for setting up IAM Role- Managed Permissions:
AWSLambdaExecute
- To allow Lambda to log events & Write to S3 Buckets - Inline Policy:
ListBucketPolicy
mentioned below
- Managed Permissions:
- CloudWatch Log Group Name[s]
-
To receive CloudWatch Logs exports the following bucket policy should be applied.
{ "Version": "2008-10-17", "Id": "Policy1335892530063", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "logs.YOUR-REGION.amazonaws.com" }, "Action": "s3:GetBucketAcl", "Resource": "arn:aws:s3:::YOUR-BUCKET-NAME" }, { "Effect": "Allow", "Principal": { "Service": "logs.YOUR-REGION.amazonaws.com" }, "Action": "s3:PutObject", "Resource": "arn:aws:s3:::YOUR-BUCKET-NAME/*", "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control" } } } ] }
-
Create a new IAM Policy
ListBucketPolicy
with the following configuration. If you want, restrict it only to the buckets of your interests, in resources section. A policy to allow the script to verify valid S3 buckets{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:ListBucket", "s3:HeadBucket" ], "Resource": "*" } ] }
-
- Provide the function name & choose
Python 3.7
- Attach the IAM Role -
serverless-cw-to-s3-exporter-role
- Get code here
Save
the lambda function
- Provide the function name & choose
-
Goto the Cloudwatch Dashboard, We are going to use
Event Rules
- Choose
Create a new Rule
- Choose
Schedule
- Get Help - For
Target
, ChooseLambda Function
- From dropdown select your Lambda Function Name
- In the bottom,
Configure Details
- Fill the
Rule Name
&Rule Description
- Make sure it is Enabled
Enable
Trigger byChecking
the box- Click
Save
Now your lambda function should be triggered based on your schedule
- Choose
-
You can test it manually or set the cloudwatch events to trigger closest to the current UTC time
Increase your lambda runtime as necessary depending about the amount of data to move. Perhaps run it inside step functions.
Python 3.x
This Lambda function has the following limitation:
- Currently If you want to export all log groups, you have to list them all manually. This is by design, as the export tasks are limited to
1 PENDING
task at any time by AWS, and Lambda have limited time to push all the logs.
Buy me a coffee β through Paypal, or You can reach out to get more details through here.
[1] - AWS Docs - Export Log Data to Amazon S3
[2] - CloudWatch - Schedule Expressions for Rules
Level: 200