Amazon SQS + Lambda implements a custom function to trigger when a new message arrives at the queue

Existing requirements: when the producer sends a new message to the message queue, the message queue should process it automatically according to the message, such as pushing the message to the consumer.

Amazon SQS message queue itself does not support further processing when new messages arrive at the queue, but we can realize this requirement in combination with Amazon Lambda function.

Let's take a simple example to introduce how Amazon SQS message queue and Amazon Lambda function cooperate to realize the above requirements.

1. Create a queue

Lambda function supports both standard queues and FIFO queues, so any type of queue can be created here. Simply set it up and create a queue.

2. Create a Lambda function

Simply create a Lambda function, enter the function name (the function name in the screenshot already exists, ignore it), select the runtime (function language), and then create it directly.

Note:
By default, when creating a Lambda function, a new role with basic Lambda permissions will be created. This role does not have execution permissions related to SQS message queue. You can configure the permissions of this role later.

3. Configure permissions for the role executing Lambda functions

  • First enter the details page of the Lambda function just created, then click "configuration" - > "permissions", and then click the name of the executing role to jump to the role management page of the role.

  • Click "add inline policy"

  • Select SQS for the service, GetQueueAttributes, ReceiveMessage and DeleteMessage for the operation, and then click Add ARN.

  • Copy and paste the ARN of the message queue created in the first step, and click "add".

  • Click "view policy" in the lower right corner to the following page, enter the name and click "create policy".

4. Add trigger to Lambda function

Set the message queue created in step 1 as the trigger of Lambda function.

First, go back to the details page of the Lambda function we created, click "add trigger" to the following page, select SQS, select the queue we created, and click "add".

5. Processing logic of user-defined Lambda function

Now that the message queue we created has been associated with the Lambda function, how can we customize the processing logic when new messages arrive at the queue?

Lambda function supports uploading zip and jar file, so we can first build a project, write the processing logic, and then package and upload it to the lambda queue, so that when new messages arrive at the queue, they can be processed according to the logic we have written.

Including POM The XML is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>doc-examples</groupId>
  <artifactId>lambda-java-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>lambda-java-example</name>
  
  <dependencies>
  	<dependency>
  		<groupId>com.amazonaws</groupId>
  		<artifactId>aws-lambda-java-core</artifactId>
  		<version>1.2.1</version>
  	</dependency>
  	<dependency>
  		<groupId>com.amazonaws</groupId>
  		<artifactId>aws-lambda-java-events</artifactId>
  		<version>3.1.0</version>
  	</dependency>
  	<dependency>
  		<groupId>com.amazonaws</groupId>
  		<artifactId>aws-lambda-java-log4j2</artifactId>
  		<version>1.2.0</version>
  	</dependency>
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-shade-plugin</artifactId>
  			<version>3.2.2</version>
  		</plugin>
  	</plugins>
  </build>
</project>

Write Java classes for processing logic (the class name in the official document is Hello, and the class name I created is Handler. This class name doesn't matter, as long as the subsequent configurations in Lambda functions are consistent) as follows:

package example;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;

public class Handler implements RequestHandler<SQSEvent, Void>{
	
    
    public Void handleRequest(SQSEvent event, Context context)
    {
        for(SQSMessage msg : event.getRecords()){
            System.out.println(new String(msg.getBody()));
        }
        return null;
    }
}
  • Put the packaged Upload the jar file to the Lambda function. Go to the Lambda function details page and click upload at the code source.

  • Finally, modify the "handler" in the "runtime settings" again.

Note:
As long as the configuration of the handler is consistent with our project structure.
Package name Class name:: method name

At this point, a simple example is completed, and you can test it below.

6. Test

Run the test case and send 15 messages to the queue we created.

Then take a look at the log of the Lambda function. It can be seen that after the message arrives at the queue, the Lambda function is indeed triggered and our customized processing logic is executed.

Keywords: message queue

Added by X.Cyclop on Thu, 23 Dec 2021 21:54:36 +0200