Gunakan PutRule dengan AWS SDK - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan PutRule dengan AWS SDK

Contoh kode berikut menunjukkan cara menggunakanPutRule.

Java
SDKuntuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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); } } }
  • Untuk API detailnya, lihat PutRuledi AWS SDK for Java 2.x APIReferensi.

JavaScript
SDKuntuk JavaScript (v3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Impor modul SDK dan klien dan panggil fileAPI.

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

Buat klien dalam modul terpisah dan ekspor klien tersebut.

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

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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