HTTP Webhook Strategy - Integration with Amazon Simple Queue Service (SQS)

See the example of integration Amazon Simple Queue Service through webhooks.

Thanks to HTTP webhook strategy, you can choose the event service that enables communication between Emporix and external systems. This article demonstrates how you can connect Amazon Simple Queue Service to receive events notifications from Emporix. Follow the steps to set up the connection on both sides. Adjust the actual implementation to your needs. Amazon Simple Queue Service (SQS) is a service that allows for sending, storing, and receiving messages between distributed systems. As such, you can choose it as one of the solutions to connect Emporix with some other systems in your company through the webhooks functionality.

Preparing Amazon infrastructure

The integration between Emporix and Amazon Simple Queue Service in this example will use Amazon Lambda that is able to receive the HTTP request, validate HMAC signature and then send the request body to the specified queue.

Amazon Simple Queue Service (SQS)

To create SQS, go to the AWS Management Console.

  1. Create a queue called webhooks and use the default settings.

  2. Save the URL value of your queue, which you can see in the Details section. This is later required in the Lambda code.

Secret key

  1. Create a Secret Key that is required in the later steps to set up the connection between Lambda and SQS.

  1. Save the Access Key Id and Secret Key values to provide them later in the Lambda code.

Amazon Lambda

  1. Create a Lambda function called webhookFun.

The function uses NodeJS 16 and JavaScript.

  1. Modify the function's code to provide necessary details in the placeholders:

const AWS = require('aws-sdk');
const crypto = require('crypto');

// i
AWS.config.update({
  region: 'eu-north-1',
  accessKeyId: 'FILL_IT',
  secretAccessKey: 'FILL_IT',
});

const sqs = new AWS.SQS();

const secretKey = 'password123'; // ii

exports.handler = async (event, context) => {

  console.log(event);
  const body = JSON.stringify(event.body);
  const hmac = crypto.createHmac('sha256', secretKey);
  hmac.update(body);

  const computedHmac = hmac.digest('base64');
  
  if (computedHmac !== event.headers['emporix-event-signature']) {
    return {
      statusCode: 401,
      body: JSON.stringify("HMAC validation didn't pass."),
    };
  }
  
  try { // iii
    const messageParams = {
      MessageBody: body,
      QueueUrl: 'queue-url',
    };

    await sqs.sendMessage(messageParams).promise();

    return {
      statusCode: 200,
      body: JSON.stringify('The message was sent to SQS queue.'),
    };
  } catch (error) {
    console.error('Error:', error);
    return {
      statusCode: 500,
      body: JSON.stringify('An error occured during sending message to SQS queue.'),
    };
  }
};

Pay attention to the marked lines:

i. AWS.config - define relevant values to the following properties: region, accessKeyId and secretAccessKey.

ii. secretKey constant - provide the secret phrase for HMAC validation. You need to specify the same value in the webhooks HTTP configuration in the later steps. Here, we defined it as password123.

Find more information on how to configure HMAC validation in HTTP Webhook Strategy - HMAC Configuration.

iii. when HMAC validation passes, the message body is sent with the request to the queue. Provide the QueueUrl which points to your queue.

  1. Deploy the modified function.

API Gateway

To fully prepare Amazon's part, API Gateway is required so that it triggers the Lambda through HTTP requests.

  1. Create an API Gateway of an HTTP API type.

a. In the Integrations section, point to your Lambda.

b. For the API name, choose webhooks.

c. In Routes, create a POST route.

  1. Deploy the API Gateway and save the url to your function. It will be needed while defining webhooks HTTP configuration on Emporix side.

Configuring Emporix webhooks

Now, prepare Commerce Engine.

  1. Configure webhooks to send the events as HTTP POST requests. Use the following curl and provide relevant information in the placeholders:

curl -L 'https://api.emporix.io/webhook/{{tenant}}/config' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {{token}}' \
-d '{
    "code": "http",
    "active": true,
    "provider": "HTTP",
    "configuration": {
        "secretKey": "password123",
        "destinationUrl": "{{destinationUrl}}"
    }
}'

Placeholders to fill in:

  • tenant - name of your production tenant

  • token - your generated Emporix access token with relevant scopes

  • destinationUrl - link to your API Gateway

  1. Subscribe to a chosen event. In this example, we subscribe to the product creation event (remember to replace the values in the placeholders):

curl -L -X PATCH 'https://api.emporix.io/webhook/{{tenant}}/event-subscriptions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {{token}}' \
-d '[
  {
    "eventType": "product.created",
    "action": "SUBSCRIBE",
    "fieldsToSubscribe": [
      "name"
    ],
    "fieldsToUnsubscribe": [
      "description"
    ]
  }
]
 '

Creating product

The integration is ready. To see it in action, let's create a product.

Create a product with the following curl request providing your relevant details in the placeholders:

curl -L 'https://api.emporix.io/product/{{tenant}}/products/' \
-H 'Content-Language: en' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer {{token}}' \
-d '{
    "name": "Test product for documentation",
    "code": "unique",
    "description": "product description",
    "published": false
}'

After the product is created the event is sent. The webhook service consumes the event and sends it as HTTP request to the specified destinationUrl, which is the API Gateway. Then, the Lambda function receives the request and sends it to Amazon Simple Queue Service (SQS).

Last updated

Was this helpful?