Search code examples
pythonamazon-web-servicesamazon-ecsamazon-cloudwatchaws-fargate

how to setup/write to the aws log group from a fargate task?


I have setting up a fargate task via cloudformation template ( see sample below) , the code for the task is in script.py file below. I successfully set up the task, now i want write logs to the cloudwatch. I have set up a log group while setting up the fargate task , and planning to use logger in python to write logs. how do i write to the aws log group i have created with my fargate task, from script.py using logger in python.

AWSTemplateFormatVersion: 2010-09-09
Description: An example CloudFormation template for Fargate.
Parameters:
  VPC:
    Type: AWS::EC2::VPC::Id
  SubnetA:
    Type: AWS::EC2::Subnet::Id
  SubnetB:
    Type: AWS::EC2::Subnet::Id
  Image:
    Type: String
    Default: 123456789012.dkr.ecr.region.amazonaws.com/image:tag
Resources: 
  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: !Join ['', [!Ref ServiceName, Cluster]]

  TaskDefinition:
    Type: AWS::ECS::TaskDefinition
    DependsOn: LogGroup
    Properties:
      Family: !Join ['', [!Ref ServiceName, TaskDefinition]]
      NetworkMode: awsvpc
      RequiresCompatibilities:
        - FARGATE
      Cpu: 256
      Memory: 2GB
      ExecutionRoleArn: !Ref ExecutionRole
      TaskRoleArn: !Ref TaskRole
      ContainerDefinitions:
        - Name: !Ref ServiceName
          Image: !Ref Image
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: '/ecs/my_log_group'
              awslogs-region: 'us-east-1'
              awslogs-stream-prefix: ecs-myfargatetask


script.py

import logging

logger = logging.getlogger('...')

logger.info('logging working')



Solution

  • You just need to configure your Python logger to output to STDOUT/STDERR (which I believe is what it does by default). The console output of the main process of your docker container in ECS is what is sent to CloudWatch Logs.