Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

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;
  }
}

                                                  



GitHub icon Visit the GitHub repo for this snippet.

Mohammed Tomeh

Presented by Mohammed Tomeh

Cloud Application Architect at AWS

Follow on LinkedIn

Vaibhav Jain

Presented by Vaibhav Jain

AWS - Sr. Application Architect

Follow on LinkedIn

Majd Arbash

Presented by Majd Arbash

Solutions Architect at AWS

Follow on LinkedIn