Automate! Export of Cloudwatch Logs to S3 Bucket Using Lambda with Eventbridge Trigger
Tags: cloudwatchlogs, s3bucket, lambda, eventbridgerule
Published: 2022-10-20 18:35:29

“ I have checked the documents of AWS to export cloudwatch logs to s3 bucket automatically. So I found that it can be possible using lambda functions with eventbridge rules. Pricing of S3, lambda and trigger set is very cheap in comparison to storing the logs in cloudwatch.”

Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. With Lambda, you can run code for virtually any type of application or backend service. To learn more, read the [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html).

Amazon EventBridge is a serverless event bus service that you can use to connect your applications with data from a variety of sources. EventBridge delivers a stream of real-time data from your applications, software as a service (SaaS) applications, and AWS services to targets such as AWS Lambda functions, HTTP invocation endpoints using API destinations, or event buses in other AWS accounts.

In this post, you will get to know how to automate the export of cloudwatch logs to s3 bucket using lambda with eventbridge trigger. Here I have used a lambda function with eventbridge trigger on it so that the cloudwatch logs get exported to s3 bucket as per schedule on trigger when run every time which automates the process.

#Prerequisites
You’ll need an Amazon RDS Database for this post. [Getting started with RDS](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.html) provides instructions on how to create a RDS. For this blog, I assume that I have a rds database.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/354v6z6y2kghw7tk0qgq.png)
The architecture diagram shows the overall deployment architecture with data flow, amazon RDS, cloudwatch logs, s3 bucket, eventbridge rule, lambda function.

#Solution overview
The blog post consists of the following phases:
1. Create of Option Group and Parameter Group with Required Values to Get the Logs in Cloudwatch
2. Create a Bucket in S3 with Custom Bucket Policy and IAM Role with Required Permission Policy
3. Create Lambda Function with Eventbridge Trigger
4. Output of Logs in S3 Bucket and Log Stream in Cloudwatch

I have a rds database as below →
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vb3d2s66u9w65lut863.png)

##Phase 1: Create of Option Group and Parameter Group with Required Values to Get the Logs in Cloudwatch
1. Open the option group console, create an option group with add option for query parameter. Open the parameter group, create a parameter group with values for parameters for log enabled to cloudwatch. Also modify the rds with the created option and parameter group. Also can check the log groups in cloudwatch console.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrxwzxu3z079sz12dbbx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9lybsno42u3zsjb9imn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwrzwgpknz1g130tlr8c.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h33u4xwm9ucdvj4xsurp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jq94ymm9ua1ojhohcdrf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocpwjkpbs0gtla1lf9o2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ojq4so222wz04iqo95m1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpwhars78o7jtpwhi3mt.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfjeq8e0t4jmy18o3iss.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfpowifwvtdub3050zcp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykwk2lbhucdtjmxeriq8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o21cg3cik5q158kbr3hk.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0iekz1cudf353lumhh25.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/216p43o1i6qqc7p1njrl.png)

##Phase 2: Create a Bucket in S3 with Custom Bucket Policy and IAM Role with Required Permission Policy
1. Open the S3 console, create a bucket with a custom bucket policy. Open the IAM console, create an IAM role for lambda service with AWS managed permission policy.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3gjcrki6e38qxom7rb5.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0mle1rx6bywe3ssefvg.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7s3cgnsgkhaipz672nr1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mrhmju3xlcoz0z0mpiua.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf9ooehv8b5fa3cx2n8z.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9me3426hhzulu5is1v4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g2xc0ahxu0gbtjfv2rh1.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pj7opji7nezs1nklsts.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jasotrqk0bpobs6ak2d3.png)

##Phase 3: Create Lambda Function with Eventbridge Trigger
1. Open the lambda console, create a lambda function with environment variables and required custom parameter values. Open the eventbridge console, create a rule for target as lambda function to run every 5 min to export the cloudwatch logs to s3 bucket.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b2iymtncvioc3dqaate9.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90vzrxtpdd3w4ngafj25.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z8ibejhw3iw8malbmeu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbvcnubufkitg6m6cpkn.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/deyo7ruhcofze84k1r9r.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jhv87x6mmjxdv1ir6ubc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvan0rmr552ycgng6jm8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9zoi7iphfxs3m09pv0g5.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swyrzduyd9g24rb96er8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omylnapwaq8lus9rdu4a.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/etqqiegqv44tq0x470e6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0ild63rpa3kn9th7rm1.png)

##Phase 4: Output of Logs in S3 Bucket and Log Stream in Cloudwatch
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hr50m6qyyx4cqzzornfs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aubdc6s6sgek5bgditou.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5i5rtrgy3fnwdykx1dcx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7nxz8wz49ngqqry3js18.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8b0l8lfyq65p3n1z4pvw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a12oyah543x2m6tw7174.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/60pmxukgw0ks0yki60a2.png)

#Clean-up
Delete S3 Bucket, IAM Role, Lambda Function, EventBridge Rule, RDS Database, Cloudwatch Log groups.

#Pricing
I review the pricing and estimated cost of this example. 

Cost of RDS = $0.10

Cost of Cloudwatch = $0.0

Cost of Lambda = $0.0

Cost of Simple Storage Service = $0.009

Total Cost = $0.109

#Summary
In this post, I showed “how to automate the export of cloudwatch logs to s3 bucket using lambda with eventbridge trigger”.

For more details on Amazon EventBridge Rules, Checkout Get started Amazon EventBridge Rules, open the [Amazon EventBridge console](https://us-east-1.console.aws.amazon.com/events/home?region=us-east-1#/). To learn more, read the [Amazon EventBridge documentation](https://docs.aws.amazon.com/eventbridge/?id=docs_gateway).

For more details on AWS Lambda, Checkout Get started AWS Lambda, open the [AWS Lambda console](https://ap-south-1.console.aws.amazon.com/lambda/home?region=ap-south-1#/discover). To learn more, read the [AWS Lambda documentation](https://docs.aws.amazon.com/lambda/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)