Skip to content

ecs scheduled task not writes on Cloudwatch

0

I have changed a ECS service to scheduled task.

After the change the task no longer writes on CloudWatch. The scheduled task has a specific role on eventbridge with:

  • AmazonEC2ContainerServiceEventsRole

And one inline policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "logs:PutLogEvents",
                "logs:CreateLogStream"
            ],
            "Effect": "Allow",
            "Resource": "*",
            "Sid": "Logs"
        }
    ]
}

The task is executed (I see it on eventbridge metrics) but the logs not are writed.

Also I see and "Amazon CloudWatch IAM role" in the console that not is configured but i don't know how configure it, I can't see any documentation

Enter image description here

How I can get cloudwatch permisions for an scheduled task?

asked 2 years ago477 views

1 Answer
0

The issue you're experiencing with your ECS scheduled task not writing logs to CloudWatch is likely due to insufficient permissions in the IAM role associated with your task. To resolve this, you need to ensure that the correct permissions are in place for both the task execution and the CloudWatch logging.

Here are the steps you should take:

  1. First, make sure your task definition is using the correct task execution role. This role should have the necessary permissions to interact with CloudWatch Logs.

  2. The task execution role should have the AmazonECSTaskExecutionRolePolicy attached, which includes the required permissions for CloudWatch Logs. If you're using a custom policy, ensure it includes these permissions:

    • logs:CreateLogStream
    • logs:PutLogEvents
  3. In addition to the task execution role, your container instances (if you're using EC2 launch type) or Fargate tasks need the appropriate permissions. The role should include:

    • logs:CreateLogGroup
    • logs:CreateLogStream
    • logs:PutLogEvents
  4. The inline policy you've shown for your EventBridge role is a good start, but it's missing the 'logs:CreateLogGroup' permission. Add this to ensure full functionality.

  5. Regarding the "Amazon CloudWatch IAM role" you see in the console, this is typically used for CloudWatch agent configuration on EC2 instances. For ECS tasks, you don't need to configure this separately.

  6. Make sure your task definition includes the proper log configuration. In your task definition, you should have a logConfiguration section that specifies the awslogs driver and the appropriate log group and region.

  7. If you're using the Fargate launch type, ensure your task definition includes the necessary networkMode and requires compatibilities for Fargate.

After making these changes, your scheduled ECS task should be able to write logs to CloudWatch. If you continue to experience issues, double-check that your task is actually running and that there are no errors in the task execution itself.
Sources
Send Amazon ECS logs to CloudWatch - Amazon Elastic Container Service
Access to CloudWatch Logs - AWS Lambda
Troubleshoot permission errors for an instance on CloudWatch | AWS re:Post

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.