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 "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

We’ll unzip the file using unzip command.

unzip awscliv2.zip

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

[[email protected] ~]# ./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.

[[email protected] ~]# /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.

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

Set the Cron:

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

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

Run the script with sh log.sh or ./log.sh

 log send to s3

Check the logs on the S3 bucket.

autorotate logs to s3

That’s it.


Please enter your comment!
Please enter your name here