Implementing newly-launched synchronous workflows for step functions

Hi,

I just had similar use case. Like you said, it’s a new feature, serverless-step-functions plugin does not support it yet (https://github.com/serverless-operations/serverless-step-functions/issues/378).

I managed to make it work with CloudFormation so I though I will share it with you.
You need to use step functions of type express and Http API gateway.
Step functions need to be express type, because they offer synchronous execution.

You need to create:

  1. Step function, e.g. by using serverless-step-functions plugin

     stepFunctions:
       validate: true
      stateMachines:
        myStateMachine:
          type: EXPRESS
          tracingConfig:
            enabled: true
          id: MyStateMachine
         name: 'My step function
         definition: ${file(state-machines/state-machine.json)}
    
  2. Execution role for HttpApi integration

     InvokeStepFunctionRole:
        Type: AWS::IAM::Role
        Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
            Service:
              - apigateway.amazonaws.com
            Action:
             - sts:AssumeRole
      Policies:
       - PolicyName: InvokeStepFunctionPolicy
         PolicyDocument:
           Statement:
             - Effect: Allow
               Action:
                 - states:DescribeExecution
                 - states:StartSyncExecution
               Resource:
                 - !Ref MyStateMachine
    
  3. Given you have a gateway already created (HttpApi) resource, you need to create integration

    HttpApiStepFunctionIntegration:
      Type: AWS::ApiGatewayV2::Integration
      Properties:
       ApiId: !Ref HttpApi
       CredentialsArn: !GetAtt InvokeStepFunctionRole.Arn
       Description: 'Start my step machine step function'
       IntegrationType: AWS_PROXY
       IntegrationSubtype: StepFunctions-StartSyncExecution
       PayloadFormatVersion: "1.0"
       RequestParameters: {"Input": "$request.body", "StateMachineArn": "#{MyStateMachine}"}
    
  • note: #{MyStateMachine} is serverless pseudo parameters syntax for !Ref MyStateMachine
  1. Http Api route with integration from point 3.

     HttpApiRoute:
       DependsOn:
         - HttpApiStepFunctionIntegration
         - HttpApi
       Type: AWS::ApiGatewayV2::Route
       Properties:
         ApiId: !Ref HttpApi
         RouteKey: POST /start-stepfunction-sync
         Target: !Join ['/', ['integrations', !Ref HttpApiStepFunctionIntegration]]
    

The only drawback I noticed is the format of the response which cannot be mapped at the moment according to documentation, only headers and status codes can be mapped: [Modifying the response of the StartSyncExecution · Issue #6 · aws-samples/contact-form-processing-with-synchronous-express-workflows · GitHub]

Cheers,
jimmy