Set up AWS cloud watch Logs:
- Configure your IAM role or user for Cloud Watch Logs:
Cloud Watch uses the Identity and Access Management (IAM) service for authentication and authorization so follow step:1 in below AWS doc link.http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/QuickStartEC2Instance.html
- Download AWS cloud watch logs agent setup file:
wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
- Run AWS cloud watch agent setup file for configuration:
sudo python ./awslogs-agent-setup.py --region us-west-2
It will ask for AWS access key and secret key to continue the setup so provide that information when prompted. Then it will ask for log detail for the file path and other configuration for logging file to aws cloud watch.
below is the sample parameters where app-application-logs will be the group name in aws cloud watch and will be the file path on the server from where all logs will go to cloud watch.
/var/www/html/application/logs/*.*
[/var/www/html/application/logs/*.*] datetime_format = %Y-%m-%d %H:%M:%S file = /var/www/html/application/logs/*.* buffer_duration = 5000 log_stream_name = {instance_id} initial_position = start_of_file log_group_name = app-application-logs
- Setup and configuration will be done once above process get completed and AWS cloud watch agent will continue writing specified log files to AWS cloud watch.
Note: Configuration file path: /var/awslogs/etc/awslogs.conf
You can begin accessing new log events after a few moments at
https://console.aws.amazon.com/cloudwatch/home?region=us-west-2#logs:
Controlling aws cloud watch:
- Control aws cloud watch daemon.
sudo service awslogs stop #To stop aws log.
sudo service awslogs start #To start aws log.
sudo service awslogs restart #To restart awslog. - To see diagnostic information for the CloudWatch Logs Agent, see:
/var/log/awslogs.log
- You can rerun interactive setup using:
sudo python ./awslogs-agent-setup.py --region us-west-2 --only-generate-config
- You can also set the expiration dates
sudo python ./awslogs-agent-setup.py --region us-west-2 --only-generate-config
for the log for aws cloudwatch Log group. so suppose if expiration is set to 5 days then logs older than 5 days will be deleted from cloudwatch Logs.
Uninstall aws cloud watch agent and configurations:
- Run below commands to remove related files and uninstall cloud watch setup and agent:
#Stop the awslogs service. service awslogs stop #Remove cloud watch related files. rm -rf /var/awslogs rm -rf /etc/init.d/awslogs rm -rf /home/ubuntu/awslogs-agent-setup.py
- Its done. Restart the server if required.
Troubleshoot:
- If any issues comes like Trying to install CloudWatch Agent “Failed to create virtualenv” during setup installation then run below command:
export LC_ALL=C
(It forces applications to use the default language for output, and forces sorting to be bytewise)
References:
- http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/QuickStartEC2Instance.html
- http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/AgentReference.html
- http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html