When I run a task in Amazon Elastic Container Service (Amazon ECS), I receive a "ResourceInitializationError: failed to validate logger args" error.
Short description
When an Amazon ECS task can't find the Amazon CloudWatch log group defined in the task definition, Amazon ECS returns a ResourceInitialization error. You receive the following error message:
"ResourceInitializationError: failed to validate logger args: create stream has been retried 1 times: failed to create CloudWatch log stream: ResourceNotFoundException: The specified log group does not exist. : exit status 1"
To resolve the error, create a new log group for the task.
To troubleshoot errors for Amazon ECS tasks that fail to start, use the AWSSupport-TroubleshootECSTaskFailedToStart runbook. Then, refer to the relevant steps for your issue.
Resolution
Important
- Use the AWSSupport-TroubleshootECSTaskFailedToStart runbook in the same AWS Region where your Amazon ECS cluster resources are located.
- When you use the runbook, you must use the most recently failed Task ID. If the failed task is part of an Amazon ECS service, then use the most recently failed task in the service. The failed task must be visible in ECS:DescribeTasks during the automation. By default, stopped ECS tasks are visible for 1 hour after entering the Stopped state. Use the most recently failed task ID to prevent the task state cleanup from interrupting the analysis during the automation.
For instructions on how to initiate the runbook, see AWSSupport-TroubleshootECSTaskFailedToStart. Based on the output of the automation, use one of the following steps.
Identify the missing log group
If you don't know which log group is defined in the task definition and returns an error, then run the following describe-task-definition command:
aws ecs describe-task-definition --task-definition nginx-fargate:3 | jq -r .taskDefinition.containerDefinitions[].logConfiguration
Note: Replace nginx-fargate:3 with your task definition name and revision number.
The output describes the log group that you must recreate in CloudWatch.
Create the CloudWatch log group with the console
To create a CloudWatch log group, see Create a log group in CloudWatch Logs.
Create the CloudWatch log group with the AWS CLI
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, confirm that you're using the most recent AWS CLI version.
To create a CloudWatch log group, run the following create-log-group AWS CLI command.
aws logs create-log-group --log-group-name mylogs
Note: Replace mylogs with your log group name.
Use auto-configuration in the task definition
Amazon ECS can automatically create CloudWatch log groups for you when you add the awslogs-create-group: true option to your task definition. This eliminates the need to manually create log groups before running your tasks.
Choose one of the following auto-configuration approaches:
Option 1: Use the default log group name
When you set awslogs-create-group: true without specifying a custom log group name, Amazon ECS automatically creates a log group using your task definition family name with ecs as the prefix.
The following example shows the minimal configuration needed:
{
"containerDefinitions": [
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "awslogs-wordpress",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "awslogs-example"
}
}
}
]
}
Option 2: Use a custom log group name
If you need a specific log group naming convention for your organization, you can specify a custom log group name. Amazon ECS creates the log group with your specified name when the task starts.
The following example shows how to specify a custom log group name:
{
"containerDefinitions": [
{
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "example_container",
"awslogs-region": "eu-west-1",
"awslogs-create-group": "true",
"awslogs-stream-prefix": "example"
}
}
}
]
}
Note: The managed AWS Identity and Access Management (IAM) policy AmazonECSTaskExecutionRolePolicy doesn't include logs:CreateLogGroup permissions. To use the awslogs-create-group option, add logs:CreateLogGroup as an inline IAM policy.
To configure IAM permissions for auto-configuration, see Send Amazon ECS logs to CloudWatch.
Related information
Example Amazon ECS task definition: Route logs to CloudWatch