Build a serverless request poller on AWS

Daryl Ng
2 min readAug 18, 2021

Here is a simplified architecture of the serverless request poller.

Serverless Request Poller Architecture

The idea is to send the message to AWS SQS, which will then be picked up by AWS Lambda. In the message, you can define the maximum number of retries, retry count, message delay and the necessary parameters for the request polling.

When you want to continue polling, you can add the message back to SQS and increment the retry count. You can optionally set the message delay, just like a backoff. When done, simply return without error.

Let’s get started

First, you will need to create a Lambda and SQS. You can do in from console, or use the following Cloudformation template.

Resources:
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
AutoPublishAlias: !Ref Environment
CodeUri: main.zip
Description: 'A serverless request poller'
Environment:
Variables:
QUEUE_URL: !Ref Queue
Events:
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt Queue.Arn
BatchSize: 100
Enabled: true
MaximumBatchingWindowInSeconds: 1 # Required if BatchSize > 10
FunctionName: !Ref Name
Handler: main
MemorySize: 256
Policies:
- Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sqs:SendMessage # Required to add message back to SQS
Resource:
- !GetAtt Queue.Arn
Runtime: go1.x # Set this runtime accordingly
Timeout: 900
Tracing: Active
Queue:
Type: AWS::SQS::Queue
Properties:
VisibilityTimeout: 5400 # 6 times of Lambda timeout

Next, you will need to write the logic for the Lambda. Here is an example for the handler for a Go runtime.

func Handler(ctx context.Context, event events.SQSEvent) (err error) {
for _, message := range event.Records {
// Parse the record and process accordingly
// For example, if the response from the request is still
// pending, you can add the message back to SQS
}

return
}

And that’s all! It’s that simple.

Do share in the comments on how you are handling request polling.

Also, do follow me if you have not already done so!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response

Thanks for the informative article! I think the benefits of microservices that you described are especially important for developers. However, I have a question about the scalability of microservices. While it is true that microservices can be…