Connecting Salesforce with Amazon EventBridge: An Event-Driven Solution

Sonali Takke
Cloudwerx
Published in
7 min readMay 2, 2023

--

Connecting with AWS is a frequently encountered requirement in Salesforce integration projects. This blog post will outline a detailed guide on how to utilize Event Bus Relay for publishing platform events from Salesforce to AWS, without the need for creating an integration application or utilizing any middleware.

Throughout this blog, we will be using the POSTMAN tool to send a POST request to Salesforce URIs. For every request, we have set the Authorization Type to Oauth 2.0 and specified the access token. (If you are not aware of the steps to generate an access token refer to this blog)

1. Define a Platform Event

You can create a new one or utilize any existing custom platform event specified within your Salesforce organization.

2. Create a Channel for a Custom Platform Event

First, we will create a channel that holds a stream of custom platform events.

  1. Choose the POST method since we are going to create a Channel.
  2. Enter the following URL:
{instance URL}/services/data/v57.0/tooling/sobjects/PlatformEventChannel

3. Pass the following parameters in the Body:

  • From available Radio Group Options select raw.
  • Expand the text dropdown and select JSON.
  • Use below given example request body. You can specify any channel Label and name.
{
"FullName": "DemoRelayChannel__chn",
"Metadata": {
"channelType": "event",
"label": "Custom Channel for Relaying Platform Events"
}
}
  • Set the authorization as Oauth 2.0 and specify the access token.
  • Send the request. The response received looks similar to this response.

3. Create a Channel Member to Associate a Custom Platform Event

After creating the channel for platform events, proceed to include a channel member in it.

  1. Choose the POST method since we are going to create a Channel member.
  2. Enter the following URL:
{instance URL}/services/data/v57.0/tooling/sobjects/PlatformEventChannelMember

3. Use below given example request body. Specify the channel name against the eventChannel key and the platform event name against the selectedEntity key.

{
"FullName": "DemoRelayChannel_chn_AWSEvent_e",
"Metadata": {
"eventChannel": "DemoRelayChannel__chn",
"selectedEntity": "AWSEvent__e"
}
}

4. Send the request. The response received looks similar to this response.

4. Create a Named Credential

You can generate a named credential through either the Salesforce user interface in Setup or by utilizing the Tooling API. It will store your AWS account information and authentication configuration.

  1. Choose the POST method since we are going to create a Platform Event in Salesforce.
  2. Enter the following URL:
{instance URL}/services/data/v57.0/tooling/sobjects/NamedCredential

3. Use this example request body. Replace XXXXXXXXXXXX in the endpoint field with a valid 12-digit AWS account ID and US-EAST-1 with the region for your AWS account.

{
"FullName" : "AWSNamedCredential",
"Metadata" : {
"endpoint" : "arn:aws:US-EAST-1:XXXXXXXXXXXX",
"generateAuthorizationHeader" : true,
"label" : "AWSNamedCredential",
"principalType" : "NamedUser",
"protocol" : "NoAuthentication"
}
}

4. Send the request. The response received looks similar to this response.

5. Create an Event Relay Configuration

Event Relay automatically delivers events to Amazon EventBridge. It uses named credentials to associate a channel in the Salesforce event bus with the Amazon EventBridge.

  1. Choose the POST method since we are going to create an Event Relay configuration in Salesforce.
  2. Enter the following URL:
{instance URL}/services/data/v57.0/tooling/sobjects/EventRelayConfig/

3. Use this example request body. Replace the destinationResourceName value with the callout:namedcredential. Also, replace the value for eventChannel with the event channel which we created earlier.

{
"FullName": "AWSEventRelay",
"Metadata": {
"destinationResourceName": "callout:AWSNamedCredential",
"eventChannel": "DemoRelayChannel__chn"
}
}

4. Send the request. The response received looks similar to this response.

6. Activate the Event Bus in Amazon EventBridge

Upon creating an event relay configuration within Salesforce, a partner event source will be generated in Amazon EventBridge. However, the event source will have a pending status until further steps are taken to authorize the connection between the source and the relay.

  1. You can use Query editor or POSTMAN to query on the EventRelayFeedback object to get the RemoteResource field value.
  2. Using a POSTMAN, Choose the GET method since we are querying EventRelay configuration from Salesforce.
  3. Enter the following URL and replace {Relay_Id_Placeholder} with the EventRelayConfig ID that you saved earlier.:
{instance URL}/services/data/v57.0/query/?q=SELECT Id, RemoteResource, Status, ErrorMessage, ErrorTime, ErrorIdentifier FROM EventRelayFeedback WHERE EventRelayConfigId = {Relay_Id_Placeholder}

4. Send the request. The response received looks similar to this response. Copy the RemoteResource value.

5. Navigate to https://aws.amazon.com, and sign in using your AWS account credentials.

6. In the Search box, type Amazon EventBridge, and click Amazon EventBridge under Services.

7. In Amazon Eventbridge, under Integration, click Partner event sources.

8. Select your event source, and click Associate with event bus.

9. The status of the event source changes to Active

7. Start the Event Relay

After creating an event relay configuration in Salesforce, it is set to a stopped state by default. It’s necessary to update the state field in EventRelayConfig to begin relaying events between the two systems. Once the event relay is started, it will continuously relay events until it is stopped or deleted.

  1. Choose the PATCH method since we are updating EventRelayConfig in Salesforce.
  2. Enter the following URL and replace <EventRelayConfigId> with the ID of the event relay configuration that you created earlier. The ID is in the format 7k2XXXXXXXXXXXXXXX.
{instance URL}/services/data/v57.0/tooling/sobjects/EventRelayConfig/<EventRelayConfigId>

3. Use this example request body, and replace the FullName value with the full name of EventRelayConfig.

{
"FullName": "AWSEventRelay",
"Metadata": {
"state": "RUN"
}
}

4. Send the request. A successful PATCH operation returns the 204 No Content HTTP status.

5. we can now validate receiving the events in AWS.🤩

8. Create an EventBridge Rule

To verify that the connection between Salesforce and AWS is functioning correctly and that event messages are being received in EventBridge, one way to test it is to add a CloudWatch log as a target of a rule. By doing so, the event messages received by EventBridge can be forwarded to the CloudWatch log. This allows you to review the log and ensure that the event messages are being delivered correctly.

  1. In Amazon EventBridge, click Rules.
  2. Select your event bus from the dropdown. The name of the event bus will be the same as the name of the partner event source. It’s in this format: aws.partner/salesforce.com/orgID/channelID
  3. In the Rules section, click Create Rule button.

4. Provide a name for your rule. Click Next.

5. Under Event source, select AWS events or EventBridge partner events.

6. Skip the Sample event section. In the Event pattern, for Event source, select EventBridge partners.

7. From the partner dropdown, select Salesforce.

8. For Event type, select All Events. The event pattern box auto-populates to this value. Click Next.

9. Under Target 1, Target types, select AWS service.

10. Under Select a target, select CloudWatch log group. Complete the log group path. For example: /aws/events/mygroup/log.

11. Click Next and then Next.

12. Review the rule that you created, and then click Create rule.

To test receiving events, open the CloudWatch log.

  1. Click the rule that you just created.
  2. Under Targets, click the log. The CloudWatch log opens in a new tab.

3. Publish a platform event from Salesforce using any medium like flow, apex, etc. Refer below example for publishing an event using POSTMAN.

Publish Platform Event

4. Refresh the CloudWatch log stream. The received event is displayed in the log group, similar to this event.

Conclusion:

Once events are received in Amazon EventBridge, there are several options for processing them. You can use rules to process events within AWS, or you can send them to third-party and SaaS integrations using API destinations.

I hope this blog is helpful !!

If you liked this blog, click on the 👏 button below and share this blog with those who want to make their Salesforce and AWS integrations easier!
In case you want to connect with me on LinkedIn: My Linkedin Profile.

🤠Special Note: If you found our information valuable and would like to hear more about how Cloudwerx can help take your business to the next level, we’d love to hear from you.
Get in touch with us at
www.cloudwerx.co or hello@cloudwerx.co !

--

--