Create a CloudWatch Events scheduled rule using an Amazon SDK - Amazon CloudWatch Events
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.

Create a CloudWatch Events scheduled rule using an Amazon SDK

The following code examples show how to create an Amazon CloudWatch Events scheduled rule.

Java
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.

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.

JavaScript
SDK for JavaScript V3
Note

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

Create the client in a separate module and export it.

import { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; import { DEFAULT_REGION } from "libs/utils/util-aws-sdk.js"; export const client = new CloudWatchEventsClient({ region: DEFAULT_REGION });

Import the SDK and client modules and call the API.

import { PutRuleCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { // Request parameters for PutRule. // https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html#API_PutRule_RequestParameters const command = new PutRuleCommand({ Name: process.env.CLOUDWATCH_EVENTS_RULE, // The event pattern for the rule. // Example: {"source": ["my.app"]} EventPattern: process.env.CLOUDWATCH_EVENTS_RULE_PATTERN, // The state of the rule. Valid values: ENABLED, DISABLED State: "ENABLED", }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
SDK for JavaScript V2
Note

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

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create CloudWatchEvents service object var cwevents = new AWS.CloudWatchEvents({apiVersion: '2015-10-07'}); var params = { Name: 'DEMO_EVENT', RoleArn: 'IAM_ROLE_ARN', ScheduleExpression: 'rate(5 minutes)', State: 'ENABLED' }; cwevents.putRule(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.RuleArn); } });

For a complete list of Amazon SDK developer guides and code examples, see Using CloudWatch Events with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.