本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon CloudWatch 活動
CloudWatch Events 提供近乎即時的系統事件串流,描述 Amazon EC2 執行個體、 Lambda 函數、 Kinesis 串流、 Amazon ECS 工作、 Step Functions 狀態機器、 Amazon SNS 主題、 Amazon SQS 佇列或內建目標的 AWS 資源變更。您可以使用簡單的規則,來比對事件,並將這些事件轉傳到一或多個目標函數或串流。
Amazon EventBridge 是 CloudWatch 事件的演變。這兩種服務都使用相同的 API,因此您可以繼續使用 SDK 提供的CloudWatch 事件客戶端
新增事件
若要新增自訂 CloudWatch 事件,請使用包含一或多個PutEventsRequest
PutEventsRequestEntry
CloudWatchEventsClient’s
putEvents
方法。您可以指定項目的多個參數,例如事件的來源和類型、與事件相關聯的資源等等。
注意
對 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;
Code
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); } }
請參閱(詳見)的完整實例
新增規則
若要建立或更新規則,請使CloudWatchEventsClient’s
putRule
用規則名稱和選PutRuleRequest
匯入
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;
Code
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;
Code
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); } }
請參閱(詳見)的完整實例