Ivan Campos
Jun 29, 2018 · 3 min read

Putting an end to Polling

AWS Lambda is event-driven compute. What this means is that your Lambda functions only run when triggered and you pay only for the compute time you use. Up until today, the AWS event sources that could trigger Lambda were the following:

Absent from this list was AWS Simple Queue Service (SQS).

At the AWS Summit San Francisco, SQS as a built-in Lambda event source was announced…

..and then one week ago…

As of today, this functionality is live!

Why does this matter?

Prior to the availability of SQS as a built-in Lambda event source, an inefficient work-around solution was to poll until an SQS message was received and ready for processing.

The Polling Hack

CloudWatch Events, an existing event source, can periodically trigger a Lambda function (via a cron schedule) and wake up to check if a new message resides in SQS and act upon it.

How To Stop Polling

As of this writing, you can now perform the following steps to have SQS serve as an event source to trigger Lambda:

  1. Create an IAM Role — you can reuse a managed policy like AWSLambdaSQSQueueExecuionRole.

2. Create a Lambda function (this example uses Node.js) using the aforementioned IAM role. Add an SQS trigger to the function and add a console log message (“SQS Trigger Fired!”) to the function code.

3. Create a new Queue.

4. Configure Trigger for Lambda Function.

5. Configure Incoming Messages to Trigger a Lambda Function.

6. Save and see your configuration under the Lambda Triggers tab.

7. Test your configuration by sending a message to your Queue.

8. Open CloudWatch and navigate to your Lambda function’s log stream.

9. Open the Log Stream and confirm that the console log message (“SQS Trigger Fired!”) has made it into the log.

10. Rinse and repeat with your processes.

Now you know…and knowing is half the battle. The other half is applying this event-driven pattern to your architecture.

Slalom Technology

Thought leadership from technologists at Slalom.

Ivan Campos

Written by

@IvanCampos - Creating within virtual coal mines

Slalom Technology

Thought leadership from technologists at Slalom.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade