AWS SDK for Java
Developer Guide

Sending Events to CloudWatch

CloudWatch Events delivers a near real-time stream of system events that describe changes in AWS resources to Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, or built-in targets. You can match events and route them to one or more target functions or streams by using simple rules.

Add Events

To add custom CloudWatch events, call the CloudWatchEventsClient's putEvents method with a PutEventsRequest object that contains one or more PutEventsRequestEntry objects that provide details about each event. You can specify several parameters for the entry such as the source and type of the event, resources associated with the event, and so on.

Note

You can specify a maximum of 10 events per call to putEvents.

Imports

import software.amazon.awssdk.services.cloudwatchevents.CloudWatchEventsClient; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequest; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsRequestEntry; import software.amazon.awssdk.services.cloudwatchevents.model.PutEventsResponse;

Code

CloudWatchEventsClient cwe = CloudWatchEventsClient.builder().build(); final String EVENT_DETAILS = "{ \"key1\": \"value1\", \"key2\": \"value2\" }"; PutEventsRequestEntry request_entry = PutEventsRequestEntry.builder() .detail(EVENT_DETAILS) .detailType("sampleSubmitted") .resources(resource_arn) .source("aws-sdk-java-cloudwatch-example").build(); PutEventsRequest request = PutEventsRequest.builder() .entries(request_entry).build(); PutEventsResponse response = cwe.putEvents(request);

See the complete example on GitHub.

Add Rules

To create or update a rule, call the CloudWatchEventsClient's putRule method with a PutRuleRequest with the name of the rule and optional parameters such as the event pattern, IAM role to associate with the rule, and a scheduling expression that describes how often the rule is run.

Imports

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

CloudWatchEventsClient cwe = CloudWatchEventsClient.builder().build(); PutRuleRequest request = PutRuleRequest.builder() .name(rule_name) .roleArn(role_arn) .scheduleExpression("rate(5 minutes)") .state(RuleState.ENABLED) .build(); PutRuleResponse response = cwe.putRule(request);

See the complete example on GitHub.

Add Targets

Targets are the resources that are invoked when a rule is triggered. Example targets include Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, and built-in targets.

To add a target to a rule, call the CloudWatchEventsClient's putTargets method with a PutTargetsRequest containing the rule to update and a list of targets to add to the rule.

Imports

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

CloudWatchEventsClient cwe = CloudWatchEventsClient.builder().build(); Target target = Target.builder() .arn(function_arn) .id(target_id) .build(); PutTargetsRequest request = PutTargetsRequest.builder() .targets(target) .rule(rule_name) .build(); PutTargetsResponse response = cwe.putTargets(request);

See the complete example on GitHub.

More Information