使用 SDK 的 CloudWatch 事件示例 JavaScript V3 - Amazon SDK for JavaScript
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

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

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

使用 SDK 的 CloudWatch 事件示例 JavaScript V3

以下代码示例向您展示如何通过使用Amazon SDK for JavaScript带有的开发 CloudWatch 事件。

操作展示如何调用具体的代码节选。 CloudWatch 事件函数。

方案展示如何通过调用多个来完成特定任务的代码示例。 CloudWatch 事件函数。

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

主题

操作

以下代码示例演示了如何添加Amazon Lambda亚马逊的功能目标 CloudWatch 事件事件。

适用于的开 JavaScript V3

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

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

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

// Import required AWS SDK clients and commands for Node.js import { PutTargetsCommand } from "@aws-sdk/client-cloudwatch-events"; import { cweClient } from "./libs/cloudWatchEventsClient.js"; // Set the parameters export const params = { Rule: "DEMO_EVENT", Targets: [ { Arn: "LAMBDA_FUNCTION_ARN", //LAMBDA_FUNCTION_ARN Id: "myCloudWatchEventsTarget", }, ], }; export const run = async () => { try { const data = await cweClient.send(new PutTargetsCommand(params)); console.log("Success, target added; requestID: ", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开 JavaScript V2

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Rule: 'DEMO_EVENT', Targets: [ { Arn: 'LAMBDA_FUNCTION_ARN', Id: 'myCloudWatchEventsTarget', } ] }; cwevents.putTargets(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

以下代码示例显示如何创建 Amazon。 CloudWatch 事件计划规则。

适用于的开 JavaScript V3

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

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

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

// Import required AWS SDK clients and commands for Node.js import { PutRuleCommand } from "@aws-sdk/client-cloudwatch-events"; import { cweClient } from "./libs/cloudWatchEventsClient.js"; // Set the parameters export const params = { Name: "DEMO_EVENT", RoleArn: "IAM_ROLE_ARN", //IAM_ROLE_ARN ScheduleExpression: "rate(5 minutes)", State: "ENABLED", }; export const run = async () => { try { const data = await cweClient.send(new PutRuleCommand(params)); console.log("Success, scheduled rule created; Rule ARN:", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开 JavaScript V2

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Name: 'DEMO_EVENT', RoleArn: 'IAM_ROLE_ARN', ScheduleExpression: 'rate(5 minutes)', State: 'ENABLED' }; cwevents.putRule(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.RuleArn); } });

以下代码示例显示如何发送 Amazon。 CloudWatch 事件事件。

适用于的开 JavaScript V3

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

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

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

// Import required AWS SDK clients and commands for Node.js import { PutEventsCommand } from "@aws-sdk/client-cloudwatch-events"; import { cweClient } from "./libs/cloudWatchEventsClient.js"; // Set the parameters export const params = { Entries: [ { Detail: '{ "key1": "value1", "key2": "value2" }', DetailType: "appRequestSubmitted", Resources: [ "RESOURCE_ARN", //RESOURCE_ARN ], Source: "com.company.app", }, ], }; export const run = async () => { try { const data = await cweClient.send(new PutEventsCommand(params)); console.log("Success, event sent; requestID:", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
适用于的开 JavaScript V2

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Entries: [ { Detail: '{ \"key1\": \"value1\", \"key2\": \"value2\" }', DetailType: 'appRequestSubmitted', Resources: [ 'RESOURCE_ARN', ], Source: 'com.company.app' } ] }; cwevents.putEvents(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });