ECSOperator failed to display logs from Cloudwatch after providing log configurations
Apache Airflow version:
v1.10.10
Kubernetes version (if you are using kubernetes) (use kubectl version):
Environment:
- Cloud provider or hardware configuration: AWS
- OS (e.g. from /etc/os-release): Deployed in ECS Fargate
- Install tools: poetry
What happened:
I am trying to display the logs of Fargate Tasks in Airflow UI when using ECSOperator, and it is achieved by providing the following Task argument according to the docs.
awslogs_groupawslogs_regionawslogs_stream_prefix
However, it failed to work with an error msg
An error occurred (ResourceNotFoundException) when calling the GetLogEvents operation: The specified log stream does not exist
I went on and examine the source code and realise the log stream in the source code was constructed using this
stream_name = "{}/{}".format(self.awslogs_stream_prefix, task_id)
where in the official AWS docs, it states
The awslogs-stream-prefix option allows you to associate a log stream with the specified prefix, the container name, and the ID of the Amazon ECS task to which the container belongs. If you specify a prefix with this option, then the log stream takes the following format:
prefix-name/container-name/ecs-task-id
And after I manually patch my container name into the Airflow Task argument awslogs_stream_prefix it works (my current workaround).
Personally I am not sure whether this issue arises due to the specific configuration in Fargate i have, or it is generic enough to be considered as a bug.
What you expected to happen: Logs displaying in Airflow Log UI
log stream prefix is not dynamically constructed properly
How to reproduce it:
Running any ECS Fargate task with ECS Operator with default awslog configuration should be able to reproduce this.
Anything else we need to know:
Thanks for opening your first issue here! Be sure to follow the issue template!
I was also suffering the same. Thanks a lot!, the workaround works amazing! :)
Having the same issue here too, thanks to the explanation the fix was easy. I think the confusion arises comparing how the awslogs_stream_prefix is used in the operator and in AWS. The operator should take the container-name together with the task-id to build the complete log-stream name, as it is done in AWS.
I've just started facing this issue as well, as I'm trying to use the ECSOperator and have assigned it the correct awslogs_group and awslogs_steam_prefix (Same as the ones that get created when services create jobs). However, the logs don't seem to get created in CloudWatch, and only the ones that ECS generates itself seems to be created.
awslogs_stream_prefix does NOT need to contain the task_id right?
Currently my awslogs_group is something like /ecs/clustr_a
and awslogs_steam_prefix is something like task_name/container_name
@amirhmk did you able to solve it?
@amirhmk did you able to solve it?
Yes! In my case it was actually a small typo! Make sure the awslogs_group exists (maybe?)
and more importantly awslogs_steam_prefix is prefixed with the task_name and the container_name.
Also make sure your region is set properly
Thanks @amirhmk for your response. Not sure what I am doing wrong.
in cloud watch I see log entries
log group as /ecs/hello-world
logstream as /ecs/hello-world/
in ecs fargate my task name and container name are both hello-world and container log configuration is
awslogs-group = /ecs/hello-world awslogs-region =ap-southeast-1 awslogs-stream-prefix = ecs
and in airflowecsoperator task_id="hello-world" awslogs_group="/ecs/hello-world"
I tried with
awslogs_stream_prefix="/ecs/hello-world" -->existing prefix which I can see in cloudwatch awslogs_stream_prefix="/hello-world/hello-world" --> task-name/containername
but in both cases its not working with error botocore.errorfactory.ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the GetLogEvents operation: The specified log stream does not exist.
Sorry, I understand I am asking too specific here, however just hoping in case anybody can help.
I was having similar problems (displaying logs from Fargate to Airflow UI) so I wrote a short article explaining. Not sure if it works in all cases but I solved my issues, hopefully, it could help others as well. Displaying ECS Fargate logs in Airflow UI
Thanks @zdenulo finally after reading your article I realized that in awslogs_stream_prefix configuration it should not have leading / in it.
So instead of /ecs/hello-world it should be ecs/hello-world".
It worked.
Folks,
I went through this entire thread, but not able to troubleshoot the issue. I am having the same issue.
So I can see CloudWatch has my logs
CloudWatch
Log groups
/ecs/airflow-aws-gcp
ecs/airflow-aws-gcp/ad93daaa94db48f0bd278c780e946f64
Note the Stream Name is ecs/airflow-aws-gcp/ad93daaa94db48f0bd278c780e946f64
In my AirFlow ECSOperator Definition, I am providing
ECS_AWSLOGS_GROUP = '/ecs/airflow-aws-gcp'
ECS_AWSLOGS_STREAM_PREFIX = 'ecs/airflow-aws-gcp'
In the AirFlow logs, I see
[2021-09-14 23:46:03,310] {{ecs.py:257}} INFO - ECS Task started: {'tasks': [{'attachments': [{'id': '8fdb6419-df5f-4f61-a5d8-9388bb523695', 'type': 'ElasticNetworkInterface', 'status': 'PRECREATED', ......'containers': [{'containerArn': 'arn:aws:ecs:us-east-1:{ACCOUNT_ID}:container/airflow-ecs/ad93daaa94db48f0bd278c780e946f64/02114c6b-5925-45dd-a595-b9608157bdb6 ....
Am I reading it right that,
- ECS Task id is
8fdb6419-df5f-4f61-a5d8-9388bb523695 - But Log Stream that is generated has the name
ecs/airflow-aws-gcp/ad93daaa94db48f0bd278c780e946f64
Why the discrepancy? I am assuming that the ad93daaa94db48f0bd278c780e946f64 is the container instance id and not really the ECS Task Id. Am I configuring my ECS Task definition wrong?
Per the Documentation
If you don't specify a prefix with this option, then the log stream is named after the container ID that is assigned by the Docker daemon on the container instance. Because it is difficult to trace logs back to the container that sent them with just the Docker container ID (which is only available on the container instance), we recommend that you specify a prefix with this option.
I see that in my ECS Task definition, I have the correct settings.
So shouldn't the log stream be generated with the ECS Task ID and not the Docker Runtime Container Id? What could I be missing?
Another take on trying to figure out what is happening.
My ECS console shows task_id as this
But the ECSOperator logs task id as something else
[2021-09-15 01:46:33,311] {{ecs.py:257}} INFO - ECS Task started: {'tasks': [{'attachments': [{'id': 'e216354d-f05c-465c-967a-50efd86d7e0e', 'type': 'ElasticNetworkInterface', 'status': 'PRECREATED', 'details':
Again, not sure why this is happening. Is it ECSOperator bug or is it me doing something wrong in the way I have defined ECSOperator or ECS Task Definition
This is an old bug report. The issue may have been fixed as ECSOperator had many changes since. If someone is still experiencing problems please open a new issue with details