serverless-operations/serverless-step-functions

Support to separate step functions into different yml files

Pjack opened this issue ยท 13 comments

Pjack commented

This is a Feature Proposal

Description

For feature proposals:

In my imagination, we can declare it as following:

stepFunctions:
  stateMachines:
  - ${file(resources/sf-statemachine1.yml)}
  - ${file(resources/sf-statemachine2.yml)}

Thanks for help

It would be really nice if it was a clean copy/paste out of the Step Function interface on the AWS Dashboard.

@Pjack The Serverless framework has built-in support for this, and I just tried and it works out-of-the-box.

You can even mix & match:

service: aws-nodejs

plugins:
  - serverless-step-functions

provider:
  name: aws
  runtime: nodejs8.10

stepFunctions:
  stateMachines:
    hello: ${file(sfn-1.yml)}
    world:
      name: world
      definition:      
        StartAt: Done
        States:
          Done:
            Type: Pass
            End: true

and sfn-1.yml looks like this:

name: hello
definition:      
  StartAt: Done
  States:
    Done:
      Type: Pass
      End: true

@dearmond if you want something that can be easily copy and pasted out into the console, then you should use JSON instead. e.g.

stepFunctions:
  stateMachines:
    hello: ${file(sfn-1.yml)}
    world:
      name: world
      definition: ${file(sfn-2.json)}

and sfn-2.json looks like this:

{ 
  "StartAt": "Done",
  "States": {
    "Done": {
      "Type": "Pass",
      "End": true
    }
  }
}

the trick is to only put the state definitions in the JSON file

....also worth noting that the serverless-pseudo-parameters and/or serverless parameters still work in the JSON version as well, just stick them in double quotes...for example here's a single state with params:

Might be worth throwing in the readme.

"CrawlerCheck": {
   "Type": "Task",
   "Parameters": {
     "crawlerName": "crawler"
   },
   "Resource": "arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${opt:stage}-checkCrawlerHasRun",
   "ResultPath": "$.crawlerStatus",
   "Next": "CrawlerCompletion"
 }
Pjack commented

@Pjack The Serverless framework has built-in support for this, and I just tried and it works out-of-the-box.

You can even mix & match:

service: aws-nodejs

plugins:
  - serverless-step-functions

provider:
  name: aws
  runtime: nodejs8.10

stepFunctions:
  stateMachines:
    hello: ${file(sfn-1.yml)}
    world:
      name: world
      definition:      
        StartAt: Done
        States:
          Done:
            Type: Pass
            End: true

and sfn-1.yml looks like this:

name: hello
definition:      
  StartAt: Done
  States:
    Done:
      Type: Pass
      End: true

It works!
Actually, I tried this before. Obviously, I must made mistake somewhere and cause compile error.
Thanks a lot for this easy solution~

@bmatson fancy putting in a PR to update the README? :-P add a section on how to separate definitions out to a separate YML/JSON file

@dearmond if you want something that can be easily copy and pasted out into the console, then you should use JSON instead. e.g.

stepFunctions:
  stateMachines:
    hello: ${file(sfn-1.yml)}
    world:
      name: world
      definition: ${file(sfn-2.json)}

and sfn-2.json looks like this:

{ 
  "StartAt": "Done",
  "States": {
    "Done": {
      "Type": "Pass",
      "End": true
    }
  }
}

the trick is to only put the state definitions in the JSON file

Thanks a lot for the example. Really illustrative!

Do you have any progress on this?

@usamedin I doubt there will be "progress" on this, what the OP was asking for is already completely possible, just using a different (probably better) pattern that is documented in the comments. If you'd like to make a PR to update the readme with the discussion in the comments I'm sure the maintainers would appreciate it.

Thanks, @bmatson, using JSON as an input file for step functions will be very helpful. I will not promise but I will try during the weekend to fix the Readme.

Hi,
Is it possible to split states into multiple files too?

I've few reusable states that can easily be reused across different state-machiens if we can perform above point.

Hi @nitsshukla1,

Were you ever able to achieve splitting a function up into multiple files? I'm in a very similar situation where I'd love to be able to reuse certain steps.

@cwardcode sorry for the late response, and yes i was able to achieve it. In your serverless.yml have this entry:

stepFunctions:
  stateMachines:
    UploadTest: ${file(step-function/stepfunction-upload-test.yml)}

And in file stepfunction-upload-test.yml, start like this:

name: test
definition:
  StartAt: ProcessUploadFile
  States:
     ...