How to: Set-up and configure AWS CloudWatch Logs

Michael Walsh
By Michael WalshPublished

A lesser known feature of the Amazon Web Services platform is the ability to stream server logs into shared storage in CloudWatch.

Storing server logs in CloudWatch is useful as it provides a means to retain logs long after an instance has been and gone. Furthermore, it allows us to configure log metrics, which can trigger alarms based on the volume of certain HTTP status codes or other log events within a fixed period of time. So for example an email notification can be triggered if a certain number of 404 or 500 HTTP status codes occur within a set period of time.

The AWS documentation covers the simple steps to install the AWS Logs Agent on your server instances.  Setting up each log file to be streamed to CloudWatch Logs is very simple and at the end of the Agent installation you can configure one or more log files to stream.

As a starting point we often stream the following log files:

  • /var/log/syslog
  • /var/log/auth.log

In addition, we have our Nginx and PHP error logs streaming and it’s even possible to configure Varnish logs to be streamed too.

The only part that needs a little thought is configuring the timestamp format, so here are a few helpers, it does depend on your log configuration of course but here’s what worked for us:

Nginx log timestamp format:

  • Example timestamp: 2015/01/11 12:53:19
  • Timestamp format: %Y/%m/%d %H:%M:%S

System / auth log timestamp format:

  • Example timestamp: Jan 23 20:59:29
  • Timestamp format: %b %d %H:%M:%S

PHP error log timestamp format:

  • Example timestamp: 27-Jan-2015 10:00:32 UTC
  • Timestamp format: %d-%b-Y %H:%M:%S

As the above also includes the time zone, this requires an added log configuration parameter to be included in the AWS Logs config file (which is usually found here: /var/awslogs/etc/awslogs.conf)

time_zone = UTC

It’s also easy to add further logs after the initial agent installation “wizard” has finished, simply by adding another entry to the bottom of the configuration file, for example:

[/logs/error.log]
datetime_format = %Y/%m/%d %H:%M:%S
file = /logs/error.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = end_of_file
log_group_name = /logs/error.log

After the agent installation AWS Logs does display some helper notes:

  • Configuration file successfully saved at: /var/awslogs/etc/awslogs.conf
  • You can begin accessing new log events after a few moments at…
  • You can use ‘sudo service awslogs start|stop|status|restart’ to control the daemon.
  • To see diagnostic information for the CloudWatch Logs Agent, see /var/log/awslogs.log
  • You can rerun interactive setup using ‘sudo ./awslogs-agent-setup.py —region eu-west-1 —only-generate-config’

We use cookies to make this website work, to understand how it's used, and to measure our advertising. Find out more about cookies.

Cookie preferences

make an enquiry

Quick form · 3 steps · 1 min

How can we help?

I am looking for

Website

New marketing website, microsite or redesign

E-commerce

Shopify store, new or rebuilt

Web application From £10,000 + VAT

Bespoke software and internal tools

AI implementation From £10,000 + VAT

Practical AI in your existing systems

Data visualisation From £10,000 + VAT

Dashboards and data pipelines

Migrations, hosting & support

Website migrations, web hosting and ongoing support

Something else / not sure yet

Let's talk it through

Prefer to talk?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Step 1 of 3

We'll be in touch within one business day to discuss your project. In the meantime, feel free to drop us a line at hello@enovate.co.uk if anything else comes up.

book directly

Book a discovery call with Michael

Michael Walsh, Founder & Managing Director. Pick a time that suits you, add your name and email, and the calendar invitation is yours.

Loading available times…

You can still book through the full calendar, or send us a message.

Times shown in . The calendar invitation lands in your inbox the moment you confirm.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply. Booking is handled by Calendly; see our privacy policy.

You're booked in

(). The calendar invitation is on its way to .

RescheduleCancel