Utilizing AWS SDK V3 for Javascript to pull ELB metrics ('HealthyHostCount', 'UnHealthyHostCount' and 'RequestCount')

0

All, I am having an issue when trying to pull ELB metrics using the AWS SDK V3 for Javascript API calls. Any help would be greatly appreciated!

Specifics:

  • Want to pull the metrics for the target groups for network and application ELBs but I am getting empty response (Datapoints: []) from the APIs.
  • I want to find out the 'HealthyHostCount', 'UnHealthyHostCount' and 'RequestCount' metrics for both types (ALB/NLB).

Here is the contents of a .js file I created: multipull.js

When I attempt to run this I don't receive any datapoints returned as you can see from this output picture.

Enter image description here

1 Answer
0

Was able to attain a working example for the CX utilizing:

const { CloudWatchClient, GetMetricStatisticsCommand } = require('@aws-sdk/client-cloudwatch'); // CommonJS import
const client = new CloudWatchClient({ region: 'us-east-1' });
// Function to get metrics
const getMetrics = async (namespace, metricName, dimensions) => {
    const params = {
        Namespace: namespace,
        MetricName: metricName,
        Dimensions: dimensions,
        StartTime: new Date('2024-08-23T18:00:00Z'), // Required
        EndTime: new Date('2024-09-06T14:00:00Z'), // Required
        Period: 1209600, // 14 Days
        Statistics: ['Average'],
    };
    try {
        const command = new GetMetricStatisticsCommand(params);
        const response = await client.send(command);
        console.log(`Metrics for ${metricName}:`, response);
    } catch (error) {
        console.error(`Error fetching metrics for ${metricName}:`, error);
    }
};
// Define metrics and dimensions
const metrics = [
    { namespace: 'AWS/ApplicationELB', dimensions: [{ Name: 'LoadBalancer', Value: 'ALB/NLBNAMETAKENFROMCLOUDWATCHMETRICS' }, { Name: 'TargetGroup', Value: 'TARGETGROUPNAMETAKENFROMCLOUDWATCHMETRICS' }], metrics: ['HealthyHostCount', 'UnHealthyHostCount', 'RequestCount'] },
    { namespace: 'AWS/NetworkELB', dimensions: [{ Name: 'LoadBalancer', Value: 'ALB/NLBNAMETAKENFROMCLOUDWATCHMETRICS' }, { Name: 'TargetGroup', Value: 'TARGETGROUPNAMETAKENFROMCLOUDWATCHMETRICS' }], metrics: ['HealthyHostCount', 'UnHealthyHostCount'] }
];
// Fetch metrics
metrics.forEach(metric => {
    metric.metrics.forEach(metricName => {
        getMetrics(metric.namespace, metricName, metric.dimensions);
    });
});

**For the ALB/NLBNAMETAKENFROMCLOUDWATCHMETRICS and TARGETGROUPNAMETAKENFROMCLOUDWATCHMETRICS I grabbed the names from Cloudwatch Metrics by finding each metric ('HealthyHostCount', 'UnHealthyHostCount', 'RequestCount'), graphing them and then taking the names from the information when clicking on their attributes

AWS
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions