const AWSXRay = require('/opt/node_modules/aws-xray-sdk'); const AWS = AWSXRay.captureAWS(require('/opt/node_modules/aws-sdk')); const sqs = new AWS.SQS(); exports.handler = async (event, context) => { // Enable X-Ray tracing for this function const segment = AWSXRay.getSegment(); const subsegment = segment.addNewSubsegment('InserFix-function'); try { // get device id device_id = event['pathParameters']['device_id']; // Parse the request body let payload; if (event.body) { try { payload = JSON.parse(event.body); } catch (err) { return { statusCode: 400, body: JSON.stringify('Invalid JSON payload'), }; } } else { return { statusCode: 400, body: JSON.stringify('No body for payload found!'), }; } payload.device_id = device_id; const params = { QueueUrl: process.env.QUEUE_URL, // SQS queue URL MessageBody: JSON.stringify(payload), // Message body is the event data MessageAttributes: { Source: { DataType: 'String', StringValue: 'api-gateway' }, TraceId: { DataType: 'String', StringValue: AWSXRay.getSegment().trace_id } } }; // Send message to SQS const sendMessageSubsegment = subsegment.addNewSubsegment('sendMessageToSQS'); const sqsResponse = await sqs.sendMessage(params).promise(); sendMessageSubsegment.close(); return { statusCode: 200, body: JSON.stringify('Payload added to QUEUE!'), }; } catch (err) { console.log('Error: ', err); return { statusCode: 400, body: JSON.stringify('Something broke'), }; } };