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.
Follow the steps described in the AWS documentation Creating an Amazon SQS standard queue and sending a message.
Create a queue called
webhooks
and use the default settings.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
Create a
Secret Key
that is required in the later steps to set up the connection between Lambda and SQS.
Follow the steps described in the AWS documentation Where’s My Secret Access Key?
Save the
Access Key Id
andSecret Key
values to provide them later in the Lambda code.
Amazon Lambda
Create a Lambda function called
webhookFun
.
Follow the steps described in the AWS documentation Run a Serverless "Hello, World!" with AWS Lambda.
The function uses NodeJS 16 and JavaScript.
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
.
iii. when HMAC validation passes, the message body is sent with the request to the queue. Provide the QueueUrl
which points to your queue.
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.
Create an API Gateway of an
HTTP API
type.
Follow the steps described in the AWS documentation Create an HTTP API.
a. In the Integrations section, point to your Lambda.
b. For the API name, choose webhooks
.
c. In Routes, create a POST
route.
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.
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 tenanttoken
- your generated Emporix access token with relevant scopesdestinationUrl
- link to your API Gateway
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?