Search code examples
amazon-web-serviceswebsocketaws-lambdaaws-step-functions

Is there a way to invoke an AWS Step Function or Lambda in response to a websocket message?


TLDR: Is there a way to trigger an AWS lambda or step function based on an external system's websocket message?

I'm building a synchronization service which connects to a system which supports websockets. I can use timers in step functions to wake periodically and call lambda functions to perform the sync, but I would prefer to subscribe to the websocket and perform the sync only when a message is received.

There are plenty of ways to expose websockets in AWS, but I haven't found a way to consume them short of something like an EC2 instance with a custom service running on it. I'm trying to stay in the serverless ecosystem.

It seems like consuming a websocket is a fairly common requirement; have I overlooked something?


Solution

  • Lambdas are ephemeral. They can't be sitting there waiting for a websocket message.

    However, I think what you can do is use an Activity task. Once the step function gets to that state it will wait. The activity worker will run on an EC2 instance and subscribe to a websocket. When a message is received it will poll the State Machine for an activity token and call SendTaskSuccess. The state machine will then continue execution and call the lambda that performs the sync.