AWS for Industries

How to Integrate Your Headless Commerce Retail Store With Commercetools—Part 2

In our previous post How to Integrate Your Headless Commerce Retail Store With Commercetools—Part 1, we walked you through the process to integrate an ecommerce store front end with a backend retail microservice from the commercetools solution using commercetools’s APIs.

Built in the Amazon Web Services (AWS) cloud, commercetools provides a set of flexible REST and GraphQL APIs designed to meet the demanding needs of modern ecommerce solutions. The APIs create an interface for programmatic access to data stored on this solution and associated functionality. commercetools, an AWS Partner, offers a production-ready commerce solution that retailers can use to build high-quality, scalable, modern ecommerce architectures.

In part 2 of this blog series, we will show you how to use Amazon EventBridge—a serverless event bus that makes it easier to build event-driven applications at scale across AWS, existing systems, or software-as-a-service (SaaS) applications—to react to certain events that are generated in the commercetools’s solution. We’ll walk you through the process to implement a common customer engagement use case: sending an email campaign to announce a new product to customers.

commercetools_AWS-Blog_thumbnailAbout commercetools messages

The commercetools solution generates messages when a change or an action occurs on a resource, such as an order or a product. You can create notifications about new messages using the message queue service of your choice, such as Amazon Simple Queue Service (Amazon SQS), a fully managed message queuing service that transmits data across systems using messages, or Amazon EventBridge.

Because Amazon EventBridge provides an easy way to filter and route messages based on content, commercetools users can use the service to create scalable, dynamic event pipelines at low operational costs. Some other common use cases are:

  • Product review moderation and customer engagement—upon receiving a product review, you can use Amazon Comprehend, a natural-language processing (NLP) service that uses machine learning (ML) to uncover valuable insights and connections in text, to determine if the review is negative or offensive, and in the case of unfavorable sentiment, you can proactively contact the customer to address the situation.
  • Customer engagement—when a product is discounted, you can contact customers who previously viewed or bought the same or similar products and encourage them to come back to your store.
  • Order fulfilment—once an order is placed, you want to ship the item(s) from the closest facility to the customer. See this tutorial to learn more about how to implement this solution.

Process to implement new product email announcement use case

For this example, the flow is as follows:

  1. A store administrator creates and publishes a new product on the web front end. An API calls the commercetools’s APIs to create the new product on the backend.
  2. commercetools generates the “ProductPublishedMessage and delivers it to Amazon EventBridge.
  3. Then, Amazon EventBridge routes the message to a target in AWS Lambda, a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers.
  4. The AWS Lambda function calls the API on Amazon Pinpoint, a multichannel marketing communication service, through the AWS SDK to create a new email campaign and send a message to a segment of customers announcing the new product.

Process to implement new product email announcement use case

First, you need to build a customer segment in Amazon Pinpoint for the email campaign. In Amazon Pinpoint, a segment is a group of customers that share certain attributes. For example, you can create segments for all customers who visited your store in the past year or all customers who have placed an order. You should follow these instructions to build segments to target different sets of customers.

Next, you need to create the target AWS Lambda function to which Amazon EventBridge will route the ProductPublishedMessage. You can use the sample code for your AWS Lambda function. Be sure to use the segment that you created earlier and customize the message so that it’s appropriate for the target customer set. Please note: to send email messages through Amazon Pinpoint, you need to initiate the email channel and verify one sender email address.

From the Amazon EventBridge management console, configure a new partner event source integration for commercetools.

, configure a new partner event source integration for commercetools

To complete the commercetools-Amazon EventBridge integration, you need to create a subscription with commercetools Subscription API, as shown below.

$ curl https://api.eu-central-1.aws.commercetools.com/<YOUR_PROJECT_KEY>/subscriptions -sH "Authorization: Bearer <YOUR_AUTH_TOKEN>" -X POST -d @./create-subscription-payload.json

The payload you need to send is similar to the following:

{
    "key": "YOUR_SUBSCRIPTION_ID",
    "destination": {
        "type": "EventBridge",
        "accountId": "YOUR_AWS_ACCOUNT_ID",
        "region": "YOUR_AWS_REGION"
    },
    "messages": [
        {
            "resourceTypeId": "product",
            "types": [
                "ProductPublished"
            ]
        }
    ]
}

Back in the Amazon EventBridge management console, choose “Partner event sources” in the navigation pane. Select the button next to the partner event source with the same name as the one that you specified in your payload, and then choose “Associate with event bus.”

, choose “Partner event sources” in the navigation pane

You’ll see the status of the event source changed from “pending” to “active,” and the name of the event bus will update to match the partner event source name.

Next, you need to configure a rule to match the commercetools events by following these steps. In the “Define pattern” section, do the following:

  1. Choose “Event pattern.”
  2. Choose “Predefined pattern by service.”
  3. For “Service provider,” choose “Service partners.”
  4. For “Service name,” choose “commercetools.”
  5. Customize the event pattern to match only the “Product Published” Choose “Edit,” make your changes, and then “Save.”
{
  "account": ["<YOUR_AWS_ACCOUNT_ID>"],
  "detail": {
    "type": [ "ProductPublished" ]
  }
}

configure a rule to match the commercetools events

For “Service event bus,” be sure to select the event bus that’s associated with the partner event source that you created earlier.

As a target for this rule, select the Amazon Lambda function that you created earlier. Choose “Create” to create the rule.

Finally, you need to test the solution. From the commercetools Merchant Center, create and publish a new product. Once you’ve created a new product, you can verify the status of the campaign from the Amazon Pinpoint management console.

Get started today

We hope that you found this blog to be helpful. If you want to learn more about event-drive architectures with Amazon EventBridge, be sure to check out this AWS workshop.

If you want to discuss how a headless commerce architecture can benefit your retail business or you need help creating event-driven email campaigns with Amazon Pinpoint and commercetools, contact your AWS account team or visit Retail on AWS today to get started.


AWS Partner Spotlight

commercetools is a leading commerce platform built on modern MACH principles (microservice-based, API-first, cloud-native, headless), allowing retailers to work with, not around, their commerce solution to tailor experiences to the exact needs of the business and its customers.

Learn more about commercetools here.

Daniele Stroppa

Daniele Stroppa

Daniele Stroppa is the EMEA technical lead for AWS Partners in Retail. In his role, Daniele is responsible for solutions architecture and technical strategy for retail technology and consulting Partners on AWS. He’s been with AWS for 8 years, covering roles in the Amazon Elastic Compute Cloud (Amazon EC2) Container Service team as well as in the Solutions Architecture teams. Daniele’s also passionate about software development and helping developers deliver optimal results.