Add a target 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.

Add a target using an Amazon SDK

The following code examples show how to add a target to an Amazon CloudWatch Events event.

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

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 { PutTargetsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutTargetsCommand({ // The name of the Amazon CloudWatch Events rule. Rule: process.env.CLOUDWATCH_EVENTS_RULE, // The targets to add to the rule. Targets: [ { Arn: process.env.CLOUDWATCH_EVENTS_TARGET_ARN, // The ID of the target. Choose a unique ID for each target. Id: process.env.CLOUDWATCH_EVENTS_TARGET_ID, }, ], }); 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 = { Rule: 'DEMO_EVENT', Targets: [ { Arn: 'LAMBDA_FUNCTION_ARN', Id: 'myCloudWatchEventsTarget', } ] }; cwevents.putTargets(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

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.