Triggering a SWF Workflow based on SQS messages
Asked Answered
D

2

5

Preamble: I'm trying to put together a proposal for what I assume to be a very common use-case, and I'd like to use Amazon's SWF and SQS to accomplish my goals. There may be other services that will better match what I'm trying to do, so if you have suggestions please feel free to throw them out.

Problem: The need at its most basic is for a client (mobile device, web server, etc.) to post a message that will be processed asynchronously without a response to the client - very basic.

The intended implementation is to for the client to post a message to a pre-determined SQS queue. At that point, the client is done. We would also have a defined SWF workflow responsible for picking up the message off the queue and (after some manipulation) placing it in a Dynamo DB - again, all fairly straightforward.

What I can't seem to figure out though, is how to trigger the workflow to start. From what I've been reading a workflow isn't meant to be an indefinite process. It has a start, a middle, and an end. According to the SWF documentation, a workflow can run for no longer than a year (Setting Timeout Values in SWF).

So, my question is: If I assume that a workflow represents one message-processing flow, how can I start the workflow whenever a message is posted to the SQS?

Caveat: I've looked into using SNS instead of SQS as well. This would allow me to run a server that could subscribe to SNS, and then start the workflow whenever a notification is posted. That is certainly one solution, but I'd like to avoid setting up a server for a single web service which I would then have to manage / scale according to the number of messages being processed. The reason I'm looking into using SQS/SWF in the first place is to have an auto-scaling system that I don't have to worry about.

Thank you in advance.

Dispensable answered 12/12, 2013 at 16:26 Comment(1)
For what it's worth, you can rate limit Amazon SNS to make the scaling less of an issue. See settings here: docs.aws.amazon.com/sns/latest/dg/…Meyer
A
6

I would create a worker process that listens to the SQS queue. Upon receiving a message it calls into SWF API to start a workflow execution. The workflow execution id should be generated based on the message content to ensure that duplicated messages do not result in duplicated workflows.

Aret answered 12/12, 2013 at 18:19 Comment(1)
That's pretty much what we ended up doing with a few small variations. Thanks for responding.Dispensable
A
3

You can use AWS Lambda for this purpose . A lambda function will be invoked by SQS event and therefore you don't have to write a queue poller explicitly . The lambda function could then make a post request to SWF to initiate the workflow

Alliance answered 8/5, 2016 at 21:50 Comment(3)
Yeah, things were probably a little different back thenInhabit
Does the lambda have to run for the whole time that the workflow is running? Or does it just have to start the workflow (then the lambda can exit)?Erdei
It just have to start the workflow. But the workflow (decider) worker still has to run all the time. Unfortunately SWF team still doesn't support deciders running as lambda functions.Aret

© 2022 - 2024 — McMap. All rights reserved.