How to Create Rules in AWS For Shopify EventBridge Webhooks
Blog

How to Create Rules in AWS For Shopify EventBridge Webhooks

July 11th, 2022

Let's review how to create Rules for your Shopify Webhooks in AWS EventBridge. We'll use custom patterns to match the Webhook headersand Topics.

Sunrise Integration
  by Sunrise Integration

Webhooks, EventBridge and Shopify

 

When we're developing a Shopify App that needs to consume large volumes of webhook data, our Development Team uses AWS EventBridge to ensure data deliverability. Traditionally, webhooks are sent via HTTPS to server-based endpoints that process the data. As a Shopify store increases in popularity, large volumes of webhook-events can overwhelm the app infrastructure and server capability. AWS EventBridge (or Google Pub/Sub) is the best way to scale an app for enterprise-level demands. Amazon EventBridge is a serverless, event-driven alternative to receiving webhooks via standard HTTPS.

 

In our previous EventBridge blog post, we showed you how to connect a Shopify App to AWS EventBridge. In this blog, we will review the next step of processing the information. The incoming data needs to be validated and routed to the correct services. This is where the EventBridge Rules come into play.

 

Creating Rules for Shopify AWS EventBridge - Shopify Tutorial

 

Configure Your Rule In AWS

 

Once you've connected your Shopify webhooks to AWS Eventbridge, you can create rules for the event bus. A rule will test custom-created patterns to search for specific events. When a matching event occurs, the payload is routed to the target associated with the rule. These rules are created right from the AWS Console.

 

 

Create Patterns To Match Your Webhook

 

When EventBridge consumes a webhook, the raw JSON payload enters the event bus and is ready for further processing. With the EventBridge Rules, custom JSON is written to target values within the incoming payload. If the pattern matches, then the rule sends the event to the specified target. So, how do we create these patterns and what do they look like? Let's review a sample pattern to better understand.

 

Shopify provides sample patterns that you can use to test your events. These samples are a good way to learn about writing your own custom patterns. In the example below, the rule's pattern is written to target the webhook header-topic and only match the prefix of, "products/update":

 

{
  "detail-type": ["shopifyWebhook"],
  "detail": {
    "metadata": {
      "X-Shopify-Topic": [{
        "prefix": "products/update"
      }]
    }
  }
}

 

The EventBridge Rule uses the specified JSON values to target specific fields within the incoming webhook payload. Let's break-down the above JSON to better understand the rule and how it targets these values in the EventBridge payload.

 

detail-type: Different types of events can enter the bus. Each event will desiginate itself with the "detail-type" field. In this case, Shopify is labeling all platform webhooks as, "shopifyWebhook" The pattern above is telling EventBridge to search for events with a detail-type of "shopifyWebhook" The rule will ignore any other event that doesn't match this detail-type.

 

detail: The "detail' portion of the rule is where you further specify the values within the webhook to search for a matching event. You can tell the rule to search for orders, customers, or virtually anything within the incoming payload. In the example above, we targeted the "metadata" fields for the search. The pattern is looking for the, "X-Shopify-Topic" value within the metadata and designating "products/update" as the desired value to match.

 

If this sounds a little complicated, it might help to look at a sample of the incoming event payload. Reviewing the sample below will help you better understand the rule. Let's look at an EventBridge payload. This is the data sent to EventBridge from Shopify when a webhook is initiated. We'll use the sample EventBridge payload from Shopify for this example. We've highlighted key fields in the sample payload below to show which values the rule is targeting:

 

{
  "version": "0",
  "id": "1b8e2e75-b771-e964-f0e6-fbca6a21dad8",
  "detail-type": ["shopifyWebhook"],
  "source": "aws.partner/shopify.com/shop-event-bus",
  "account": "123456789",
  "resources": [""],
  "time": "2022-07-02T12:47:58Z",
  "region": "ca-central-1",
  "detail": {
    "payload": {
      "product" : {
        "id": 1,
        "title": "Columbia Las Hermosas",
        "body_html": "",
        "vendor": "Reunion Island",
        "product_type": "Coffee",
        "created_at": "2022-07-07T14:55:00-05:00",
        "handle": "columbia-las-hermosas",
      }
    },
    "metadata": {
      "X-Shopify-Topic": "products/update",
      "X-Shopify-API-Version": "2022-07",
      "X-Shopify-Hmac-SHA256": "rncNozYG6CCjmFJjEgUWowYJ60X+oUtsqul1sTwJMpU=",
      "X-Shopify-Shop-Domain": "{shop}.myshopify.com",
      "X-Shopify-Order-Id": 1
    }
  }
}

 

When EventBridge receives a JSON webhook from Shopify, the payload is formatted as you see above. We've highlighted the two key fields that we are targeting in our rule. You can see the "detail-type" and "metadata" fields in the payload. Our sample rule is simply telling AWS to look for these fields and match the values specified. Our rule is looking for topics that match, "products/update." Any other topic will not match the rule and therefore will not be processed by the rule. If you were looking for new products, the pattern would be changed to, "products/create" or even just, "products" to match any product-based webhook.

 

 

Processing Your Rules

 

When EventBridge consumes a webhook, the raw JSON payload can be routed to various destinations. The EventBridge Rule uses pattern matching to determine which events will "match" and thus be processed by the rule. These events can be routed to nearly any AWS service including lambda, SQS, SNS, API Gateway and more. Your specific needs may vary depending on the app design. Typically we'll route these to an SQS service to queue the events or send the data directly to a lambda function for processing.

 

Select an EventBridge event bus, EventBridge API destination (SaaS partner), or another AWS service as a target.

 

Designing Your App For Enterprise Needs

 

Sunrise Integration develops enterprise-level services that are highly scalable and resilient. AWS EventBridge allows us to design Shopify apps that utilize high volumes of webhook traffic while reducing infrastructure cost and complexity. By integrating with EventBridge, we can help your business grow in a cost-efficient manner.

 

If your project needs a Shopify Expert and AWS Development Partner, we've got your back.

 

RELATED ARTICLES

Tags:   aws   shopify   webhooks