AWS project using S3, Lambda function, and SQS in Java

Lambda function writing message to SQS

Create an S3 bucket in AWS

Login to your AWS console and type S3 in search. Click on S3 Service and go to S3 service page.

S3 Bucket Creation

Create IAM role and attach a policy:

Lambda function uses both services S3 and SQS so we have to provide the access privileges and permissions to it.
We will create a role for that.
Go to console type IAM and click the IAM service. This will open the IAM service page.
Click the Roles from the left menu to display the Roles under Access management.
Click the Create role button on right.
Follow the steps to create role :

Create SQS:

Go to console and type SQS into the search bar and select Simple Queue Service.
Click the Create queue button.
select Standard, and in the Name field type the name of your queue.
Use default options for Configuration, Access policy and other sections.
click on the Create queue button.

Create a Lambda function in Java :

We need to create a jar for our lambda function and upload to lambda service.

  1. Create maven project using quickstart in any IDE (Eclipse,IntelliJ).
  2. Provide dependencies for aws-lambda-java-core, aws-java-sdk-sqs in POM.xml

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

public class S3LambdaSQS
String sqsUrl = "<Accountid>/<Queue-Name>";
AmazonSQS sqs = AmazonSQSClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("<youraccesskey>", "<yoursecretkey>")))
java.util.Date date= new java.util.Date();
public Boolean handleRequest(Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Lambda found file in S3 and Triggering SQS " );
logger.log("Executing the Lambda ");
Map<String, MessageAttributeValue> messageAttributeMap= new HashMap<>();
messageAttributeMap.put("Name",new MessageAttributeValue()
.withStringValue("Swapnil Watkar")
try {
SendMessageRequest sendMsg = new SendMessageRequest()
.withMessageBody("File-Upload_"+new Timestamp(date.getTime()))
SendMessageResult result = sqs.sendMessage(sendMsg);
logger.log("MessageID: " + result.getMessageId());
return true;
}catch (AmazonSQSException e){
logger.log("SQS Fail: " + e.getStackTrace());
return false;
mvn clean package

Add trigger on S3 bucket:

Open the lambda function and click the Add trigger button.
In the Trigger configuration select the S3 service and provide your bucket name.
Select all object events ,we ignore Prefix and Suffix for this demo.
check off Recursive invocation.
click Add.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Swapnil Watkar

Swapnil Watkar

Hi ! I am a Software Engineer, experienced in Mobile and Web applications Also AWS Certified Solutions Architect — Associate