Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
¿Por qué mi función Lambda envía un mensaje SQS dos veces con una llamada?

Solo necesito enviar un mensaje a una cola SQS estándar (no FIFO, pero no está relacionada con la pregunta), una vez con una llamada.

Sin embargo, el siguiente código envía 2 mensajes con 1 llamada.

 const AWS = require('aws-sdk') AWS.config.update({region: process.env.AWS_REGION}) const sqs = new AWS.SQS({apiVersion: '2012-11-05'}); async function sendToSQSEvent(body,attributes=null){ var m_body if (attributes != null) { m_body = { body : body, attributes : attributes }; } else{ m_body = body; } m_body = JSON.stringify(m_body); var params = { // DelaySeconds: 0, <-- i try but only delay reception MessageAttributes: { "Title": { DataType: "String", StringValue: "TIME_OUT" }, "Author": { DataType: "String", StringValue: "LAMBDA_IN" }, }, MessageBody: m_body, QueueUrl: "https://my_url/sqs" }; console.log('_________CALL_______________'); var r = await sqs.sendMessage(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.MessageId ,data); } }).promise(console.log("_________in promise___________")); console.log("___end") } exports.handler = async (event, context) => { await sendToSQSEvent(event) };

La salida de la consola es:

 START RequestId: RequestId Version: $LATEST 2021-10-11T06:23:52.992Z RequestId INFO _________CALL_______________ 2021-10-11T06:23:53.425Z RequestId INFO _________in promise___________ 2021-10-11T06:23:53.728Z RequestId INFO Success ********-****-****-****-*********b4f { ResponseMetadata: { RequestId: '********-****-****-****-*********89d' }, MD5OfMessageBody: '********************************8f', MD5OfMessageAttributes: '***********************1b0', MessageId: '********-****-****-****-*********b4f' } 2021-10-11T06:23:53.786Z RequestId INFO ___end 2021-10-11T06:23:53.807Z RequestId INFO Success ********-****-****-****-*********665 { ResponseMetadata: { RequestId: '********-****-****-****-********835' }, MD5OfMessageBody: '***********************28f', MD5OfMessageAttributes: '***********************1b0', MessageId: '********-****-****-****-*********665' } END RequestId: RequestId

¿Cual es el problema?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está enviando un mensaje dos veces mientras mezcla devoluciones de llamada síncronas ( function(err, data) ) con promesas asíncronas ( await , async function sendToSQSEvent(...) ).

Puede ver esto ya que CloudWatch está registrando 2 sqs.sendMessage(...) .

Recomendaría quedarse con este último.


Esta debería ser su lógica SQS sendMessage , que devuelve un objeto de promesa para su controlador.

 return sqs.sendMessage(params).promise();

Luego puede verificar la respuesta en su controlador:

 exports.handler = async (event, context) => { try { var data = await sendToSQSEvent(event) console.log("Success", data.MessageId ,data); } catch (err){ console.log("Error", err); } };

Este debería ser el resultado final de trabajo:

 const AWS = require('aws-sdk') AWS.config.update({ region: process.env.AWS_REGION }) const sqs = new AWS.SQS({ apiVersion: '2012-11-05' }); async function sendToSQSEvent(body, attributes = null) { var m_body if (attributes != null) { m_body = { body: body, attributes: attributes }; } else { m_body = body; } m_body = JSON.stringify(m_body); var params = { MessageAttributes: { "Title": { DataType: "String", StringValue: "TIME_OUT" }, "Author": { DataType: "String", StringValue: "LAMBDA_IN" }, }, MessageBody: m_body, QueueUrl: "https://my_url/sqs" }; return sqs.sendMessage(params).promise(); } exports.handler = async (event, context) => { try { var data = await sendToSQSEvent(event) console.log("Success", data.MessageId ,data); } catch (err){ console.log("Error", err); } };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!