concept message in category aws

This is an excerpt from Manning's book Amazon Web Services in Action, Second Edition.
Now you can run the script with node index.js "http://aws.amazon.com". The program should respond with something like “PNG will be available soon at http://url2png-$yourname.s3-website-us-east-1.amazonaws.com/XYZ.png”. To verify that the message is ready for consumption, you can ask the queue how many messages are inside. Replace $QueueUrl with your queue’s URL.
$ aws sqs get-queue-attributes \ --queue-url "$QueueUrl" \ --attribute-names ApproximateNumberOfMessages { "Attributes": { "ApproximateNumberOfMessages": "1" } }SQS only returns an approximation of the number of messages. This is due to the distributed nature of SQS. If you don’t see your message in the approximation, run the command again and eventually you will see your message.
Next, it’s time to create the worker that consumes the message and does all the work of generating a PNG.
1. Receive a message.
2. Process the message.
Listing 15.3. worker.js: receiving a message from the queue
const fs = require('fs'); const AWS = require('aws-sdk'); const webshot = require('webshot'); const sqs = new AWS.SQS({ region: 'us-east-1' }); const s3 = new AWS.S3({ region: 'us-east-1' }); const receive = (cb) => { const params = { QueueUrl: '$QueueUrl', MaxNumberOfMessages: 1, #1 VisibilityTimeout: 120, #2 WaitTimeSeconds: 10 #3 }; sqs.receiveMessage(params, (err, data) => { #4 if (err) { cb(err); } else { if (data.Messages === undefined) { #5 cb(null, null); } else { cb(null, data.Messages[0]); #6 } } }); };The receive step has now been implemented. The next step is to process the message. Thanks to the Node.js module webshot, it’s easy to create a screenshot of a website.
Listing 15.4. worker.js: processing a message (take screenshot and upload to S3)
const process = (message, cb) => { const body = JSON.parse(message.Body); #1 const file = body.id + '.png'; webshot(body.url, file, (err) => { #2 if (err) { cb(err); } else { fs.readFile(file, (err, buf) => { #3 if (err) { cb(err); } else { const params = { Bucket: 'url2png-$yourname', Key: file, ACL: 'public-read', #4 ContentType: 'image/png', Body: buf }; s3.putObject(params, (err) => { #5 if (err) { cb(err); } else { fs.unlink(file, cb); #6 } }); } }); } }); };The only step that’s missing is to acknowledge that the message was successfully consumed. This is done by deleting the message from the queue after successfully completing the task. If you receive a message from SQS, you get a ReceiptHandle, which is a unique ID that you need to specify when you delete a message from a queue.
Now you can start the worker to process the message that is already in the queue. Run the script with node worker.js. You should see some output that says the worker is in the process step and then switches to Done. After a few seconds, the screenshot should be uploaded to S3. Your first asynchronous application is complete.
Remember the output you got when you invoked node index.js "http://aws. amazon.com" to send a message to the queue? It looked similar to this: http:// url2png-$yourname.s3-website-us-east-1.amazonaws.com/XYZ.png. Now put that URL in your web browser and you will find a screenshot of the AWS website (or whatever you used as an example).
You’ve created an application that is asynchronously decoupled. If the URL2PNG service becomes popular and millions of users start using it, the queue will become longer and longer because your worker can’t produce that many PNGs from URLs. The cool thing is that you can add as many workers as you like to consume those messages. Instead of only one worker, you can start 10 or 100. The other advantage is that if a worker dies for some reason, the message that was in flight will become available for consumption after two minutes and will be picked up by another worker. That’s fault-tolerant! If you design your system to be asynchronously decoupled, it’s easy to scale and a good foundation to be fault-tolerant. The next chapter will concentrate on this topic.
Cleaning up
![]()
Delete the message queue as follows:

This is an excerpt from Manning's book AWS Lambda in Action: Event-driven serverless applications.
You can send input to the function as a JSON event to log a message:
5. Remove everything else and leave only {{Value1}} in the box. In this way, the message that will be sent is only what the Lambda function passes in the value1 parameter.
Slack is a powerful communication tool for teams. It allows you to create channels where team members can share information on a specific topic. Slack has also opened its platform via APIs that allow you to send and receive messages from an external platform. It’s possible to use both the sending and receiving features together to build an automated bot that can manage the integration of a Slack channel with external tools using a simple chat interface.
In this example, we’ll use the Slack API to send a message to a Slack team from a Lambda function. In the next chapter, you’ll see also how to receive events from Slack. In both cases, you need to configure a webhook in your Slack account.