Build a serverless request poller on AWS

Serverless Request Poller Architecture

Let’s get started

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
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
}

--

--

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