Send EventBridge events using an Amazon SDK
The following code examples show how to send Amazon EventBridge events.
- .NET
-
- Amazon SDK for .NET
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. Send an event that matches a custom pattern for a rule.
/// <summary> /// Add an event to the event bus that includes an email, message, and time. /// </summary> /// <param name="email">The email to use in the event detail of the custom event.</param> /// <returns>True if successful.</returns> public async Task<bool> PutCustomEmailEvent(string email) { var eventDetail = new { UserEmail = email, Message = "This event was generated by example code.", UtcTime = DateTime.UtcNow.ToString("g") }; var response = await _amazonEventBridge.PutEventsAsync( new PutEventsRequest() { Entries = new List<PutEventsRequestEntry>() { new PutEventsRequestEntry() { Source = "ExampleSource", Detail = JsonSerializer.Serialize(eventDetail), DetailType = "ExampleType" } } }); return response.FailedEntryCount == 0; }
-
For API details, see PutEvents in Amazon SDK for .NET API Reference.
-
- C++
-
- SDK for C++
-
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. Include the required files.
#include <aws/core/Aws.h> #include <aws/events/EventBridgeClient.h> #include <aws/events/model/PutEventsRequest.h> #include <aws/events/model/PutEventsResult.h> #include <aws/core/utils/Outcome.h> #include <iostream>
Send the event.
Aws::CloudWatchEvents::EventBridgeClient cwe; Aws::CloudWatchEvents::Model::PutEventsRequestEntry event_entry; event_entry.SetDetail(MakeDetails(event_key, event_value)); event_entry.SetDetailType("sampleSubmitted"); event_entry.AddResources(resource_arn); event_entry.SetSource("aws-sdk-cpp-cloudwatch-example"); Aws::CloudWatchEvents::Model::PutEventsRequest request; request.AddEntries(event_entry); auto outcome = cwe.PutEvents(request); if (!outcome.IsSuccess()) { std::cout << "Failed to post CloudWatch event: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully posted CloudWatch event" << std::endl; }
-
For API details, see PutEvents in Amazon SDK for C++ API Reference.
-
- 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 putEBEvents(EventBridgeClient eventBrClient, String resourceArn, String resourceArn2 ) { try { // Populate a List with the resource ARN values. List<String> resources = new ArrayList<>(); resources.add(resourceArn); resources.add(resourceArn2); PutEventsRequestEntry reqEntry = PutEventsRequestEntry.builder() .resources(resources) .source("com.mycompany.myapp") .detailType("myDetailType") .detail("{ \"key1\": \"value1\", \"key2\": \"value2\" }") .build(); PutEventsRequest eventsRequest = PutEventsRequest.builder() .entries(reqEntry) .build(); PutEventsResponse result = eventBrClient.putEvents(eventsRequest); for (PutEventsResultEntry resultEntry : result.entries()) { if (resultEntry.eventId() != null) { System.out.println("Event Id: " + resultEntry.eventId()); } else { System.out.println("Injection failed with Error Code: " + resultEntry.errorCode()); } } } catch (EventBridgeException 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)
-
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 { EventBridgeClient } from "@aws-sdk/client-eventbridge"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon EventBridge service client object. export const ebClient = new EventBridgeClient({ 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-eventbridge"; import { ebClient } from "./libs/eventBridgeClient.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 ebClient.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();
-
For API details, see PutEvents in Amazon 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 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 ebevents = new AWS.EventBridge({apiVersion: '2015-10-07'}); var params = { Entries: [ { Detail: '{ \"key1\": \"value1\", \"key2\": \"value2\" }', DetailType: 'appRequestSubmitted', Resources: [ 'RESOURCE_ARN', ], Source: 'com.company.app' } ] }; ebevents.putEvents(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Entries); } });
-
For API details, see PutEvents in Amazon SDK for JavaScript API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note This is prerelease documentation for a feature in preview release. It is subject to change.
Note There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. suspend fun putEBEvents(resourceArn: String, resourceArn2: String) { // Populate a List with the resource ARN values. val resourcesOb = mutableListOf<String>() resourcesOb.add(resourceArn) resourcesOb.add(resourceArn2) val reqEntry = PutEventsRequestEntry { resources = resourcesOb source = "com.mycompany.myapp" detailType = "myDetailType" detail = "{ \"key1\": \"value1\", \"key2\": \"value2\" }" } val request = PutEventsRequest { entries = listOf(reqEntry) } EventBridgeClient { region = "us-west-2" }.use { eventBrClient -> val response = eventBrClient.putEvents(request) response.entries?.forEach { resultEntry -> if (resultEntry.eventId != null) { println("Event Id is ${resultEntry.eventId}") } else { println("Injection failed with Error Code ${resultEntry.errorCode}") } } } }
-
For API details, see PutEvents
in Amazon SDK for Kotlin API reference.
-
For a complete list of Amazon SDK developer guides and code examples, see Using EventBridge with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.