0
votes

When I usually run a task in ECS using Fargate, the STDOUT is redirected automatically to cloudwatch and this application logs can be found without any complication.

To clarify, for example, in C#:

Console.WriteLine("log to write to CloudWatch")

That output is automatically redircted to CloudWatch logs when I use ECS with Fargate or Lambda functions

I would like to do the same using EC2.

The first impression using ECS with EC2 is that this is not as automatic as Fargate. Am I right?

Looking for a information I have found the following (apart of other older question or post):

  • In this question refers to an old post from the AWS blog, so this could be obsolete.

  • In this AWS page, they describe a few steps where you need to install some utilities to your EC2

So, summarizing, is there any way to see the STDOUT in cloudwatch when I use ECS with EC2 in the same way Fargate does?

3

3 Answers

0
votes

So, summarizing, is there any way to see the STDOUT in cloudwatch when I use ECS with EC2 in the same way Fargate does?

If you mean EC2 logging as easily as Fargate does without any complex configuration, then no. You need to provide some configuration and utilities to your EC2 to allow logging to CloudWatch. As any EC2 instance we launch, ECS instances are just a virtual machine with some operational system with a default configuration, in this case, is Amazon ECS-optimized AMIs. Other services and configurations we should provide by ourself.

Besides the link above you provided, I found this CloudFormation template which configures EC2 Spot Fleet to log to CloudWatch in the same way your second link describes.

0
votes

I don't think your correct. The StdOut logs from the ECS task launch are just as easily written and accessed running under EC2 as Fargate.

You just have this in your task definition which, as far as I can tell, is the same as in Fargate:

  "containerDefinitions": [
    {
      "dnsSearchDomains": null,
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "my-log-family",
          "awslogs-region": "us-east-1",
          "awslogs-stream-prefix": "my-stream-name"
        }
      }
...

After it launches, you should see your logs under my-log-family

If you are trying to put application logs in CloudWatch, that's another matter... this is typically done using the CloudWatch logs agent which you'd have to install into the container, but the above will capture the StdOut.

0
votes

This is how I did it.

Using the nugget: AWS.Logger.AspNetCore

An example of use:

    static async Task Main(string[] args)
    {
        Logger loggerObj = new Logger();
        ILogger<Program> logger = await loggerObj.CreateLogger("test", "eu-west-1");
        logger.LogInformation("test info");
        logger.LogError("test error");
    }

    public async Task<ILogger<Program>> CreateLogger(string logGroup, string region)
    {
        AWS.Logger.AWSLoggerConfig config = new AWS.Logger.AWSLoggerConfig();
        config.Region = region;
        config.LogStreamNameSuffix = "";
        config.LogGroup = logGroup;

        LoggerFactory logFactory = new LoggerFactory();

        logFactory.AddAWSProvider(config);
        return logFactory.CreateLogger<Program>();

    }