What is Amazon Simple Queue Service?

Amazon Simple Queue Service (Amazon SQS) offers a secure, durable, and available hosted queue that lets you integrate and decouple distributed software systems and components (send, receive, and delete a message). Amazon SQS offers common constructs such as dead-letter queues and cost allocation tags. It provides a generic web services API that you can access using any programming language that the AWS SDK supports.

Amazon SQS supports both standard and FIFO queues. For more information, see Queue types.

In AWS, SQS can be an event source to Lambda, all that heavy lifting of managing message polling and deleting has been offloaded to the Lambda service, which is now your SQS consumer. It should be called Lambda SQS Consumer Service.

 Lambda is doing the scaling of consumers of YOUR SQS queue for messages, receiving messages, and passing those messages to your Lambda function, and then deleting those messages when your Lambda function successfully handles the message(s) received. This is all explained in more detail here.

Below is a nice visual of what’s going on with this integration.


If you already know enough about SQS as an event source and how the AWS Lambda integration works, skip the preamble and move onto Get Started using the included end-to-end CloudFormation template and then come back for a deeper dive.

Get Started — Backend

I created a CloudFormation template that will provision: 

  1. A standard SQS queue.
  2. A dead letter queue.
  3. AWS Lambda function for handling messages.
  4. The event source mapping.
  5. Lambda execution permissions to SQS and SNS for sending SMS text message. 

Click on the template to launch the CloudFormation console to begin building your stack. The template defaults to the US West Oregon region (us-west-2).

Once the stack completes, you can now test the setup as follows:

1. In the Amazon SQS console, send messages to the queue.

2. AWS Lambda polls the queue and when it detects new messages, it invokes your Lambda function by passing in the event data from the queue.

3. Your function executes and creates logs in Amazon CloudWatch. You can verify the logs reported in the Amazon CloudWatch Logs console.

To really see the value of SQS, checkout my sample iOS Swift application (you can download it from GitHub here) that demonstrates single and batch messages.

AWS Lambda service (not your Lambda function, but a Lambda SQS long-poll service running on your behalf) polls your SQS queue continually for incoming messages. When a message arrives, it receives the message(s) and then invokes your Lambda function by passing the message(s) as a parameter.

From: https://dzone.com/articles/amazon-sqs-as-an-event-source-to-aws-lambda-a-deep


Configuring a queue to trigger an AWS Lambda function (console)

You can use an AWS Lambda function to process messages in an Amazon SQS queue. Lambda polls the queue and invokes your Lambda function synchronously with an event that contains queue messages. You can specify another queue to act as a dead-letter queue for messages that your Lambda function can't process. 

A Lambda function can process items from multiple queues (using one Lambda event source for each queue). You can use the same queue with multiple Lambda functions. 

If you associate an encrypted queue with a Lambda function but Lambda doesn't poll for messages, add the kms:Decrypt permission to your Lambda execution role. 

Note the following restrictions:

Your queue and the Lambda function must be in the same AWS Region.

An encrypted queue that uses the default key (AWS managed CMK for Amazon SQS) cannot invoke a Lambda function in a different AWS account. 

For information about implementing the Lambda function, see Using AWS Lambda with Amazon SQS in the AWS Lambda Developer Guide. 

Prerequisites

To configure Lambda function triggers, you must meet the following requirements:

If you use an IAM user, your Amazon SQS role must include the following permissions:

lambda:CreateEventSourceMapping

lambda:ListEventSourceMappings

lambda:ListFunctions

The Lambda execution role must include the following permissions:

sqs:DeleteMessage

sqs:GetQueueAttributes

sqs:ReceiveMessage

If you associate an encrypted queue with a Lambda function, add the kms:Decrypt permission to the Lambda execution role. 

For more information, see Overview of managing access in Amazon SQS. 

To configure a queue to trigger a Lambda function (console) 

1. Open the Amazon SQS console at https://console.aws.amazon.com/sqs/

In the navigation pane, choose Queues. 

On the Queues page, choose the queue to configure. 

On the queue's page, choose the Lambda triggers tab. 

On the Lambda triggers page, choose a Lambda trigger. 

If the list doesn't include the Lambda trigger that you need, choose Configure Lambda function trigger. Enter the Amazon Resource Name (ARN) of the Lambda function or choose an existing resource. Then choose Save. 

Choose Save. The console saves the configuration and displays the Details page for the queue. 

On the Details page, the Lambda triggers tab displays the Lambda function and its status. It takes approximately 1 minute for the Lambda function to become associated with your queue. 

To verify the results of the configuration, send a message to your queue and then view the triggered Lambda function in the Lambda console. 


https://devblog.croquis.com/ko/2017-05-13-1-aws-serverless-1/