serverless-step-functions icon indicating copy to clipboard operation
serverless-step-functions copied to clipboard

Feature Proposal - add support for Synchronous Express Workflows

Open clayzermk1 opened this issue 3 years ago • 3 comments

This is a Feature Proposal

Description

  • Please support Synchronous Express Workflows as described by https://docs.aws.amazon.com/step-functions/latest/dg/concepts-express-synchronous.html
  • It appears that serverless-step-functions supports Asynchronous Express Workflows already, but not their synchronous counterpart.
  • This would allow this plugin to be used to create Step Function microservices that can synchronously respond to HTTP requests made to API Gateway.

Similar issues:

  • #195

Additional Data

  • Serverless Framework Core Version you're using: 2.31.0
  • The Plugin Version you're using: 2.29.0

clayzermk1 avatar Mar 24 '21 23:03 clayzermk1

I was able to get Sync Express Workflows working by setting my http.action to StartSyncExecution and modifying the request and response templates. The yml I'm using is something like this:

id: SimpleHandler
name: nb-SimpleHandler
events:
  - http:
      path: simple
      method: POST
      action: StartSyncExecution
      request:
        template:
          application/json: |
            #set($body= $input.json('$'))
            #set($inputRoot='{ "data" :'+$body+',"apiInfo":{"httpMethod" :"'+ $context.httpMethod+'", "apiKey":"'+ $context.identity.apiKey+'"}}')
            #set($apiData=$util.escapeJavaScript($inputRoot))
            #set($apiData=$apiData.replaceAll("\\'","'"))
            {
              "input" :"$apiData",
              "stateMachineArn": "REPLACE_WITH_STATE_MACHINE_ARN"
            }
      response:
        headers:
          Content-Type: "'application/json'"
        template:
          application/json: |
            #set ($bodyObj = $util.parseJson($input.body))
            #if ($bodyObj.status == "SUCCEEDED")
              $bodyObj.output
            #elseif ($bodyObj.status == "FAILED")
              #set($context.responseOverride.status = 500)
              {
                "cause": "$bodyObj.cause",
                "error": "$bodyObj.error"
              }
            #else
              #set($context.responseOverride.status = 500)
              $input.body
            #end
type: EXPRESS
definition:
  Comment: "A simple step function based http request handler"
  StartAt: StartHandler
  States:
    StartHandler:
      Type: Task
      Resource: arn:aws:lambda:us-west-2:${env:ACCOUNT_ID}:function:nb-simpleHandlerStart
      Next: CompleteRequest
    CompleteRequest:
      Type: Task
      Resource: arn:aws:lambda:us-west-2:${env:ACCOUNT_ID}:function:nb-simpleHandlerComplete
      End: true

neilbts avatar Sep 21 '21 21:09 neilbts

@neilbts nice workaround. Any ideas on how to fill "REPLACE_WITH_STATE_MACHINE_ARN" dynamically?

The original template has "stateMachineArn":"\${StateMachineArn}" but it doesn't work with your solution.

mobits avatar Mar 04 '22 23:03 mobits

Hello! Any updates on this?

kgssense avatar Jul 08 '22 15:07 kgssense