本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon CloudWatch 活动
CloudWatch 事件提供近乎实时的系统事件流,这些事件描述了 Amazon EC2 实例、 Lambda 函数、 Kinesis 流、 Amazon ECS 任务、 Step Functions 状态机、 Amazon SNS 主题、 Amazon SQS 队列或内置目标的 Amazon 资源变化。通过使用简单的规则,您可以匹配事件并将事件路由到一个或多个目标函数或流。
Amazon EventBridge 是 CloudWatch 活动的演变。这两项服务使用相同的 API,因此您可以继续使用 SDK 提供的CloudWatch 事件客户端
添加事件
要添加自定义 CloudWatch 事件,请使用一个PutEventsRequest
CloudWatchEventsClient’s
putEvents
方法,该对象包含一个或多个提供有关每个事件的详细信息的PutEventsRequestEntry
注意
对于每个 putEvents
调用,您最多可以指定 10 个事件。
导入
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry;
代码
public static void putCWEvents(CloudWatchEventsClient cwe, String resourceArn ) { try { final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; PutEventsRequestEntry requestEntry = PutEventsRequestEntry.builder() .detail(EVENT_DETAILS) .detailType("sampleSubmitted") .resources(resourceArn) .source("aws-sdk-java-cloudwatch-example") .build(); PutEventsRequest request = PutEventsRequest.builder() .entries(requestEntry) .build(); cwe.putEvents(request); System.out.println("Successfully put CloudWatch event"); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
请参阅上的完整示例
添加规则
要创建或更新规则,请使用PutRuleRequest
CloudWatchEventsClient’s
putRule
方法。
导入
import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleRequest; import software.amazon.awssdk.services.cloudwatchevents.model.PutRuleResponse; import software.amazon.awssdk.services.cloudwatchevents.model.RuleState;
代码
public static void putCWRule(CloudWatchEventsClient cwe, String ruleName, String roleArn) { try { PutRuleRequest request = PutRuleRequest.builder() .name(ruleName) .roleArn(roleArn) .scheduleExpression("rate(5 minutes)") .state(RuleState.ENABLED) .build(); PutRuleResponse response = cwe.putRule(request); System.out.printf( "Successfully created CloudWatch events rule %s with arn %s", roleArn, response.ruleArn()); } catch ( CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
请参阅上的完整示例
添加目标
目标是触发规则时调用的资源。示例目标包括 Amazon EC2 实例、 Lambda 函数、 Kinesis 流、 Amazon ECS 任务、 Step Functions 状态机和内置目标。
要向规则中添加目标,请使用PutTargetsRequest
CloudWatchEventsClient’s
putTargets
方法。
导入
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.PutTargetsResponse; import software.amazon.awssdk.services.cloudwatchevents.model.Target;
代码
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(); PutTargetsResponse response = 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); } }
请参阅上的完整示例