How to do it…

Follow these steps to create a simple AWS Lambda function that processes messages from an Amazon Simple Queue Service (SQS) queue. The ability to automatically process all the messages in a queue with a Lambda function is an incredibly valuable feature that was announced by AWS in 2018. Anyone who has written code to poll a queue for messages knows that it can be surprisingly hard to do correctly, especially at scale with distributed systems. Adding this tool to your belt will allow you to tackle a wide variety of architectural challenges:

  1. Go to the SQS dashboard and create a new queue. Name the queue MySampleQ, choose Standard Queue, and click Quick-Create Queue.
  2. Go to the AWS Lambda dashboard and click Create function.
  3. Choose Author from scratch and name the function MyQReader.

 

  1. Choose Node.js 8.10 as the runtime and click Create function.
  2. Scroll down to the function code and paste in the following:
exports.handler = async (event) => {
// Log the entire event
console.info('event', JSON.stringify(event))
let numRecordsProcessed = 0
event.Records.forEach(record => {
// Process the message from SQS here
console.log(record.body);
numRecordsProcessed++
});
const response = {
statusCode: 200,
body: JSON.stringify(`${numRecordsProcessed} messages handled!`),
};
return response;
};
  1. Your Lambda function needs additional privileges to interact with SQS. Scroll down to the execution role and click the role name to view it in the IAM console.
  2. Expand the policy and click Edit policy.
  3. Click the JSON tab and add the following policy statement to the array of statements that are already present in the policy. Note that the best practice is to replace the asterisk (*) in the Resource property with the actual ARN of your SQS queue:
        {
"Effect": "Allow",
"Action": [
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes"
],
"Resource": "*"
}
  1. Go back to your browser tab with the Lambda function.
  2. Scroll down to Designer and select SQS from the event sources.
  3. Select MySampleQ from the dropdown under Configure triggers:

Configuring triggers
  1. Click Add and then Save the function.
  2. Open a new tab (leaving the Lambda tab open) and go to the SQS dashboard. 

 

  1. Select MySampleQ and, from the Queue Actions dropdown, select Send a Message:

Sending a message
  1. Type some text into the box and click Send Message.
  2. Go back to the Lambda function and click the Monitoring tab. Click View logs in CloudWatch.

 

  1. On the CloudWatch page, click the latest log stream and check out the log messages that have been sent from your Lambda function to confirm that the message was successfully processed:

CloudWatch logs being sent from the Lambda function
  1. Delete the Lambda function and the SQS queue to avoid future charges.

Delegating work to a queue can drastically improve the perceived performance of your web applications, and there is no easier way to accomplish queue processing than to wire up a Lambda function to SQS. Using a queue also helps you decompose your application into decoupled, separately deployable components, which reduces risks when you're making future changes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset