如何解决从 ECS 容器中的 docker 到 cloudwatch 的日志记录问题?

How to troubleshoot logging to cloudwatch from docker in an ECS container?

我为此焦头烂额。 这是我的设置:

我看到创建了一个用于 ECS 任务的 Cloudwatch 日志组,但我没有打印到 stderr。

我的 ECS 容器具有默认的 ecsInstanceRole。 ecsInstanceRole 具有 AmazonEC2ContainerServiceforEC2Role 策略,如下所示:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ecs:CreateCluster",
                "ecs:DeregisterContainerInstance",
                "ecs:DiscoverPollEndpoint",
                "ecs:Poll",
                "ecs:RegisterContainerInstance",
                "ecs:StartTelemetrySession",
                "ecs:UpdateContainerInstancesState",
                "ecs:Submit*",
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
                ],
                "Resource": "*"
            }
        ]
    }

任务定义

{
    "containerDefinitions": [
        {
            "dnsSearchDomains": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-group": "torres",
                    "awslogs-region": "us-west-2",
                    "awslogs-stream-prefix": "torres"
                }
            },
            "command": [
                "sleep",
                "infinity"
            ],
            "cpu": 100,
            "mountPoints": [
                {
                    "readOnly": null,
                    "containerPath": "/mnt/efs",
                    "sourceVolume": "efs"
                }
            ],
            "memoryReservation": 512,
            "image": "X.dkr.ecr.us-west-2.amazonaws.com/torres-docker:latest",
            "interactive": true,
            "essential": true,
            "pseudoTerminal": true,
            "readonlyRootFilesystem": false,
            "privileged": false,
            "name": "torres"
        }
    ],
    "family": "torres",
    "volumes": [
        {
            "name": "efs",
            "host": {
                "sourcePath": "/mnt/efs"
            }
        }
    ]
}

我想出了这个问题,但我不知道为什么它解决了它。 当我将我的命令从 sleep infinity 更改为仅 运行 一段记录 python script.py 的代码时,CloudWatch 中的日志记录开始工作。

在我使用 sleep infinity ssh-ing 启动容器并从 shell 启动 script.py 之前,这不是日志记录。