Event-Driven Architecture with AWS Lambda & Amazon SQS Java

1 min read

Event-Driven Architecture with AWS Lambda & Amazon SQS Java

Using AWS Lambda to process messages from Amazon SQS is a powerful serverless pattern that allows you to scale seamlessly and only pay for what you use. Here’s a detailed article showing how to build an AWS Lambda function that consumes messages from an SQS queue, all in Java.

Overview

In this guide, we’ll walk through how to:

  • Create an SQS queue
  • Write a Java-based AWS Lambda function
  • Configure the SQS trigger
  • Deploy using AWS SAM (or you can use the AWS Console)

Step 1: Create an Amazon SQS Queue

You can create the queue from the AWS Console or use the AWS CLI:

aws sqs create-queue --queue-name MyQueue

Take note of the Queue ARN—you’ll need it to set up the Lambda trigger.


Step 2: Write the Lambda Function in Java

We’ll use Maven to build our Lambda handler. Here’s the project structure:

sqs-lambda-java/
├── src/
   └── main/
       └── java/
           └── com/example/
               └── SqsEventHandler.java
├── pom.xml

SqsEventHandler.java

package com.example;

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

public class SqsEventHandler implements RequestHandler<SQSEvent, Void> {

    @Override
    public Void handleRequest(SQSEvent event, Context context) {
        for (SQSEvent.SQSMessage msg : event.getRecords()) {
            System.out.println("Received message: " + msg.getBody());
            // Process message logic here
        }
        return null;
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>sqs-lambda-java</artifactId>
    <version>1.0</version>

    <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.11.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals><goal>shade</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Package your code:

mvn clean package

Step 3: Deploy and Attach to SQS

You can upload the .jar to AWS Lambda using the Console or CLI. Then:

  • In the Lambda configuration, add an SQS trigger
  • Choose the SQS queue you created
  • Lambda will now poll the queue and invoke your function when messages arrive

Testing

Send a test message:

aws sqs send-message \
  --queue-url https://sqs.<region>.amazonaws.com/<account-id>/MyQueue \
  --message-body "Hello from SQS!"

Your Lambda will log the message body in CloudWatch Logs.


Recap

  • No need to poll SQS manually—AWS takes care of that.
  • Lambda scales with demand.
  • Clean and simple Java logic using the AWS SDK.

🤞 Never miss a story from us, get weekly updates to your inbox!

Leave a Reply

Your email address will not be published. Required fields are marked *