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.
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.
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
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“
Click on “Tag” and give a name.
Click on “Review” and give a role name “tomcatlogs3”.
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”.
Select your role in the drop-down and click on “apply”.
Create a bucket on S3 storage. I have already created with “tomcatlog1” and also created a folder inside its name “node3”.
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.
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
Check the logs on the S3 bucket.