Download the script file from here ecs-report
Managing and monitoring tasks in Amazon Elastic Container Service (ECS) can be a complex task. As your ECS clusters and services scale, it becomes increasingly important to keep track of their status and ensure that they are running smoothly. In this blog post, we’ll explore a Python script that uses the Boto3 library to generate a report on the status of ECS tasks. This script can be a valuable tool for DevOps engineers and system administrators who want to keep a close eye on their ECS infrastructure.
Understanding the Script
The script begins by importing the necessary libraries: boto3 for interacting with the AWS APIs and csv for generating the report in CSV format. Additionally, it imports the datetime and timezone modules to handle date and time calculations.
The generate_task_report() function is the heart of the script. It initializes a Boto3 client for ECS and retrieves the list of cluster ARNs using the list_clusters() API call. It then iterates over each cluster and retrieves the list of service ARNs using the list_services() API call. For each service, it retrieves detailed information using the describe_services() API call. The script extracts relevant information such as the service name, status, last deployment time, task duration, and more. If the service has reached a steady state, it is marked as such in the report. The gathered data is stored in a list of dictionaries.
After generating the report data, the write_report_to_csv() function is called. This function takes the report data as input and writes it to a CSV file named task_report.csv using the csv.DictWriter class. The CSV file contains columns for the cluster ARN, service ARN, service name, status, last deployment time, task duration, and timestamp.
How to Use the Script
Once you have the dependencies installed, you can copy the script into a Python file (e.g., ecs_task_report.py). Make sure to replace the AWS credentials in your environment or set them up using the AWS CLI or environment variables.
To generate the task report, simply execute the script by running the following command:
python ecs_task_report.py
The script will connect to your AWS account, retrieve information about your ECS clusters and services, and generate a report in CSV format. The report will be saved as task_report.csv in the same directory as the script.
Use Case: Verifying ECS Clusters After a Forced Deployment
Let’s consider a use case where you have just performed an ECS update with a force new deployment option. After the update, you want to ensure that all ECS clusters and services have returned to a normal, steady state. This is crucial for maintaining the stability and availability of your application.
By running the provided script, you can quickly generate a task report that includes the status of each service and the time since the last deployment. You can use this report to check if all services have reached a steady state and to identify any services that may require further investigation. If a service is not in a steady state or has a long task duration, it might indicate an issue that needs attentionor troubleshooting.
Once you have the task report in CSV format, you can easily review and analyze the data. You can sort the report based on the status column to quickly identify any services that are not in a steady state. Additionally, you can filter the report based on task duration to find services that have been running for an unusually long time.
By running this script and reviewing the generated task reports, you can gain valuable insights into the health of your ECS infrastructure. It helps you identify any services that are not in a steady state or have long task durations, allowing you to promptly address any issues and maintain the stability of your applications.
