Use PutRule with an AWS SDK or command line tool - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use PutRule with an AWS SDK or command line tool

The following code examples show how to use PutRule.

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 AWS 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 AWS 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 AWS Code Examples Repository.

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();

Create the client in a separate module and export it.

import { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; export const client = new CloudWatchEventsClient({});
SDK for JavaScript (v2)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS 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); } });