How to publish FROM an AWS lambda to a cloud watch metric in Node.js

前端 未结 2 1702
你的背包
你的背包 2021-02-20 15:57

Inside a lambda I use to periodically check in on a service I check the value of the result from the server and I want that value published to AWS cloudwatch as a metric to form

2条回答
  •  北荒
    北荒 (楼主)
    2021-02-20 16:43

    Yes, that is possible:

    const AWS = require('aws-sdk');
    
    const metric = {
      MetricData: [ /* required */
        {
          MetricName: 'YOUR_METRIC_NAME', /* required */
          Dimensions: [
            {
              Name: 'URL', /* required */
              Value: url /* required */
            },
          /* more items */
          ],
          Timestamp: new Date(),
          Unit: 'Count',
          Value: SOME_VALUE
        },
        /* more items */
      ],
      Namespace: 'YOUR_METRIC_NAMESPACE' /* required */
    };
    
    const cloudwatch = new AWS.CloudWatch({region: 'eu-west-1'});
    cloudwatch.putMetricData(metric, (err, data) => {
    
    
    if (err) {
        console.log(err, err.stack); // an error occurred
      } else {
        console.log(data);           // successful response
    }
    });
    

    First your create the data that you want to store as a metric, the you use the CloudWatch API to send it to CloudWatch. (Of course the function must have permission to write to CloudWatch.)

    More documentation is here: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatch.html

提交回复
热议问题