Amazon SNS trigger to invoke a Lambda function

Using an Amazon SNS trigger to invoke a Lambda function

The Lambda function retrieves an SNS messages from the event parameter that it receives. The function logs the content of each message.

Select language:

Usage Example with Node:

Consuming an SNS event with Lambda using JavaScript.

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

async function processMessageAsync(record) {
  try {
    const message = JSON.stringify(record.Sns.Message);
    console.log(`Processed message ${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

Vaibhav Jain

Vaibhav Jain

AWS - Sr. Application Architect

Follow on LinkedIn

Majd Arbash

Majd Arbash

Solutions Architect at AWS

Follow on LinkedIn