简体   繁体   中英

SQS send message error using @aws-sdk node js at AWS lambda

I am trying to send message from my AWS Lambda to AWS SQS but it isn't quiet working and throwing me the error.

2022-12-26T14:58:31.651Z    282ada00-ea4a-45b6-afe4-e3a7f16e8c5a    INFO    MissingParameter: The request must contain the parameter Label.
    at throwDefaultError (/var/task/node_modules/@aws-sdk/smithy-client/dist-cjs/default-error-handler.js:8:22)
    at deserializeAws_queryAddPermissionCommandError (/var/task/node_modules/@aws-sdk/client-sqs/dist-cjs/protocols/Aws_query.js:292:51)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/task/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
    at async /var/task/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:14:20
    at async /var/task/node_modules/@aws-sdk/middleware-retry/dist-cjs/retryMiddleware.js:27:46
    at async /var/task/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:5:22
    at async sendToSQS (/var/task/sendToSqs.js:28:20)
    at async exports.handler (/var/task/index.js:19:22) {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: 'bf137e9a-24bc-52bd-9416-22b99c6b82f5',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Type: 'Sender',
  Code: 'MissingParameter',
  Detail: ''
}

I am not sure what parameters and in which way I need to include to make it work. This is my code for sending message, where from my main module I send a simple data value as part of my message to be sent to SQS.

const { SQSClient, AddPermissionCommand } = require("@aws-sdk/client-sqs");


 const sendToSQS=async(data)=>{
    const client = new SQSClient({ region: "eu-west-1" });


    var params = {
      DelaySeconds: 0,
      MessageAttributes: {
        "Author": {
          DataType: "String",
          StringValue: "event params"
        },
      },
      MessageGroupId:`${data}`,
      MessageBody:JSON.stringify(data),
      QueueUrl:"https://sqs.eu-west-1.amazonaws.com/336070547288/Salesforce-cqd-orders-delayer-retry"
    };
    
    
    try{
        
    const command = new AddPermissionCommand(params);
        
    let queueRes = await client.send(command);
    
    console.info("[LAMBDA/@sqs] retry mechanism has succeeded. Data sent to SQS successfully")
    console.log(queueRes)
   
         const response = {
            statusCode: 200,
            body: "Data sent from lambda to sqs successfully.",
        }; 
    return response
            
        
        
    }catch(error){
        
        console.error("[LAMBDA/@s] retry mechanism has failed. Data wasn't sent to SQS")
        console.log(error)
        
            
        const response = {
            statusCode: 200,
            body: "Lambda to SQS error",
        };
    
        return response;
        
}


 }


 module.exports={sendToSQS}

Your message has delaySeconds which is not required and MessageGroupId which is only required for FIFO queue.

You can check sendMessage code reference from here AWS Wiki

Also, check this API reference for Send Message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM