AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
PutTargets
搭配使用 AWS SDK
以下代码示例演示如何使用 PutTargets
。
- Java
-
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutTargetsRequest; import software.amazon.awssdk.services.cloudwatchevents.model.Target; /** * To run this Java V2 code example, ensure that you have setup your development * environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutTargets { public static void main(String[] args) { final String usage = """ Usage: <ruleName> <functionArn> <targetId>\s Where: ruleName - A rule name (for example, myrule). functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:xxxxxx047983:function:lamda1). targetId - A target id value. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; String functionArn = args[1]; String targetId = args[2]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWTargets(cwe, ruleName, functionArn, targetId); cwe.close(); } public static void putCWTargets(CloudWatchEventsClient cwe, String ruleName, String functionArn, String targetId) { try { Target target = Target.builder() .arn(functionArn) .id(targetId) .build(); PutTargetsRequest request = PutTargetsRequest.builder() .targets(target) .rule(ruleName) .build(); cwe.putTargets(request); System.out.printf( "Successfully created CloudWatch events target for rule %s", ruleName); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 PutTargets” 中的。
-
- JavaScript
-
- SDK对于 JavaScript (v3)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 导入SDK和客户端模块并调用API。
import { PutTargetsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutTargetsCommand({ // The name of the Amazon CloudWatch Events rule. Rule: process.env.CLOUDWATCH_EVENTS_RULE, // The targets to add to the rule. Targets: [ { Arn: process.env.CLOUDWATCH_EVENTS_TARGET_ARN, // The ID of the target. Choose a unique ID for each target. Id: process.env.CLOUDWATCH_EVENTS_TARGET_ID, }, ], }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
在单独的模块中创建客户端并将其导出。
import { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});
-
有关更多信息,请参阅 AWS SDK for JavaScript 开发人员指南。
-
有关API详细信息,请参阅 “AWS SDK for JavaScript API参考 PutTargets” 中的。
-
- SDK对于 JavaScript (v2)
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 // 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); } });
-
有关更多信息,请参阅 AWS SDK for JavaScript 开发人员指南。
-
有关API详细信息,请参阅 “AWS SDK for JavaScript API参考 PutTargets” 中的。
-
PutRule
CloudWatch 日志