CloudWatch 使用软件开发工具包的示例 JavaScript V3 - AWS SDK for JavaScript

帮助我们改进AWS SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub.

这些区域有:AWS SDK for JavaScriptV3 API 参考指南详细描述了所有的 API 操作AWS SDK for JavaScript版本 3 (V3)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

CloudWatch 使用软件开发工具包的示例 JavaScript V3

以下代码示例显示如何使用AWS SDK for JavaScriptV3 CloudWatch.

操作展示如何称呼个人的代码节选 CloudWatch 函数。

方案展示如何通过多个 multic 来完成特定任务的代码示例,这些示例介绍如何通过多个 CloudWatch 函数。

每个示例都包含一个指向以下内容的链接 GitHub其中包含了有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何创建 Amazon CloudWatch 监控指标的告警。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { PutMetricAlarmCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { AlarmName: "Web_Server_CPU_Utilization", ComparisonOperator: "GreaterThanThreshold", EvaluationPeriods: 1, MetricName: "CPUUtilization", Namespace: "AWS/EC2", Period: 60, Statistic: "Average", Threshold: 70.0, ActionsEnabled: false, AlarmDescription: "Alarm when server CPU exceeds 70%", Dimensions: [ { Name: "InstanceId", Value: "INSTANCE_ID", }, ], Unit: "Percent", }; export const run = async () => { try { const data = await cwClient.send(new PutMetricAlarmCommand(params)); console.log("Success", data); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); var params = { AlarmName: 'Web_Server_CPU_Utilization', ComparisonOperator: 'GreaterThanThreshold', EvaluationPeriods: 1, MetricName: 'CPUUtilization', Namespace: 'AWS/EC2', Period: 60, Statistic: 'Average', Threshold: 70.0, ActionsEnabled: false, AlarmDescription: 'Alarm when server CPU exceeds 70%', Dimensions: [ { Name: 'InstanceId', Value: 'INSTANCE_ID' }, ], Unit: 'Percent' }; cw.putMetricAlarm(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

以下代码示例演示如何删除 Amazon CloudWatch 警报。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { DeleteAlarmsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { AlarmNames: "ALARM_NAME" }; // e.g., "Web_Server_CPU_Utilization" export const run = async () => { try { const data = await cwClient.send(new DeleteAlarmsCommand(params)); console.log("Success, alarm deleted; requestID:", data); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

导入软件开发工具包和客户端模块,然后调用 API。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); var params = { AlarmNames: ['Web_Server_CPU_Utilization'] }; cw.deleteAlarms(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

以下代码示例展示如何描述 Amazon CloudWatch 指标指标指标指标指标指标警报。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { DescribeAlarmsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { StateValue: "INSUFFICIENT_DATA" }; export const run = async () => { try { const data = await cwClient.send(new DescribeAlarmsCommand(params)); console.log("Success", data); return data; data.MetricAlarms.forEach(function (item, index, array) { console.log(item.AlarmName); return data; }); } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); cw.describeAlarms({StateValue: 'INSUFFICIENT_DATA'}, function(err, data) { if (err) { console.log("Error", err); } else { // List the names of all current alarms in the console data.MetricAlarms.forEach(function (item, index, array) { console.log(item.AlarmName); }); } });

以下代码示例演示如何禁用亚马逊 CloudWatch 警报报报报操作。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { DisableAlarmActionsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { AlarmNames: "ALARM_NAME" }; // e.g., "Web_Server_CPU_Utilization" export const run = async () => { try { const data = await cwClient.send(new DisableAlarmActionsCommand(params)); console.log("Success, alarm disabled:", data); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

导入软件开发工具包和客户端模块,然后调用 API。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); cw.disableAlarmActions({AlarmNames: ['Web_Server_CPU_Utilization']}, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

以下代码示例演示如何启用 Amazon CloudWatch 警报报报报操作。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { PutMetricAlarmCommand, EnableAlarmActionsCommand, } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { AlarmName: "ALARM_NAME", //ALARM_NAME ComparisonOperator: "GreaterThanThreshold", EvaluationPeriods: 1, MetricName: "CPUUtilization", Namespace: "AWS/EC2", Period: 60, Statistic: "Average", Threshold: 70.0, ActionsEnabled: true, AlarmActions: ["ACTION_ARN"], //e.g., "arn:aws:automate:us-east-1:ec2:stop" AlarmDescription: "Alarm when server CPU exceeds 70%", Dimensions: [ { Name: "InstanceId", Value: "INSTANCE_ID", }, ], Unit: "Percent", }; export const run = async () => { try { const data = await cwClient.send(new PutMetricAlarmCommand(params)); console.log("Alarm action added; RequestID:", data); return data; const paramsEnableAlarmAction = { AlarmNames: [params.AlarmName], }; try { const data = await cwClient.send( new EnableAlarmActionsCommand(paramsEnableAlarmAction) ); console.log("Alarm action enabled; RequestID:", data.$metadata.requestId); } catch (err) { console.log("Error", err); return data; } } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

导入软件开发工具包和客户端模块,然后调用 API。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); var params = { AlarmName: 'Web_Server_CPU_Utilization', ComparisonOperator: 'GreaterThanThreshold', EvaluationPeriods: 1, MetricName: 'CPUUtilization', Namespace: 'AWS/EC2', Period: 60, Statistic: 'Average', Threshold: 70.0, ActionsEnabled: true, AlarmActions: ['ACTION_ARN'], AlarmDescription: 'Alarm when server CPU exceeds 70%', Dimensions: [ { Name: 'InstanceId', Value: 'INSTANCE_ID' }, ], Unit: 'Percent' }; cw.putMetricAlarm(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Alarm action added", data); var paramsEnableAlarmAction = { AlarmNames: [params.AlarmName] }; cw.enableAlarmActions(paramsEnableAlarmAction, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Alarm action enabled", data); } }); } });

以下代码示例演示如何列出 Amazon CloudWatch 指标。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { ListMetricsCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { Dimensions: [ { Name: "LogGroupName" /* required */, }, ], MetricName: "IncomingLogEvents", Namespace: "AWS/Logs", }; export const run = async () => { try { const data = await cwClient.send(new ListMetricsCommand(params)); console.log("Success. Metrics:", JSON.stringify(data.Metrics)); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); var params = { Dimensions: [ { Name: 'LogGroupName', /* required */ }, ], MetricName: 'IncomingLogEvents', Namespace: 'AWS/Logs' }; cw.listMetrics(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Metrics", JSON.stringify(data.Metrics)); } });

以下代码示例显示如何将数据放入 Amazon CloudWatch 指标。

适用于的开发工具包 JavaScript V3
提示

要了解如何设置和运行此示例,请参阅GitHub.

在单独的模块中创建客户端并将其导出。

import { CloudWatchClient } from "@aws-sdk/client-cloudwatch"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cwClient = new CloudWatchClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { PutMetricDataCommand } from "@aws-sdk/client-cloudwatch"; import { cwClient } from "./libs/cloudWatchClient.js"; // Set the parameters export const params = { MetricData: [ { MetricName: "PAGES_VISITED", Dimensions: [ { Name: "UNIQUE_PAGES", Value: "URLS", }, ], Unit: "None", Value: 1.0, }, ], Namespace: "SITE/TRAFFIC", }; export const run = async () => { try { const data = await cwClient.send(new PutMetricDataCommand(params)); console.log("Success", data.$metadata.requestId); return data; } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开发工具包 JavaScript V2
提示

要了解如何设置和运行此示例,请参阅GitHub.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatch service object var cw = new AWS.CloudWatch({apiVersion: '2010-08-01'}); // Create parameters JSON for putMetricData var params = { MetricData: [ { MetricName: 'PAGES_VISITED', Dimensions: [ { Name: 'UNIQUE_PAGES', Value: 'URLS' }, ], Unit: 'None', Value: 1.0 }, ], Namespace: 'SITE/TRAFFIC' }; cw.putMetricData(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", JSON.stringify(data)); } });