Sending logs to cloudwatch

I am using winston to generate logs from my express app which is not on cloud. Can I send them to cloudwatch or does cloudwatch only works if your app is running on an EC2 instance?


Related discussions:

sure you can, there is API for that. you need to parse though, and convert it to timestamps and lines.

api link: https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html

boto3 link: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.put_log_events

cli link: https://docs.aws.amazon.com/cli/latest/reference/logs/put-log-events.html

It will work irrespective of where the app is running. If the app is writing to a file, use the unified cloudwatch agent to send the logs to cloudwatch.

If you’re already using winston, you might check out winston-cloudwatch (assuming you’re using node.js): https://www.npmjs.com/package/winston-cloudwatch

Put it in a container and then use the Docker log driver setting to send to CloudWatch logs. Super simple and you can use Instance Roles instead of having to use IAM keys. Requires no code changes on your part. (Unless your app cant run in a container)

try this

curl -X POST
https://monitoring.us-east-1.amazonaws.com
-H ‘Accept: application/json’
-H ‘Authorization: AWS4-HMAC-SHA256 Credential=YOUR_ACCESS_KEY_GOES_HERE/20190326/us-east-1/monitoring/aws4_request, SignedHeaders=accept;content-encoding;content-length;content-type;host;x-amz-date;x-amz-target, Signature=SIGV4_SIGNATURE_GOES_HERE’
-H ‘Content-Encoding: amz-1.0’
-H ‘Content-Length: 141’
-H ‘Content-Type: application/json’
-H ‘X-Amz-Date: 20190326T071934Z’
-H ‘X-Amz-Target: GraniteServiceVersion20100801.PutMetricData’
-H ‘host: monitoring.us-east-1.amazonaws.com’
-d ‘{
“Namespace”: “StackOverflow”,
“MetricData”: [
{
“MetricName”: “TestMetric”,
“Value”: 123.0
}
] }’

You can also use the CloudWatch Agent to ship logs from non-EC2 servers

The unified CloudWatch agent enables you to do the following: … Collect logs from Amazon EC2 instances and on-premises servers, running either Linux or Windows Server.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Install-CloudWatch-Agent.html

You can, but the real question is why would you want to? There are plenty of much more efficient and less expensive log aggregation solutions out there with free tiers. CloudWatch Logs isn’t that good.

Fluentd can ship logs to Cloudwatch. I never tried it outside of AWS, but it should work as long as you can reach the AWS API from your server.

We use winstonjs also. It does correctly log to cloud-watch from anywhere, as long as you have setup the AWS cli profile correctly on the machine you’re logging from.

That’d work for CW Metrics, but not CW Logs

CloudWatch Agent for the win! Did it at Re:Invent. Just install the agent, add a role to your instance, and point it at a file and you’re done.

For log aggregation, searching, etc. CloudWatch just seems horrible to me.

I jettisoned it in favor of Graylog. ELK is cool too but alerting functionality seemed to cost money and I needed log-based alerting badly.

Been using it for a few months now and it’s great and the users love being able to search easily for errors, debug problems in our app stack, etc.

We wanted a SaaS solution because logs are for production app so on-premise would not be a good idea. Splunk was also an alternative but I think its highly paid as compared to cloudwatch and elasticsearch combined.

Thanks everyone for your inputs.

There are others out there. I used logz.io. Here’s the pricing: https://logz.io/pricing

Also, if you’re using elasticsearch you don’t really need cloudwatch (no need to combine the two). You can use Kibana for log querying.

Btw original python powered cloudwatch log forwarder sucks. I had a lot of headache with it. Recently they made libs for fluentbit and it works like a charm. Bonus is very low resources consumption.
https://github.com/aws/aws-for-fluent-bit

I’m curious about this. Did you set up Graylog for all your environments (prd/stg/dev)? Or just prod?

How big is your elastic lsearch cluster? Do you have any alerting from any tracked metrics from Graylog?

All our environments - production, staging, dev - for now. Our environment is very small though for the moment - couple dozen AWS instances across two regions. A lot of issues creep in through logged messages vs basic infrastructure problems like running out of disk space, excess CPU/memory utilization, general capacity issues, etc. so logging is quite important. Graylog server sizing is t2.xlarge and I gave it about 200GB of disk. No additional Elasticsearch cluster nodes. As we scale of course I expect that will grow.

CloudWatch logs will be a fraction of that price with much cheaper and longer retention options. We priced out a bunch of different SaaS options and CloudWatch pricing was almost free in comparison.