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)