DEV Community

Cover image for Cloudwatch Monitoring
muhirwaJD
muhirwaJD

Posted on

Cloudwatch Monitoring

๐Ÿ› ๏ธ Node.js Logging to CloudWatch from Dockerized EC2 App

๐Ÿงฉ Project Overview

In this project, I built a lightweight Node.js web server using Morgan middleware to log HTTP requests. I deployed it on an EC2 instance, containerized the app with Docker, and then configured AWS CloudWatch to stream logs both from a file (for traditional deployment) and directly from Docker (for container-based monitoring).

This dual setup gave me deep visibility into application traffic using two popular logging strategiesโ€”and helped me better understand how to monitor apps in cloud-native environments.


๐Ÿš€ What I Built

  • โœ… A Node.js app with HTTP request logging using Morgan
  • โœ… Dockerized version of the app using a custom Dockerfile
  • โœ… Deployed on an Ubuntu EC2 instance
  • โœ… Implemented two types of logging to CloudWatch:
    • File-based logging with CloudWatch Agent (for non-containerized apps)
    • Docker log driver for real-time streaming (for containerized apps)

๐Ÿ’ก Why This Matters

Logging is critical for:

  • ๐Ÿ“ˆ Monitoring traffic and diagnosing issues
  • ๐Ÿ”’ Ensuring app health and security
  • ๐Ÿ” Auditing activity for compliance or troubleshooting

This project taught me:

  • How to configure IAM roles securely for EC2 and CloudWatch
  • How to write logs to both file and console for hybrid observability
  • How to use Docker's native log drivers to stream data to AWS CloudWatch
  • The differences and use cases between classic agent-based logging vs container-native logging

๐Ÿง  Key Learnings

  • Docker logs can be streamed directly to CloudWatch without writing to disk
  • Morgan allows dual streaming (file + console), which is perfect for hybrid setups
  • CloudWatch Agent requires precise file paths and log group names to function
  • IAM roles must be attached to EC2 instances with the correct policies (CloudWatchAgentServerPolicy, AmazonSSMManagedInstanceCore)
  • Real-time log monitoring helps identify traffic patterns and potential issues instantly

๐Ÿ› ๏ธ Tech Stack

  • Node.js
  • Express / Morgan middleware
  • Docker
  • AWS EC2 (Ubuntu)
  • AWS CloudWatch Logs
  • AWS IAM Roles
  • CloudWatch Agent

๐Ÿ” How to Try It Yourself

  1. Clone the Node.js app with Morgan logging
  2. Build the Docker image and run it
  3. Either:
    • Use CloudWatch Agent to stream from a file, or
    • Use Docker's awslogs driver for direct streaming
  4. Monitor traffic logs in CloudWatch in real time!

๐Ÿ“ธ Screenshots

  • Terminal output

Logs sent to terminal

  • CloudWatch Log Group + Stream

Logs sent to cloudwatch

Note: You'll see the IPs hidden ofc. Sorry hackers, these IPs are not for you ๐Ÿ™ƒ

Top comments (0)