Send CloudWatch Events events 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.

Send CloudWatch Events events using an Amazon SDK

The following code examples show how to send Amazon CloudWatch Events events.

Java
SDK for Java 2.x
Tip

To learn how to set up and run this example, see GitHub.

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.

JavaScript
SDK for JavaScript V3
Tip

To learn how to set up and run this example, see GitHub.

Create the client in a separate module and export it.

import { CloudWatchEventsClient } from "@aws-sdk/client-cloudwatch-events"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch service client object. export const cweClient = new CloudWatchEventsClient({ region: REGION });

Import the SDK and client modules and call the API.

// Import required AWS SDK clients and commands for Node.js import { PutEventsCommand } from "@aws-sdk/client-cloudwatch-events"; import { cweClient } from "./libs/cloudWatchEventsClient.js"; // Set the parameters export const params = { Entries: [ { Detail: '{ "key1": "value1", "key2": "value2" }', DetailType: "appRequestSubmitted", Resources: [ "RESOURCE_ARN", //RESOURCE_ARN ], Source: "com.company.app", }, ], }; export const run = async () => { try { const data = await cweClient.send(new PutEventsCommand(params)); console.log("Success, event sent; requestID:", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2
Tip

To learn how to set up and run this example, see GitHub.

// 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 = { Entries: [ { Detail: '{ \"key1\": \"value1\", \"key2\": \"value2\" }', DetailType: 'appRequestSubmitted', Resources: [ 'RESOURCE_ARN', ], Source: 'com.company.app' } ] }; cwevents.putEvents(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });

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.