Using an Amazon Simple Queue Service to invoke a Lambda function on LocalStack

Rochisha Jaiswal
3 min readFeb 23, 2022

--

We can use an AWS Lambda function to process messages in an Amazon Simple Queue Service (Amazon SQS) queue. Lambda event source mappings support standard queues and first-in, first-out (FIFO) queues. With Amazon SQS, we can offload tasks from one component of your application by sending them to a queue and processing them asynchronously.

Lambda polls the queue and invokes our Lambda function synchronously with an event that contains queue messages. Lambda reads messages in batches and invokes our function once for each batch. When our function successfully processes a batch, Lambda deletes its messages from the queue.

LocalStack provides an easy way to develop AWS cloud applications directly from our localhost. It spins up a testing environment on our local machine that provides almost the same parity functionality and APIs as the real AWS cloud environment.

Let’s Start

We must have Docker installed on our system.

Docker-Compose.YML

version: '2.1'services:
localstack:
container_name: "localstack-image"
image: localstack/localstack-full
network_mode: bridge
ports:
- "4566:4566"
- "4571:4571"
- "8082:8082"
environment:
- USE_LIGHT_IMAGE=0
- DEBUG=1
- PORT_WEB_UI=8082
- LAMBDA_EXECUTOR=local
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=${TMPDIR}
- START_WEB=1
volumes:
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"

Run the following command :

docker-compose up

We will see the LocalStack container is running successfully.

Create the execution role

An IAM resource-based policy controls the permissions to invoke the function.

aws iam create-role --role-name lambda-ex --assume-role-policy-document "{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}"  --endpoint-url http://localhost:4566

Create the IAM policy

The IAM policy defines the permissions for the Lambda function.

aws iam create-policy --policy-name my-policy --policy-document file://policy.txt --endpoint-url http://localhost:4566

Attach the IAM policy to an IAM role

An IAM execution role defines the permissions that control what the function is allowed to do when interacting with other AWS services.

aws iam attach-role-policy --policy-arn arn:aws:iam::000000000000:policy/my-policy --role-name lambda-ex --endpoint-url http://localhost:4566

Create the function code

Copy the following code example into a file named Handler.java.

Create the deployment package

The deployment package is a .zip file archive containing your Lambda function code and its dependencies.

Run the following command :

mvn package

Create the Lambda function

The create-function command specifies the function handler as example.handler. The function can use the abbreviated handler format of package.Class because the function implements a handler interface.

aws lambda create-function --function-name my-math-function --zip-file fileb://blank-java-1.0-SNAPSHOT.jar --handler example.Handler::handleRequest --runtime java8 --role arn:aws:iam::000000000000:role/lambda-ex --endpoint-url http://localhost:4566

Create an Amazon SQS queue

Create an Amazon SQS queue that the Lambda function can use as an event source.

aws sqs create-queue --queue-name MyQueue --endpoint-url http://localhost:4566

After creating the queue, record its Amazon Resource Name (ARN). You need this in the next step when you associate the queue with your Lambda function.

aws sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/MyQueue --attribute-names All --endpoint-url http://localhost:4566

Configure the event source

To create a mapping between your Amazon SQS queue and your Lambda function, run the following create-event-source-mapping AWS CLI command.

aws lambda create-event-source-mapping --function-name my-math-function --batch-size 5 --maximum-batching-window-in-seconds 60  --event-source-arn arn:aws:sqs:us-east-1:000000000000:MyQueue --endpoint-url http://localhost:4566

Test the setup

aws sqs send-message --queue-url  http://localhost:4566/000000000000/MyQueue  --message-body "Successfull!!" --endpoint-url http://localhost:4566

Lambda polls the queue for updates. When there is a new message, Lambda invokes your function with this new event data from the queue.

Congratulations!

You now have a successfully configured Amazon SQS to publish events and trigger Lambda. It’s time to put it to use!

Dear reader, I hope this was clear and useful. If you found it interesting don’t forget to like this article and follow me to be notified about similar ones in future. See ya!

--

--

Rochisha Jaiswal
Rochisha Jaiswal

Written by Rochisha Jaiswal

Appian BPM Developer at Novartis

Responses (2)