Logging and Debugging ECS
Introduction: Production-Ready Logging and Debugging
In the previous lesson, you successfully deployed your containerized application as an ECS service on Fargate. Your service is now running, handling requests, and automatically recovering from failures. However, having a running service is just the beginning of your production journey. When issues arise — and they will — you need robust logging and debugging capabilities to quickly identify and resolve problems.
CloudWatch Logs integration with ECS Fargate provides a powerful foundation for monitoring your containerized applications. Remember from Lesson 3, when you configured your task definition with the awslogs log driver? That configuration automatically routes all container stdout and stderr output to CloudWatch Logs, creating a centralized location for all your application logs.
In this lesson, you will master the essential skills for production logging and debugging. You will learn how to manage log retention to control costs, access real-time logs as they stream from your containers, write sophisticated queries to find specific events or errors, and systematically debug failed tasks. By the end of this lesson, you will have a complete toolkit for maintaining and troubleshooting your ECS workloads in production environments.
CloudWatch Logs Configuration and Retention
Your task definition from Lesson 3 already includes the logging configuration that routes container output to CloudWatch Logs. When ECS runs your tasks, it automatically creates log groups and log streams based on the logConfiguration you specified. Each running task gets its own log stream within the log group, allowing you to track individual container instances.
However, logs can accumulate quickly and become expensive to store long term. Setting appropriate retention policies helps you balance debugging needs with cost control. You can configure log groups to automatically delete older logs after a specified period, ranging from one day to never expire.
To set a retention policy for your existing log group, use the put-retention-policy command. This example sets logs to expire after 7 days, which is suitable for development and testing environments:
You can verify the retention policy was applied by describing the log group:
The output will show your log group details, including the retention setting:
For production workloads, consider longer retention periods like 30, 90, or 365 days, depending on your compliance requirements and debugging needs. You can always adjust retention policies later without losing existing logs that have not yet expired.
