Send CloudWatch Events events using an AWS SDK
The following code examples show how to send Amazon CloudWatch Events events.
- 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
. 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 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
. 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 { PutEventsCommand } from "@aws-sdk/client-cloudwatch-events"; import { client } from "../libs/client.js"; const run = async () => { const command = new PutEventsCommand({ // The list of events to send to Amazon CloudWatch Events. Entries: [ { // The name of the application or service that is sending the event. Source: "my.app", // The name of the event that is being sent. DetailType: "My Custom Event", // The data that is sent with the event. Detail: JSON.stringify({ timeOfEvent: new Date().toISOString() }), }, ], }); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see PutEvents in AWS SDK for JavaScript API Reference.
-
- 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 = { 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 more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see PutEvents in AWS SDK for JavaScript API Reference.
-
For a complete list of AWS SDK developer guides and code examples, see Using CloudWatch Events with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.