AWS SDK for Node.js调用getMetricData报错:该方法不存在
My Issue
I'm attempting to pull SQS queue visibility metrics from AWS CloudWatch using Node.js, but I keep hitting this error:
TypeError: cloudwatchClient.getMetricData is not a function at getCloudwatchMetricData (/home/baonv/cowell-project/gitlab-cowell/servertechnology/uketoru/done/53434/local.js:194:22) at Object.exports.handler (/home/baonv/cowell-project/gitlab-cowell/servertechnology/uketoru/done/53434/local.js:313:13) at Object._executeSync (/usr/local/lib/node_modules/lambda-local/lib/lambdalocal.js:136:34) at Object._execute [as execute] (/usr/local/lib/node_modules/lambda-local/lib/lambdalocal.js:39:22) at /usr/local/lib/node_modules/lambda-local/bin/lambda-local:100:21 at Object.<anonymous> (/usr/local/lib/node_modules/lambda-local/bin/lambda-local:132:3) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12)
Here's the code I'm using:
function getCloudwatchMetricData(queueName, callback) { console.log("Get CloudWatch metric data of SQS: " + queueName); var params = { MetricDataQueries: [{ Id: queueName, MetricStat: { Metric: { Dimensions: [{ Name: 'QueueName', Value: queueName }], MetricName: 'ApproximateNumberOfMessagesVisible', Namespace: 'AWS/SQS' }, Period: 300, Stat: 'Average', Unit: 'Count' }, ReturnData: true }], StartTime: startTime, EndTime: endTime, ScanBy: 'TimestampAscending' }; cloudwatchClient.getMetricData(params, function (err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); }
The identical logic works fine when I run it via AWS CLI:
aws cloudwatch get-metric-data --metric-data-queries file://testdata/metric-data-queries.json --start-time 2018-05-20T00:00:00Z --end-time 2018-05-21T00:00:00Z --scan-by TimestampAscending
I specifically don't want to use the getMetricStatistics method — can someone help me resolve this?
Solution
This error is almost always caused by one of two issues: using an outdated AWS SDK version, or incorrectly initializing the CloudWatch client (especially if you're mixing v2 and v3 SDK patterns). Here's how to fix it:
1. Verify & Update AWS SDK for JavaScript v2
If you're using the classic v2 SDK (aws-sdk package), getMetricData was added in version 2.200.0. If your SDK is older than this, the method won't exist.
- First, check your current SDK version:
npm list aws-sdk - If it's outdated, upgrade to the latest v2 version:
npm install aws-sdk@latest - Double-check your client initialization — you must create a proper instance of
AWS.CloudWatch:
Don't forget: you can't callconst AWS = require('aws-sdk'); // Replace with your AWS region, or use environment variables const cloudwatchClient = new AWS.CloudWatch({ region: 'us-east-1' });getMetricDatadirectly on theAWS.CloudWatchclass — you need to use an instance of it.
2. If You're Using AWS SDK for JavaScript v3
The v3 SDK uses a modular, command-based pattern — you can't call getMetricData directly on the client. Instead, you need to use the GetMetricDataCommand:
- Install the CloudWatch client package:
npm install @aws-sdk/client-cloudwatch - Rewrite your function to use the v3 pattern (async/await is recommended over callbacks):
const { CloudWatchClient, GetMetricDataCommand } = require("@aws-sdk/client-cloudwatch"); // Initialize client with your region const cloudwatchClient = new CloudWatchClient({ region: 'us-east-1' }); async function getCloudwatchMetricData(queueName) { console.log("Get CloudWatch metric data of SQS: " + queueName); const params = { MetricDataQueries: [{ Id: queueName, MetricStat: { Metric: { Dimensions: [{ Name: 'QueueName', Value: queueName }], MetricName: 'ApproximateNumberOfMessagesVisible', Namespace: 'AWS/SQS' }, Period: 300, Stat: 'Average', Unit: 'Count' }, ReturnData: true }], StartTime: startTime, EndTime: endTime, ScanBy: 'TimestampAscending' }; try { const response = await cloudwatchClient.send(new GetMetricDataCommand(params)); console.log(response); } catch (err) { console.error(err, err.stack); } }
Quick Troubleshooting Tip
If you have both v2 and v3 SDK packages installed, make sure you're importing the correct one in your code. Mixing imports (e.g., requiring aws-sdk but trying to use v3 commands) will cause this exact error.
内容的提问来源于stack exchange,提问作者Nguyễn Văn Bảo

