CloudWatch Events examples using SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

CloudWatch Events examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Java 2.x with CloudWatch Events.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use PutEvents.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutEvents { public static void main(String[] args) { final String usage = """ Usage: <resourceArn> Where: resourceArn - An Amazon Resource Name (ARN) related to the events. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String resourceArn = args[0]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWEvents(cwe, resourceArn); cwe.close(); } 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); } } }
  • For API details, see PutEvents in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use PutRule.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutRule { public static void main(String[] args) { final String usage = """ Usage: <ruleName> roleArn>\s Where: ruleName - A rule name (for example, myrule). roleArn - A role ARN value (for example, arn:aws:iam::xxxxxx047983:user/MyUser). """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; String roleArn = args[1]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWRule(cwe, ruleName, roleArn); cwe.close(); } 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); } } }
  • For API details, see PutRule in Amazon SDK for Java 2.x API Reference.

The following code example shows how to use PutTargets.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

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.Target; /** * To run this Java V2 code example, ensure that you have setup your development * environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutTargets { public static void main(String[] args) { final String usage = """ Usage: <ruleName> <functionArn> <targetId>\s Where: ruleName - A rule name (for example, myrule). functionArn - An AWS Lambda function ARN (for example, arn:aws:lambda:us-west-2:xxxxxx047983:function:lamda1). targetId - A target id value. """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String ruleName = args[0]; String functionArn = args[1]; String targetId = args[2]; CloudWatchEventsClient cwe = CloudWatchEventsClient.builder() .build(); putCWTargets(cwe, ruleName, functionArn, targetId); cwe.close(); } 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(); 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); } } }
  • For API details, see PutTargets in Amazon SDK for Java 2.x API Reference.