帮助我们改进AWS SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub
这些区域有:AWS SDK for JavaScriptV3 API 参考指南详细描述了所有的 API 操作AWS SDK for JavaScript版本 3 (V3)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在 Amazon CloudWatch 中创建警报
此 Node.js 代码示例演示:
如何检索有关 CloudWatch 警报的基本信息。
如何创建和删除 CloudWatch 警报。
场景
警报会每隔一段时间 (由您指定) 监控一个指标,并根据相对于给定阈值的指标值每隔若干个时间段执行一项或多项操作。
在本示例中,使用一系列 Node.js 模块在 CloudWatch 中创建警报。Node.js 模块使用适用于 JavaScript 的开发工具包使用以下方法来创建警报。CloudWatch
客户端类:
有关 CloudWatch 警报的更多信息,请参阅创建 Amazon CloudWatch 警报中的Amazon CloudWatch 用户指南.
先决条件任
要设置和运行此示例,您必须先完成以下任务:
-
设置项目环境以运行这些 Node TypeScript 示例,然后安装所需的AWS SDK for JavaScript和第三方模块。按照上的说明操作GitHub
. 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息,请参阅从共享凭证文件加载 Node.js 中的凭证。
这些示例使用 ECMASCRIPT6 (ES6)。这需要 Node.js 版本 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅。Node.js 下载内容。
但是,如果你更喜欢使用 CommonJS 语法,请参阅JavaScript ES6/常用JS 语法
描述警报
创建libs
创建文件名为的 Node.js 模块cloudWatchClient.js
. 将下面的代码复制并粘贴到其中,这将创建 CloudWatch 客户端对象。Replace领域
使用您的AWSregion region。
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 });
这个代码是可用的GitHub 上的位置
创建文件名为 describeAlarms.js
的 Node.js 模块。确保按前面所示配置开发工具包,包括下载开发工具包。CloudWatch
客户端。创建 JSON 对象,保存用于检索警报描述的参数,将返回的警报限制为具有状态 INSUFFICIENT_DATA
的警报。然后调用DescribeAlarmsCommand
方法CloudWatch
客户端服务对象。
// 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();
要运行示例,请在命令提示符处输入以下内容。
node describeAlarms.js
可以找到这个示例代码GitHub 上的
针对 CloudWatch 指标创建警报
创建libs
创建文件名为的 Node.js 模块cloudWatchClient.js
. 将下面的代码复制并粘贴到其中,这将创建 CloudWatch 客户端对象。Replace领域
使用您的AWSregion region。
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 });
这个代码是可用的GitHub 上的
创建文件名为 putMetricAlarm.js
的 Node.js 模块。确保按前面所示配置开发工具包,包括安装所需的客户端和软件包。针对基于指标创建警报所需的参数创建 JSON 对象,在本示例中指标为 Amazon EC2 实例的 CPU 利用率。其余参数设置为在指标超过 70% 的阈值时触发警报。然后调用DescribeAlarmsCommand
方法CloudWatch
客户端服务对象。
ReplaceINSTANCE_ID
将用 Amazon EC2 实例的 ID。
// 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();
要运行示例,请在命令提示符处输入以下内容。
node putMetricAlarm.js
可以找到这个示例代码GitHub 上的位置
删除警报
创建libs
创建文件名为的 Node.js 模块cloudWatchClient.js
. 将下面的代码复制并粘贴到其中,这将创建 CloudWatch 客户端对象。Replace领域
使用您的AWSregion region。
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 });
这个代码是可用的GitHub 上的位置
创建文件名为 deleteAlarms.js
的 Node.js 模块。确保按前面所示配置开发工具包,包括下载 CloudWatch 客户端。创建 JSON 对象以保存要删除的警报的名称。然后调用DeleteAlarmsCommand
方法CloudWatch
客户端服务对象。
ReplaceALARM _NAME
警报的名称。
// 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();
要运行示例,请在命令提示符处输入以下内容。
node deleteAlarms.js
可以找到这个示例代码GitHub 上的位置