1 Answer
- Newest
- Most votes
- Most comments
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
answered a month ago
Relevant content
- Accepted Answerasked a year ago
- asked 2 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated 7 months ago