Sample Amazon SQS function code

Sample Amazon SQS function code

The Lambda function retrieves a batch of SQS messages from the event parameter that it receives. The function logs the content of each message. This snippet demostrates SQS Lambda integration without batch item handling.

Select language:

Usage Example with Node:

Sample Amazon SQS function code using JavaScript without batch item handling.

exports.handler = async (event, context) => {
  for (const message of event.Records) {
    await processMessageAsync(message);
  }
  console.info("done");
};

async function processMessageAsync(message) {
  try {
    console.log(`Processed message ${message.body}`);
    // TODO: Do interesting work based on the new message
    await Promise.resolve(1); //Placeholder for actual async work
  } catch (err) {
    console.error("An error occurred");
    throw err;
  }
}

                                                      



Created by:

Mohammed Tomeh

Mohammed Tomeh

Cloud Application Architect at AWS

Follow on LinkedIn

Girish B

Girish B

Senior Solutions Architect at AWS

Follow on LinkedIn

Sanskar

Sanskar

Sanskar is a Serverless Cloud Engineer at Amazon Web Services and is based in India. Sanskar has avid interest in microservices and serverless patterns.

Follow on LinkedIn