π οΈ 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)