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

4 minute read
0

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.

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 share when you're troubleshooting an issue.

You can also use AWSSupport-CollectECSInstanceLogs runbook in AWS Systems Manager Automation to collect logs from your ECS instance. The runbook allows you to upload the logs to an S3 bucket that you created. You must provide the location of the S3 bucket in the LogDestination input parameter.

Note: AWSSupport-CollectECSInstanceLogs doesn't support debug mode. For more information, see Run an automation.

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 can 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 turn on debug logging for the Docker daemon and Amazon ECS container agent with the --mode=enable-debug option. This option allows the script to collect the logs before turning on 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 turn on debug mode when you run the script, run the following command:

[ec2-user ~]$ sudo bash ./ecs-logs-collector.sh --mode=enable-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 were created by the script. In the folder, you can find the collect.tgz file, which is a compressed archive of all 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 turn on 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 turning on 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 turrn on debug mode when you run the script, run the following command using Windows PowerShell with administrator privileges:

.\ecs-logs-collector.ps1 -RunMode debug
AWS OFFICIAL
AWS OFFICIALUpdated a year ago