Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the Cloudwatch log stream for a Fargate service?

I've got a Fargate service running, and can view its Cloudwatch log streams using the AWS console (navigate to the service, and click on its Logs tab).

I'm looking at the AWS documentation for GetLogEvents and see that you can access the logs using the log group name and log stream name. While I know the log group name for the service, the log stream name is generated dynamically.

How do I obtain the current log stream name for the running Fargate service?

I'm checking the AmazonECSClient documentation, any pointers would be helpful.

EDIT:

I found that the log group is actually specified for the container, not the service. Retrieving the task definition for the service, I can iterate over the container definitions which have the LogConfiguration section that indicates the Options, however that only provides the log group and a stream prefix, no log stream name:

- service
 - task definition
  - container definitions
   - LogConfiguration:
       LogDriver:  awslogs
       Options:    awslogs-group=/ecs/myservice
                   awslogs-region=us-east-1
                   awslogs-stream-prefix=ecs

EDIT 2:

I see from the AWS Console, that the link in the Logs tab does contain the log stream name. See the stream value in this sample URL:

https://us-east-1.console.aws.amazon.com/cloudwatch/home
?region=us-east-1
#logEventViewer:group=/ecs/myservice;stream=ecs/myservice/ad7246dd-bb0e-4eff-b059-767d30d40e69

How does the AWS Console obtain that value?

like image 241
Alan Avatar asked May 17 '18 17:05

Alan


1 Answers

I finally found the format of the log stream name in the AWS documentation here:

awslogs-stream-prefix

Required: No, unless using the Fargate launch type in which case it is required.

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

Note that the ecs-task-id is the GUID portion of the task's ARN:

For this sample Task ARN:
arn:aws:ecs:us-east-1:123456789012:task/12373b3b-84c1-4398-850b-4caef9a983fc

the ecs-task-id to use for the log stream name is:
12373b3b-84c1-4398-850b-4caef9a983fc
like image 154
Alan Avatar answered Oct 14 '22 05:10

Alan