将事件发送到 CloudWatch - AWS SDK for Java

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

将事件发送到 CloudWatch

CloudWatch事件提供近乎实时的系统事件流,这些系统事件描述AWS资源Amazon EC2实例,Lambda函数,Kinesis流式传输,Amazon ECS任务,Step Functions状态机,Amazon SNS主题,Amazon SQS队列或内置目标。通过使用简单的规则,您可以匹配事件并将事件路由到一个或多个目标函数或流。

添加事件

添加自定义CloudWatch事件,请致电 AmazonCloudWatch EventsSvents客户端putEvents方法PutEventsRequest包含一个或多个的对象PutEventsRequestEntry提供每个事件的详细信息的对象。您可以为条目指定多个参数,例如事件的来源和类型、与事件相关联的资源等等。

注意

对于每个 putEvents 调用,您最多可以指定 10 个事件。

导入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder; import com.amazonaws.services.cloudwatchevents.model.PutEventsRequest; import com.amazonaws.services.cloudwatchevents.model.PutEventsRequestEntry; import com.amazonaws.services.cloudwatchevents.model.PutEventsResult;

代码

final AmazonCloudWatchEvents cwe = AmazonCloudWatchEventsClientBuilder.defaultClient(); final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; PutEventsRequestEntry request_entry = new PutEventsRequestEntry() .withDetail(EVENT_DETAILS) .withDetailType("sampleSubmitted") .withResources(resource_arn) .withSource("aws-sdk-java-cloudwatch-example"); PutEventsRequest request = new PutEventsRequest() .withEntries(request_entry); PutEventsResult response = cwe.putEvents(request);

添加规则

要创建或更新规则,请致电 AmazonCloudwatch EventsRequestputRule方法PutRuleRequest具有规则的名称和可选参数,例如事件模式、IAM要与规则关联的角色,以及计划表达式这描述了规则的运行频率。

导入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder; import com.amazonaws.services.cloudwatchevents.model.PutRuleRequest; import com.amazonaws.services.cloudwatchevents.model.PutRuleResult; import com.amazonaws.services.cloudwatchevents.model.RuleState;

代码

final AmazonCloudWatchEvents cwe = AmazonCloudWatchEventsClientBuilder.defaultClient(); PutRuleRequest request = new PutRuleRequest() .withName(rule_name) .withRoleArn(role_arn) .withScheduleExpression("rate(5 minutes)") .withState(RuleState.ENABLED); PutRuleResult response = cwe.putRule(request);

添加目标

目标是触发规则时调用的资源。示例目标包括 Amazon EC2 实例、Lambda 函数、Kinesis 流、Amazon ECS 任务、Step Functions 状态机和内置目标。

要向规则添加目标,请致电 AmazonCloudwatch EventsRequestputTargets方法PutTargetsRequest包含要更新的规则和要添加到规则的目标列表。

导入

import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEvents; import com.amazonaws.services.cloudwatchevents.AmazonCloudWatchEventsClientBuilder; import com.amazonaws.services.cloudwatchevents.model.PutTargetsRequest; import com.amazonaws.services.cloudwatchevents.model.PutTargetsResult; import com.amazonaws.services.cloudwatchevents.model.Target;

代码

final AmazonCloudWatchEvents cwe = AmazonCloudWatchEventsClientBuilder.defaultClient(); Target target = new Target() .withArn(function_arn) .withId(target_id); PutTargetsRequest request = new PutTargetsRequest() .withTargets(target) .withRule(rule_name); PutTargetsResult response = cwe.putTargets(request);

更多信息