send logs to cloudwatch

How To Send Logs and Configure Cloudwatch Agent On Linux

We can send logs to cloudwatch using awslogs agent and we need to configure cloudwatch agent with log configuration on AWS EC2. you will have to install cloudwatch agent on EC2 instances.

We have to install awslogs package on EC2 instance and also create a log group on cloudwatch aws where we can send the logs according to the project. You can see the logs on the cloudwatch dashboard. Once you have configured you can also send the logs to s3 bucket to keep for a period of time and later move to another storage bucket.

Check Out: How To Install Themes on WordPress 5.4 On Linux

You have to create an IAM role and attach it to the EC2 instances. Open the IAM dashboard service.

Now select roles -> create role -> then select EC2

Click on the Next permissions. After this, select the policy

And these two policies as well.

Then click on add tag.

Check Out: Install and Configure AWS CLI Version 2 Tool On Windows

Review the final then click on create a role

Now click on the role policy which you have created then select the attached policy.

how to configure awslogs on ec2

Click on Create policy, it will open in the new tab and select JSON then put these code.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
      "logs:CreateLogGroup",
      "logs:CreateLogStream",
      "logs:PutLogEvents",
      "logs:DescribeLogStreams"
],
      "Resource": [
      "*"
       ]
     }
  ]
}

Click on review policy

send application logs to cloudwatch

If you think everything Is fine, click on create policy.

Check Out: How To Change Default Torrent Program on Ubuntu

Now return to your roles which u have created and click on add policies and attach the policy.

how to send logs to cloudwatch

Now attach the IAM roles to the EC2 instances:

Select instances -> Action -> Instances settings -> Attach IAM roles

cloudwatch logs configure

Once you click on it. Select the roles and apply them.

how to send logs

We’ve finished the IAM role here and now it’s time to install cloud agents on EC2 instances.

Install the AWS logs agent on the Linux machine.

[[email protected] ~]# yum install awslogs -y

Start and enable the service using the below commands

[[email protected] ~]# service awslogs start
Starting awslogs: [ OK ]
[[email protected] ~]# chkconfig awslogs on

You can use this command too systemctl status awslogsd on rhel 7 or 8.

Check Out: How To Install Numix Circle Icon Theme On Ubuntu 20.04

AWS Logs Configuration:

Note: In my case, The region will be pointed to us-east-1 which is where the server launched. If you need a different region, you can edit the awscli.conf file.

[[email protected] awslogs]# cat awscli.conf
[plugins]
cwlogs = cwlogs
[default]
region = us-east-1

Go to the location cd /etc/awslogs/ and open the awslogs.conf. I have configured the logs for Messages and apache access logs. Similarly, you configure any logs.

[/var/log/messages]
datetime_format = %b %d %H:%M:%S
file = /var/log/messages
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /var/log/messages

[/var/log/httpd/access_log]
datetime_format = %b %d %H:%M:%S
file = /var/log/messages
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /var/log/httpd/access_log

Restart the awslogs service using the below commands.

[[email protected] awslogs]# service awslogs restart
stopping awslogs
Stopping awslogs: [ OK ]
Starting awslogs: [ OK ]

Verify the logs on Cloudwatch:

You will see the logs section when you click on the Cloudwatch service.

Check Out: Copy Database To Another Database Using SQLYog On Ubuntu

Click on Logs -> Then choose logs Group. You can see the logs files here which you have configured on the server.

aws log configuration

Now if you want to check the logs then click on any logs file.

install cloudwatch agent

Once you have selected the log file you will be seeing the logs.

configure awslogs

You’re done configure awslogs install cloudwatch agent

LEAVE A REPLY

Please enter your comment!
Please enter your name here