Skip to main content

Logging & Observability

One of the greatest things of AWS Lambda is that you get all the benfits of CloudWatch logging built into the platform. Logging is just a simple Ruby puts command away. Here are a few amazing things to help you succeed with good logging and observability patterns in AWS with CloudWatch.

STDOUT is a Must!

Lambda is a read-only file system. The Lamby gem will set the RAILS_LOG_TO_STDOUT environment variable on your behalf. It also freedom patches the core Ruby Logger class to force STDOUT. That said, be on the lookout for any rogue disk-based logging you may have to address. Older Rails applications may have to use a pattern like this.

logger =
logger.formatter =
config.logger = logger
config.log_level = :info

Using Lograge

Our cookiecutter installs and configures the Lograge gem to reduce CloudWatch data costs while easily allowing CloudWatch Insights to parse and query your logs. If your project is not using Lograge, please consider adding it as we do.

gem 'lograge'
config.lograge.enabled = true
config.lograge.formatter =
config.lograge.custom_payload do |controller|
{ requestid: controller.request.request_id }

CloudWatch Log Insights

CloudWatch Logs Insights enables you to interactively search and analyze your log data in Amazon CloudWatch Logs. You can perform queries to help you quickly and effectively respond to operational issues. If an issue occurs, you can use CloudWatch Logs Insights to identify potential causes and validate deployed fixes.

🎥 YouTube: Analyze Log Data with CloudWatch Logs Insights

CloudWatch Embedded Metrics

The CloudWatch Embedded Metric Format enables CloudWatch to ingest complex high-cardinality application data in the form of logs and easily generate actionable metrics and alarms from them. By sending your logs in the new Embedded Metric Format, you can now easily create custom metrics without having to instrument or maintain separate code, while gaining powerful analytical capabilities on your log data. You can get started with embedded metrics by using our rails-lambda/aws-embedded-metrics Ruby gem. The following Lamby-friendly libraries use this format:

  • Crypteia: SSM Parameters as secure ENV variables.
  • Lambdakiq: ActiveJob on SQS & Lambda.

New Relic

Some older Application Performance Monitor (APM) gems can be used with Lambda but you must flush their data after each request in a way that does not impact response performance. You can do this with the LambdaPunch.

config.lambda.handled_proc = do |_event, context|
LambdaPunch.push { NewRelic::Agent.agent.flush_pipe_data }
Async Processing Using Lambda Extensions