How To Send Tomcat Logs To S3 Bucket From EC2 Instance

We can send and store tomcat logs to the s3 bucket. We can also use a shell script to autorotate logs to s3. Let’s see, how to send tomcat logs to the s3 bucket.

How To Send Tomcat Logs To S3 Storage Bucket From EC2 Instance:

We can configure S3cmd to send the tomcat logs to the S3 bucket and store them. you can also apply the policy on the bucket to move the logs after a few days to the other storage. 

Check Out: How To List Git Remote Branches of Repository From Github

Method 1:

I’m going to configure AWS CLI for Linux. you can download the package using the below command. 

curl "" -o ""

We’ll unzip the file using unzip command.


You will see the AWS folder where you did unzip it and run the install script.

[root@tomcat ~]# ./aws/install
You can now run: /usr/local/bin/aws –version

Once the installed script finished. you can check the version using the below command.

[root@tomcat ~]# /usr/local/bin/aws --version
aws-cli/2.0.31 Python/3.7.3 Linux/4.14.181-108.257.amzn1.x86_64 botocore/2.0.0dev35

Check Out: How To Force Quit Application Running on Windows 8/10

Method 2:

Install the S3cmd tool on your Linux machine using the below commands.

yum install epel-release -y
yum install s3cmd -y

Once you have installed it, we need to create a role for the S3 bucket to give access.

Create a Role:

Go to the IAM service -> Click on “roles” -> then click on “Create roles”.

We have to select the EC2 service.

Click on “Next: Permission” and search for “AmazonS3FullAccess

how to send tomcat logs to s3 bucket

Click on “Tag” and give a name.

Check Out: How To Create Build Using AWS CodeBuild Service

create iam role for s3 bucket

Click on “Review” and give a role name “tomcatlogs3”.

shell script to send logs to s3

Click on “Create role” and check it.

Now we have to attach this role to the EC2 instance, so select the “instance” -> click on “Action” -> Select “Instance settings” -> click on “Attach/Replace IAM role”.

send logs to s3 bucket

Select your role in the drop-down and click on “apply”.

tomcat logs to s3

Create a bucket on S3 storage. I have already created with “tomcatlog1” and also created a folder inside its name “node3”.

store logs to s3

Now we will configure Cron’s job to send the logs to the S3 Storage. I’m assuming you have already configured the Tomcat instance.

Check Out: Troubleshoot Linux Server Performance Using vmstat Command

Create a folder with the below command on your server.

mkdir -p /var/www/cron

Now run the below command to configure it.

s3cmd sync /opt/apache-tomcat-7.0.96/logs/* s3://tomcatlog1/node3/
chmod +x

Set the Cron:

We can use cron to autorotate the logs to s3 Bucket.

crontab -e
30 10 * * * /usr/bin/sh /var/www/cron/

Run the script with sh or ./

 log send to s3

Check the logs on the S3 bucket.

autorotate logs to s3

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment