How do I collect logs from my Amazon ECS container instance automatically?

Last updated:  2019-04-17

I'm troubleshooting my Amazon Elastic Container Service (Amazon ECS) environment or opening a case with AWS Support, and I need to collect various logs from my container instances. How can I do this?

Short Description

The Amazon ECS logs collector is a script that collects logs automatically from your container instance. The script collects general operating system logs as well as Docker and Amazon ECS container agent logs. Then, the script compresses and archives the logs into a single file that you can easily share when you're troubleshooting an issue.

The Amazon ECS logs collector supports the following operating systems:

  • Amazon Linux
  • Red Hat Enterprise Linux 7
  • Debian 8
  • Ubuntu 14.04
  • Windows 2016

Resolution

Download and run the Amazon ECS logs collector for Linux

1.    Connect to your container instance.

2.    To confirm that curl is installed, run the following command:

curl --version

The command output should be similar to the following:

curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3

Note: If the command doesn't return the expected results, install the curl package.

3.    To download the Amazon ECS logs collector script, run the following command:

curl -O https://raw.githubusercontent.com/awslabs/ecs-logs-collector/master/ecs-logs-collector.sh

4.    To collect the logs and create an archive with the script, run the following command:

[ec2-user ~]$ sudo bash ./ecs-logs-collector.sh

After you run the script, you can examine the collected logs in the collect folder that was created by the script. In the folder, you'll find the collect.tgz file, which is a compressed archive of all the logs.

Run the Amazon ECS logs collector in debug mode for Linux

If the logs don't provide all the information that you need, you can enable debug logging for the Docker daemon and Amazon ECS container agent with the --mode=debug option. This option allows the script to collect the logs before enabling debug mode. The script restarts the Docker daemon and Amazon ECS agent, and then terminates all containers running on the instance. Before running the following command, consider draining the container instance and moving any important tasks to other container instances. For more information, see Container Instance Draining.

To enable debug mode when you run the script, run the following command:

[ec2-user ~]$ sudo bash ./ecs-logs-collector.sh --mode=debug

Download and run the Amazon ECS logs collector for Windows

1.    Connect to your container instance.

2.    To download the script, open Windows PowerShell with administrator privileges, and then run the following command:

Invoke-WebRequest -OutFile ecs-logs-collector.ps1 https://raw.githubusercontent.com/awslabs/aws-ecs-logs-collector-for-windows/master/ecs-logs-collector.ps1

3.    In the same window, run the following script to collect the logs and create the archive:

.\ecs-logs-collector.ps1

After you run the script, you can examine the collected logs in the collect folder that was created by the script. In the folder, you will find the collect.tgz file, which is a compressed archive of all of the logs.

Run the Amazon ECS logs collector in debug mode for Windows

If the logs don't provide all the information that you need, you can enable debug logging for the Docker daemon and Amazon ECS container agent with the -RunMode debug option. This option allows the script to collect the logs before enabling debug mode. The script restarts the Docker daemon and Amazon ECS agent, and then terminates all containers running on the instance. Before running the following command, consider draining the container instance and moving any important tasks to other container instances. For more information, see Container Instance Draining.

To enable debug mode when you run the script, run the following command using Windows PowerShell with administrator privileges:

.\ecs-logs-collector.ps1 -RunMode debug

Did this article help you?

Anything we could improve?


Need more help?