skip to Main Content

I have a Lambda function that has some data and must send it to a SQS queue. But it seems like the send is not working. There is no error and it seems to gracefully continue execution without any message being sent. Here is my Lambda code:

  //JavaScript

  import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
  
  const message = {
    label: "test",
  };

  const client = new SQSClient({});
  const SQS_QUEUE_URL = "https://sqs.us-east-1.amazonaws.com/...";

  const send = async (message, sqsQueueUrl = SQS_QUEUE_URL) => {
    const command = new SendMessageCommand({
      QueueUrl: sqsQueueUrl,
      DelaySeconds: 5,
      MessageAttributes: {
        Label: {
          DataType: "String",
          StringValue: message.label,
        },
      },
      MessageBody:
        "Sent test data to SQS queue",
    });
    const response = await client.send(command);
    return response;
  };
  
  const doSend = async(message, SQS_QUEUE_URL) => {
    const response = await send(message, SQS_QUEUE_URL);
    response.then(function() {
      console.log("Message has been sent to SQS Q1");
    }).catch(function() {
      console.log("Something went wrong");
    });
  };
  doSend(message, SQS_QUEUE_URL);

Viewing the logs on CloudWatch, I can see the "Test" message being printed, but not the response. I have other things after the send(...), all that code works, it’s just the send(...) that isn’t doing anything. My runtime is Node.js 20.x.

I have already attached a policy to provide full SQS access for my Lambda function. I also included the region when initializing the client, didn’t seem to make a difference.

2

Answers


  1. Your send method is returning a promise so you have to wait for the response(await send(message, SQS_QUEUE_URL)) in order to fulfill the request made by the SQS client(client.send), otherwise the lambda instance will finish before. I don’t know how you are setting up the lambda handler but I’ll give you an example of how you should restructure the code.

    import { SendMessageCommand, SQSClient } from "@aws-sdk/client-sqs";
    
    const message = {
      label: "test",
    };
    
    const client = new SQSClient({});
    const SQS_QUEUE_URL = "https://sqs.us-east-1.amazonaws.com/...";
    
    const send = async (message, sqsQueueUrl = SQS_QUEUE_URL) => {
      const command = new SendMessageCommand({
        QueueUrl: sqsQueueUrl,
        DelaySeconds: 5,
        MessageAttributes: {
          Label: {
            DataType: "String",
            StringValue: message.label,
          },
        },
        MessageBody:
          "Sent test data to SQS queue",
      });
      console.log("Test");
      const response = await client.send(command);
      console.log(response);
      return response;
    };
    
    export const handler = async (event) => {
      // Your code here
    
      await send(message, SQS_QUEUE_URL);
    
      // Your code here
    };
    
    Login or Signup to reply.
  2. Could you please try to catch any errors and let us know what the error message is? If no error occurs, the MessageID should be printed

    const doSend = async (message, sqsQueueUrl) => {
      try {
        const response = await send(message, sqsQueueUrl);  
        console.log("Message has been sent to SQS Q1:", response.MessageId);   
      } catch (error) {   
        console.error("Something went wrong:", error);
      }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search