AWS API Gateway to Node.js Lambda to SQS FIFO queue: Pass through event.body

I’ve used the serverless framework to create a Node.js Lamda function which is triggered by a POST event. Since I need wrap the event.body with additional details, my event handler builds up the payload in order to pass it to the SQS.sendMessage(payload) call.
It all works beautifully, until I need to pass through a payload that contains a more complex JSON body.
This works:
module.exports.handler = (event, context, callback) => {

var payload = createSqsPayload(event);

SQS.sendMessage(payload)
.promise()
.then(function(data){
var success_body = {
“message” : “Successfully received OrderCreatedResponse.”
}
var success_response = {
“statusCode”: 200,
“headers”: {},
“body”: JSON.stringify(success_body),
“isBase64Encoded”: false
};
callback(null, success_response);
})
.catch(function(err){
console.log(err);
var error_body = {
“message” : “Failed to process order response event”
}
var error_response = {
“statusCode”: 500,
“headers”: {},
“body”: JSON.stringify(error_body),
“isBase64Encoded”: false
};
callback(null, error_response);
})
}

function createSqsPayload(event) {
var body = JSON.parse(event.body);

var sqs_payload = {
“uuid” : uuidv4(),
“eventType”: EVENT_TYPE,
“timestamp” : new Date(),
“orderCreated”: {
“integratorReferenceId” : body.integratorReferenceId,
“orderId” : body.orderId,
“statusCode” : body.statusCode,
“integratorId” : body.integratorId,
“supplierIntegrationId” : body.supplierIntegrationId
},
“orderUpdated”: {
},
“attachmentCreated”: {
}
}

var payload = {
MessageBody: JSON.stringify(sqs_payload),
QueueUrl: QUEUE_URL,
MessageAttributes: {
“contentType”: {
DataType: “String”,
StringValue: “application/json”
}
},
MessageGroupId: MESSAGE_GROUP_ID
};
console.log(’\nCREATED_EVENT payload ‘+JSON.stringify(sqs_payload.timestamp)+’:\n’, payload);
return payload
}

What doesn’t work is when I try to do the next function which accepts and “orderUpdate” payload that contains an event.body with a JSON document that has repeatable nested objects and arrays.
In my createSqsPayloadEvent() - is it necessary to parse the event.body just to create the sqs_payload using the data from it? All I need to do is wrapper the payload into a compound JSON document in order to pass it through to the SQS FIFO queue.

If I need to do that, can anyone show me how to turn the following document into something that can be part of the SqsPayload?
{
“orderId”: “Lisa~Test~Order”,
“orderRequestId”: “30a7f16e-ba0e-4261-a219-8be40cb3101”,
“createdDate”: “2015-12-01T12:26:54.7523953Z”,
“preparedBy”: {
“firstName”: “Lisa”,
“lastName”: “Doe”,
“phone”: “001 (330) 309-6412”,
“fax”: “”,
“email”: null,
“addressLine1”: “4406 Highland Dr”,
“addressLine2”: “suite 117”,
“city”: “Akron”,
“stateProvince”: “OH”,
“country”: “USA”,
“postalCode”: “44322”
},
“orderedParts”: [
{
“partId”: “b4b61228-27a1-4ac2-bf4f-da5c30a5d7ec”,
“requestedPartId”: “de8a8821-da83-4889-b3dd-3a42fc7058a6”,
“partNumber”: “95245331”,
“estimateDescription”: “new description”,
“catalogDescription”: “N/A”,
“partType”: “PAN”,
“alternatePartNumber”: null,
“alternatePartType”: null,
“originalPartNumbers”: [
“MA1100188R”
],
“supplierResponseCode”: “Related”,
“partQuantity”: 1,
“estimatedDelivery”: “2015-06-13T12:30:00Z”,
“invoiceNumber”: “”,
“lineNumber”: “1”,
“estimateListPrice”: 0,
“oEListPrice”: 0,
“sellPrice”: 79.2,
“unit”: “each”,
“supplementLevel”: 1
},
{
“partId”: “2c2f2b14-45ef-45f3-9764-cc51a2312500”,
“requestedPartId”: “433a4d69-b6c8-4c2b-acfa-e0fbbdadfd5f”,
“partNumber”: “98765432114”,
“estimateDescription”: “new description”,
“catalogDescription”: “N/A”,
“partType”: “PAN”,
“alternatePartNumber”: null,
“alternatePartType”: null,
“originalPartNumbers”: [
“98765432114”
],
“supplierResponseCode”: “AsRequested”,
“partQuantity”: 1,
“estimatedDelivery”: “2015-06-13T12:30:00Z”,
“invoiceNumber”: “”,
“lineNumber”: “1”,
“estimateListPrice”: 0,
“oEListPrice”: 0,
“sellPrice”: 66,
“unit”: “each”,
“supplementLevel”: 1
}
],
“supplementLevel”: 1,
“code”: null,
“integratorId”: “integratorA”,
“supplierIntegrationId”: “23c98e18-233a-4ef2-b053-73177f6efc03”
}

With a little help from my friends… this issue has been resolved. Since we already had the body JSON object, we only needed to pass it through. Sigh. Sleep is required.

1 Like