This content originally appeared on DEV Community and was authored by DEV Community
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).
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.
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).
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.
If you need advice on Step Functions or why creating then, please just reach me. Thanks!
This content originally appeared on DEV Community and was authored by DEV Community
DEV Community | Sciencx (2022-03-04T17:57:53+00:00) [EN] SAM, Typescript and Sync Step functions. Retrieved from https://www.scien.cx/2022/03/04/en-sam-typescript-and-sync-step-functions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.