

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon CloudWatch Events の使用
<a name="examples-cloudwatch-send-events"></a>

 CloudWatch Events は、 AWS リソースの変更を記述するシステムイベントのほぼリアルタイムのストリームを、 Amazon EC2 インスタンス、 Lambda 関数、 Kinesis ストリーム、 Amazon ECS タスク、 Step Functions ステートマシン、 Amazon SNS トピック、 Amazon SQS キュー、または組み込みターゲットに配信します。簡単なルールを使用して、一致したイベントを 1 つ以上のターゲット関数またはストリームに振り分けることができます。

Amazon EventBridge は、Amazon CloudWatch Events の[進化形](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cwe-now-eb.html)です。どちらのサービスも同じ API を使用するため、SDK が提供する [CloudWatch Events クライアント](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatch/CloudWatchClient.html)を引き続き使用することも、SDK for Java の [EventBridge クライアント](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/eventbridge/EventBridgeClient.html)に移行して CloudWatch Events 機能を使用することもできます。CloudWatch Events の[ユーザーガイドドキュメント](https://docs.aws.amazon.com/eventbridge/latest/userguide/index.html)と [API リファレンス](https://docs.aws.amazon.com/eventbridge/latest/APIReference/index.html)が、EventBridge ドキュメントサイトから利用可能になりました。

## イベントの追加
<a name="add-events"></a>

カスタム CloudWatch イベントを追加するには、各イベントの詳細を提供する 1 つ以上の[https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutEventsRequest.html](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutEventsRequest.html)オブジェクトを含む[https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutEventsRequestEntry.html](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutEventsRequestEntry.html)オブジェクトを使用して `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;
```

 **コード** 

```
    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);
        }
    }
```

[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/0b1785e42949ebf959eaa0f0da4dc2a48f92ea25/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutEvents.java) で完全な例をご覧ください。

## ルールの追加
<a name="add-rules"></a>

ルールを作成または更新するには、 `CloudWatchEventsClient’s``putRule`メソッドを呼び出して、ルールの名前と[イベントパターン](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)、ルールに関連付ける IAM ロール、ルールの実行頻度を記述する[スケジューリング式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html)などのオプションパラメータ[https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutRuleRequest.html](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutRuleRequest.html)を指定します。

 **インポート** 

```
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);
        }
    }
```

[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/0b1785e42949ebf959eaa0f0da4dc2a48f92ea25/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutRule.java) で完全な例をご覧ください。

## ターゲットの追加
<a name="add-targets"></a>

ターゲットは、ルールがトリガーされたときに呼び出されるリソースです。ターゲットの例には、 Amazon EC2 インスタンス、 Lambda 関数、 Kinesis ストリーム、 Amazon ECS タスク、 Step Functions ステートマシン、組み込みターゲットなどがあります。

ルールにターゲットを追加するには、更新するルールを含む [https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutTargetsRequest.html](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatchevents/model/PutTargetsRequest.html) とルールに追加するターゲットのリストを使用して `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);
        }
    }
```

[GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/blob/0b1785e42949ebf959eaa0f0da4dc2a48f92ea25/javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutTargets.java) で完全な例をご覧ください。

## 詳細情報
<a name="more-information"></a>
+  「Amazon EventBridge ユーザーガイド」の「[PutEvents を使用したイベントの追加](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-putevents.html)」。
+  「Amazon EventBridge ユーザーガイド」の「[ルールのスケジュール式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-create-scheduled-rule-schedule)」
+  「Amazon EventBridge ユーザーガイド」の「[CloudWatch Eventsのイベントタイプ](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html)」
+  「Amazon EventBridge ユーザーガイド」の「[イベントパターン](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)」
+  詳細については、 Amazon EventBridge API リファレンスの「[PutEvents](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEvents.html)」を参照してください。
+  Amazon EventBridge API リファレンスの「[PutTargets](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutTargets.html)」
+  Amazon EventBridge API リファレンスの「[PutRule](https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html)」