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

Do not use step function custom `name` as cloudformation resource key name

Open dpwrussell opened this issue 7 years ago • 4 comments

This is a (Feature Proposal)

Description

When using a custom name in the definition of a step function in serverless, this is obviously intended to populate the StateMachineName property. It does that sucessfully

It has the unfortunate side-effect of also changing the resource key name used in the cloudformation template. This is not desirable as it makes it impossible to reference the newly created step function in a resources block because the name will be transformed. I can not think of any advantage in changing the resource key name to this value.

For example, take this partial example:

stepFunctions:
  stateMachines:
    MyStepFunction:
      name: ${self:provider.environment.STACK_PREFIX}-${self:provider.environment.STAGE}-MyStepFunction
      definition:
        ...

Where STACK_PREFIX="test-1" and STAGE="dev". The resulting name is test-1-dev-MyStepFunction which is exactly what I want StateMachineName to be set to. However this causes the cloudformation template to look like this:

"TestDash1DashdevDashMyStepFunction": {
  "Type": "AWS::StepFunctions::StateMachine",
  "Properties":
    ...

Note the capitalisation of the first letter and the replacement of the dashes with "Dash". If I then wish to reference this step function in my resources block I can usually use the known pattern that is used to create the cloudformation key names, so I can reference them, but in this case I can't, because it is a transformation of the name property I supplied.

resources:
  Resources:

    MyStepFunctionARN:
      Type: AWS::SSM::Parameter
      Properties:
        Name: /${self:provider.environment.STACK_PREFIX}/${self:provider.environment.STAGE}/MyStepFunctionARN
        Description: ARN of my step function
        Type: String
        Value:
          Ref: <??????????>

A Ref that would work is TestDash1DashdevDashMyStepFunction, but as far as I know there is no way to (within the serverless template) access the mechanism to transform test-1-dev-MyStepFunction into this.

As there is no advantage to renaming the key name (as it's entirely internal to the template and it already has a well defined way to define its own key name), I suggest simply leaving it as it would have been if name had never been specified: MyStepFunctionStepFunctionsStateMachine.

Additional Data

  • Serverless Framework Core Version you're using: 1.29.2
  • The Plugin Version you're using: [email protected]

dpwrussell avatar Aug 15 '18 18:08 dpwrussell

I've since been pointed at this gotcha, but that can not resolve the use case described because the parameters can contain dashes or underscores themselves. I guess it is possible to always insert a custom and static string at the start to resolve the upper case letter transformation though.

In general though, I don't think that renaming the key name and output using the property designed for StateMachineName makes sense.

dpwrussell avatar Aug 15 '18 19:08 dpwrussell

How did you solve the issue?

siovaneDAZN avatar Oct 23 '20 13:10 siovaneDAZN

Any update

raydazn avatar Oct 23 '20 14:10 raydazn

My solution was to add a custom logical id for the stateMachine.

kevthunder avatar Apr 20 '21 16:04 kevthunder