We're launching a new cybersecurity venture! Learn more about Snowbit >

Cloudwatch Metrics Cloudwatch Metrics

Last Updated: Mar. 30, 2022

Coralogix Cloudwatch-Metrics SAR allow you to send CloudWatch Metrics to Coralogix.

The recommended and easiest integration method will be to use our AWS Serverless Application Repository.

Go to Serverless Application Repository and search for ‘coralogix’. Don’t forget to check the ‘Show apps that create custom IAM roles or resource policies’ box located just under the search field to see all available applications.

aws serverless applicaiton repository

Select Coralogix-CloudWatch-Metrics form the list.

VariableDescription
Application NameThe stack name of this application created via AWS CloudFormation
NotificationEmailFailure notification email address
ApplicationNameApplication Name in Coralogix.
CoralogixRegionThe Coralogix location region [Europe, India, Singapore, US] Check your Coralogix account if url ends in .us its US. Region. Same for the others.
FunctionArchitectureOur Function supports x86_64 or arm64
FunctionMemorySizeMax Memory for the function it self.
FunctionTimeoutFunction TimeOut
MetricsCheck Example Below
PrivateKeyYour Coralogix account send your logs key. Can be found in DataFlow -> Api Keys

Metrics Example:

This Example needs to be minified and inserted in the Metrics Value Above.

[
   {
     "Dimensions": [
       {
         "Name": "FunctionName",
         "Value": "test-function"
       }
     ],
     "Period": 60,
     "Statistics": [
       "SampleCount",
       "Average",
       "Sum",
       "Minimum",
       "Maximum"
     ],
     "MetricName": "Invocations",
     "Namespace": "AWS/Lambda"
   },
   {
     "Dimensions": [
       {
         "Name": "FunctionName",
         "Value": "test-function"
       }
     ],
     "Period": 60,
     "Statistics": [
       "SampleCount",
       "Average",
       "Sum",
       "Minimum",
       "Maximum"
     ],
     "MetricName": "Duration",
     "Namespace": "AWS/Lambda"
   }
 ]

You can find the GitHub Repository https://github.com/coralogix/coralogix-aws-serverless/tree/master/src/cloudwatch-metrics

For AWS CloudWatch Metrics and References please follow https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-services-cloudwatch-metrics.html

Manual Installation

Manual Installation is only recommended when Serverless application repository is not available.

For example in AWS Government Cloud

This tutorial will describe how to import your Cloudwatch metrics into Coralogix by namespace and metrics name. The metric can then be used in Kibana or Elastic Timelion for visualization and correlation with your logs.

1. Author a nodejs 12.x from scratch

cloudwatch metrics info

2. Add the following policy to your lambda:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "cloudwatch:GetMetricStatistics",
                "cloudwatch:ListMetrics",
                "cloudwatch:DescribeAlarms"
            ],
            "Resource": "*"
        }
    ]
}

3. Edit the following code inline, make sure you add the 2 files, index.js, and params.js

index.js:

// Load the AWS SDK for Node.js

var AWS = require('aws-sdk');
var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'});
var params = require('./params').params
const https = require('https');
const assert = require('assert');
const appName = process.env.app_name ? process.env.app_name : 'NO_APPLICATION';

assert(process.env.private_key, 'No private key')

exports.handler = async function (event, context,callback) {
AWS.config.update({region: event.region});
var EndTime = new Date;var StartTime = new Date(EndTime - 2*60*1000);
  var getMetricStatistics = function getMetricStatistics(param)
{
  param.StartTime = StartTime;
  param.EndTime = EndTime;
   return new Promise(function(resolve, reject){
        cw.getMetricStatistics(param,function(err,data){
            if(err){
                  console.log(err);
                  reject(err);
            }
            else{
                  resolve(data)
            }
        })
  })
}

var postToCoralogix = function postToCoralogix(parsedEvents) {
   return new Promise(function(resolve, reject){
        try {
            var options = {
                hostname: 'api.coralogix.com',
                port: 443,
                path: '/api/v1/logs',
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                        }
                    };
            var req = https.request(options, function (res) {
                res.setEncoding('utf8');
                res.on('data', function (body) {
                    resolve(null)
                });
            });
            req.on('error', function (e) {
                console.log('problem with request: ' + e.message);
                reject(e.message);
            });
            // write data to request body
            req.write(JSON.stringify(parsedEvents));
            req.end();
        } catch (ex) {
            console.log(ex);
            reject(ex);
        }
  })

    }

for (var i = 0; i < params.length; i++) {
  try{
    var result = await getMetricStatistics(params[i])
    var logEntries = []
    console.log(result)
    for (var j = 0; j < result.Datapoints.length; j++) {
      logEntries.push({
            "timestamp": new Date(result.Datapoints[j].Timestamp).getTime(),
            "severity": 3,
            "text": {Datapoints:result.Datapoints[j],MetricName:params[i].MetricName,Period:params[i].Period,Dimension:params[i].Dimensions[0]}
        })
    }
    if(logEntries.length > 0)
    {
        const events = {
        "privateKey": process.env.private_key,
        "applicationName": appName,
        "subsystemName": params[i].Namespace,
        "logEntries": logEntries}
        console.log(events)
        var error = await  postToCoralogix(events)
        error == null ? console.log('logs sent to coralogix') : console.log('error while sending events to coralogix',error)
    }
  }
  catch(err){
   console.log(err)
  }
 }
};

params.js:

const statistics = ['SampleCount', 'Average', 'Sum', 'Minimum', 'Maximum']
const period = 60
exports.params = [{
  Dimensions: [
        {
      Name: 'FunctionName',
      Value: 's3ToCoralogix'
    }
  ],
  Period: period,
  Statistics: statistics,
  MetricName: 'Invocations',
  Namespace: 'AWS/Lambda'
},{
  Dimensions: [
        {
      Name: 'FunctionName',
      Value: 's3ToCoralogix'
    }
  ],
  Period: period,
  Statistics: statistics,
  MetricName: 'Duration',
  Namespace: 'AWS/Lambda'
},{
  Dimensions: [
        {
      Name: 'FunctionName',
      Value: 'CWevents'
    }
  ],
  Period: period,
  Statistics: statistics,
  MetricName: 'Duration',
  Namespace: 'AWS/Lambda'
}];

4. Configure the params.js
The params is an array of parameters that define a call to Cloudwatch, each param needs to have: Namespace, MetricName, Statistics, and Dimensions. 

Namespace:
is a container for metrics like AWS/ApiGateway, AWS/Lambda etc… a list of all AWS namespaces can be found here:https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-namespaces.html 

MetricName:
each Namespace has its own available metrics, for example, AWS/Lambda has Duration, Invocation, Error etc…https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/lam-metricscollected.html#lambda-cloudwatch-metrics

Dimensions:
Each namespace has its own dimensions, for example, AWS/Lambda has FunctionNamehttps://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/lam-metricscollected.html#lam-metric-dimensions

Period:
Is the bin size to aggregate the metrics, the default and the minimum is 60 seconds

Statistics:
Which kind of statistics to query, the default is: ‘SampleCount’, ‘Average’, ‘Sum’, ‘Minimum’, ‘Maximum’

Here is an example to get metrics from AWS/Lambda named CWevents:

{
  Dimensions: [
        {
      Name: 'FunctionName',
      Value: 'CWevents'
    }
  ],
  Period: period,
  Statistics: statistics,
  MetricName: 'Duration',
  Namespace: 'AWS/Lambda'
}

5. Add Coralogix’s mandatory environment variables, private_key and applicationName (e.g test, dev, prod) 

coralogix environment variables

6. Increase the runtime timeout and memory to 2 min, and 1024mb

lambda settings

7. Add Cloudwatch events trigger: 

cloudwatch event triggers

8.  Configure the trigger, please choose Schedule expression with rate (2 minutes)

lambda event trigger

That’s all! you should have your CW metrics streaming to Coralogix now. Not seeing events yet? Ping us on our in-app chat or send an email

On this page