Salesforce to AWS

Relay Events from Salesforce to AWS guide

Salesforce recently launched relay events that are based on event-driven applications. It can be used to send events from Salesforce to AWS Amazon EventBridge without having to build an integration or use middleware. The  Event Relay service subscribes to Salesforce platform events and Change Data Capture events and relays them to EventBridge.EventBridge can be used by AWS developers to route events to AWS services or Salesforce.

Salesforce Event Relay implementation

To implement Salesforce Event Relay, we can use Salesforce Streaming APIs, which allow us to send real-time notifications to clients via the publish-subscribe model. Certain types of notifications are triggered by changes in Salesforce data. Change data capture events and PushTopic events can both trigger these notifications. Other notifications include data that you specify. Platform events and generic streaming are among them.

Because change data capture is the most recent generation of streaming APIs for pushing real-time changes, we are using it for event relay implementation.

Quick Read: Salesforce DevOps Center- Quick Setup Example

The steps for creating an event relay configuration in Salesforce for a custom Change Data Capture are outlined below:

  1. Enable Change Data Capture for the object you want to sync data.

    Steps to Enable Change Data Capture:
    • From  Setup, in the Quick Find box, enter Change Data Capture, and then select Change Data Capture.
      Salesforce to AWS
    • Select the object and scroll right, then click to save.
  2. Create a Channel for a Change Data Capture
    A channel is a continuous stream of events. It has members that define the kinds of events that can be received over the channel. A channel can only contain a stream of platform events or change data capture events. Here we are using change data capture. Steps to create a channel using Workbench:
    • Visit https://workbench.developerforce.com site.
    • Log in with your Salesforce account.
    • Click Rest Explorer under Utilities.
      Salesforce to AWS
    • Send a POST request to this URI: /services/data/v56.0/tooling/sobjects/PlatformEventChannel
      Use this example request body:

      Note – To configure a channel that receives platform events set channelType to ‘event’, as “channelType”: “event”.

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

      Salesforce to AWS
  3. Create a Channel Member to Associate the Change Data Capture Event

    Channel Member is used to associating an event with a channel. A channel can have multiple channel members (PlatformEventChannelMember), which means you can add multiple platform events or change data capture to a channel.

    • Send a POST request to this URI:

    • Use this example request body:

      CDC
    • Send the request. The response received will looks like
      Salesforce to AWS
  4. Create a Named Credential.
     
    AWS account information and authentication configuration are stored in a named credential. You use the named credential later to set up the event relay configuration.

    Send a POST request to this URI:
    • Use this example request body, and replace XXXXXXXXXXXX in the endpoint field with a valid 12-digit AWS account ID and arn:aws:aws_region. Replace the aws_region placeholder with your AWS region:
      {
      "FullName" : "MyRelayNamedCredential",
      "Metadata" : {
      "endpoint" : "arn:aws:US-WEST-2:XXXXXXXXXXXX",
      "generateAuthorizationHeader" : true,
      "label" : "MyRelayNamedCredential",
      "principalType" : "NamedUser",
      "protocol" : "NoAuthentication"
      }
      }


      Note- US-WEST-2 is an AWS region that you can relocate in the AWS account.

      Here ap-south-1 is an AWS region – Use it in Upper case as AP-SOUTH-1
    • Send the request. The response received looks similar to this response
      Salesforce to AWS
  5. Create an Event Relay Configuration

    Using the named credential, a relay configuration connects the channel in the Salesforce event bus to Amazon EventBridge. The creation of the event relay configuration results in the creation of a partner event source in Amazon EventBridge. Pending status is assigned to the partner event source.
    • Send a POST request to this URI:
      /services/data/v56.0/tooling/sobjects/EventRelayConfig/
    • Request Body:
      {
      "FullName": "MyEventRelay",
      "Metadata": {
      "destinationResourceName": "callout:MyRelayNamedCredential",
      "eventChannel": "MyRelayChangeEventChannel__chn"
      }
      }
    • Send the request. The response received looks similar to this response:
      Salesforce to AWS
    • Save the returned ID value of the new event relay as we will use it later.
  6. Activate the Event Bus in AWS Amazon EventBridge

    When you create an event relay configuration in Salesforce, it creates a pending partner event source in Amazon EventBridge. Connect the event bus to the partner event source so that it is ready to receive events from Salesforce.
    • Run the query using the Query Editor in the Developer Console:

      SELECT Id, RemoteResource, Status, ErrorMessage, ErrorTime, ErrorIdentifier FROM EventRelayFeedback WHERE EventRelayConfigId = 'Relay_Id_Placeholder'
    • Replace Relay_Id_Placeholder with the EventRelayConfig ID that you saved earlier (example – 7k2RM0000004LoAYAU). The query returns a response similar to this response:
      Salesforce to AWS
    • Copy the RemoteResource value. Navigate to https://aws.amazon.com, and sign in using your AWS account credentials.
    • In the Search box, type Amazon EventBridge, and click Amazon EventBridge under Services. In Amazon Eventbridge, under Integration, click Partner event sources.
      Salesforce to AWS
    • In the search box, enter the name of your event source, the RemoteResource value, that you just obtained in the query.
    • Select your event source, and click Associate with event bus. The status of the event source changes to Active.
      Salesforce to AWS
  7. Start the Event Relay

    The event relay configuration is created in the stopped state, and no events are relayed from Salesforce to AWS until the event relay is started. This extra step allows you to double-check that you have the correct AWS account and region configuration. After verifying the AWS information, begin the event relay by updating the state field in EventRelayConfig.
    • Get the ID of the event relay configuration that you created earlier. The ID is in the format 7k2XXXXXXXXXXXXXXX.
    • Send a PATCH request to this URI after replacing the placeholder value for the event relay ID:
      /services/data/v56.0/tooling/sobjects/EventRelayConfig/<EventRelayConfigId>
    • If you use Postman, under Tooling, click Post Tooling SObject, change POST to PATCH, replace the :SOBJECT_API_NAME placeholder with EventRelayConfig/, and then append the event relay config ID.
    • Use this request body for the PATCH request, and replace the FullName value with the full name of EventRelayConfig:
      {
        "FullName": "MyEventRelay",
        "Metadata": {
         "state": "RUN"
        }
      }


      Note: A successful PATCH operation returns the 204 No Content HTTP status.
  8. Create an EventBridge Rule for Logging Events in a CloudWatch Log

    To test the connection between Salesforce and AWS and ensure that the event messages are received in EventBridge, add a CloudWatch log as a target of a rule.
    • In Amazon Eventbridge, click Rules. Select your event bus from the dropdown. The name of the event bus is the same as the name of the partner event source that you queried earlier. It’s in this format: aws.partner/salesforce.com/orgID/channelID
    • In the Rules section, click Create rule. Provide a name for your rule. Click Next.
    • Under Event source, select AWS events or EventBridge partner events. Skip the Sample event section. In the Event pattern, for Event source, select EventBridge partners.
      • From the partner dropdown, select Salesforce.
      • For Event type, select All Events. The event pattern box auto-populates to this value.
        Salesforce to AWS
    • Click Next. Under Target 1, Target types, select AWS service. Under Select a target, select CloudWatch log group.
    • Complete the log group path. For example: /aws/events/mygroup/log.
    • Click Next and then Next. Review the rule that you created, and then click Create rule.

Quick Read: Salesforce Flow Framework

To Test Salesforce to AWS 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 an event from Salesforce by just simply creating account records. You will see the log created in the log stream.
    Salesforce to AWS
  4. Click on the recently created log and you will see the data.

I hope this article has helped you to implement relay events in the simplest way possible. If you have any questions, please leave a comment below or contact me on LinkedIn. Until then keep learning and may the force be with you.

Share: