[EN] SAM, Typescript and Sync Step functions
Tags: typescript, stepfunctions, sam
Published: 2022-03-04 17:57:53

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](https://evilmartians.com/chronicles/serverless-typescript-a-complete-setup-for-aws-sam-lambda).

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qzpwunsdp1vk43otqe1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syebmn6j1xpqzkeoox8w.png)

**_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmzomd1nubde4twybmny.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tezc170c8djmwoihom99.png)

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