DEV Community

Lucas Ferreira
Lucas Ferreira

Posted on

[EN] SAM, Typescript and Sync Step functions

Serverless functions (or Lambdas in AWS-speak) are a great choice when your application load tends to be highly irregular, and you want to avoid provisioning virtual servers and setting up the full environment to perform some resource-expensive operations for a few days or weeks a year.

Recently, I've been working on this project where we are using SAM with Typescript. Long story short, AWS SAM does not support TypeScript out of the box. So I had to create a makefile in order to build each lambda function. More details on this implementation here.

But then we found out that our enrollment process (it's about a subscription service) was not offering value to the user, we were just storing an user in a database, returning a positive response, 200 and another lambda (CapturePayment) was acting as a listener for SNS events. That's the moment where we wanted to create a AWS Step functions structure. This way, we would be triggering other lambdas as quickly activating services if the subscription is in trial mode.

So, to do that, we need to understand that Sync step functions is not possible to be created via SAM. You'll need to set your state machine (Step functions output) as "Express", you'll need to prepare your state machine file as a sequence schema (see example below).

Image description

Just to make it clear, creating Step functions requires a .asl.json file (just like example below) and a reference at your AWS SAM template.

Image description

So, if sync step functions cannot be created via SAM, how did we achieved that? And why it doesn't work well?
Hm, if you notice at this amazing channel about AWS, basically to create a sync step function, we would need to create a brand new API to do that, making sure that your functions would be out of your main API Gateway. Other thing, you would not be able to handle your headers or your response properly. Why? 'Cause at your response scope, you'd get a non-user-friendly response, containing AWS billing information, an ARN Execution for your state machine and your response would be stringified at "output" node.

To avoid that, we decided that we would need to create a lambda for this enrollment process orchestration. We would handle headers just like we wanted and we would be able to return to user just what was needed (Postel's Law).

Image description

From now on, we are able to run our state machine from AWS Console or just using any endpoint that we match to this enrollment Lambda.
And don't forget about creating a policy statement to let this lambda run synchronously state machines.

Image description

If you need advice on Step Functions or why creating then, please just reach me. Thanks!

Top comments (0)