AWS – Cloudwatch Logs

aws cloudwatch

Set up AWS cloud watch Logs:

  1. 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
  2. Download AWS cloud watch logs agent setup file:

    wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
  3. 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
  4. 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:

  1. 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.
  2. To see diagnostic information for the CloudWatch Logs Agent, see:

     /var/log/awslogs.log
  3. You can rerun interactive setup using:
    sudo python ./awslogs-agent-setup.py --region us-west-2 --only-generate-config
  4. 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.Screen Shot 2016-07-15 at 6.57.08 pmScreen Shot 2016-07-15 at 6.57.34 pmScreen Shot 2016-07-15 at 6.57.48 pm

Uninstall aws cloud watch agent and configurations:

  1. 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
  2. Its done. Restart the server if required.

Troubleshoot:

  1. 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:

  1. http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/QuickStartEC2Instance.html
  2. http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/AgentReference.html
  3. http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.

Be the first to comment on "AWS – Cloudwatch Logs"

Leave a comment

Your email address will not be published.


*


10 Shares
Tweet
Pin1
Share4
Share5
Share
%d bloggers like this: