๐ ๏ธ 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
- Clone the Node.js app with Morgan logging
- Build the Docker image and run it
- Either:
- Use CloudWatch Agent to stream from a file, or
- Use Docker's awslogs driver for direct streaming
- Monitor traffic logs in CloudWatch in real time!
๐ธ Screenshots
- Terminal output
- CloudWatch Log Group + Stream
Note: You'll see the IPs hidden ofc. Sorry hackers, these IPs are not for you ๐
Top comments (0)