简体   繁体   中英

how to get aws lambda invocation count using javascript sdk

I want to get invocation count for each of my lambda functions for a specific time range. I have used cloudwatch sdk to implement this. However I am getting a Metrics array but there is no data regarding the invocation count. My code.

var cwparams = {
  Namespace: "AWS/Lambda",
  MetricName: "Invocations",
};

const cw = await cloudWatch.listMetrics(cwparams).promise();
console.log(cw.Metrics);

My output

ResponseMetadata: { RequestId: 'some-id' },
  Metrics: [
    {
      Namespace: 'AWS/Lambda',
      MetricName: 'Invocations',
      Dimensions: [Array]
    },
    ... more objects like above
    ]
OwningAccounts: []

Dimensions array contains like this

{ Name: 'FunctionName', Value: 'us-east-1.xxxx-xxxx' },
{ Name: 'Resource', Value: 'us-east-1.xxx-xxxx:xxx' }

How do I get my desired output?

Solved it.You can use CloudWatch GetMetricStatistics API to retrieve Invocation/Duration for a specific time range and for a specific lambda function.For details, have a look at GetMetricStatistics AWS Docs

Pasted my answer below.

const params = {
    Namespace: "AWS/Lambda",
    MetricName: "Invocations",
    StartTime: "2023-01-04T00:00:00Z",
    EndTime: "2023-01-04T23:59:59Z",
    Period: 86400, // in seconds
    Statistics: ["Maximum", "Minimum", "Average"],
    Dimensions: [
      {
        Name: "FunctionName",
        Value: "your-lambda-function-name",
      },
    ],
  };
const cw = await cloudWatch.getMetricStatistics(params).promise();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM