Referring to function resource from custom resources


#1

I see that Serverless appends LambdaFunction to the names of function resources when it creates the Cloudformation template. When I refer to these functions in the custom resources section of my serverless.yml I have been doing the same:

functions:
  sendEmail: # I want to refer to this resource later
    ...
resources:
  Resources:
    EmailerSchedule:
      Type: AWS::Events::Rule
      Properties:
        Targets:
          - Arn:
              Fn:Get:Att: 
                - sendEmailLambdaFunction # I have to append `LambdaFunction` to the name specified under `functions`
                - Arn
    EmailerPermission:
      Type: AWS::Lambda::Permission
      Properties:
        FunctionName:
          Ref: sendEmailLambdaFunction # I have to append `LambdaFunction` to the name specified under `functions`

Is there a better way to do this, that doesn’t require implicit knowledge about how Serverless compiles the yaml file?

Update: I see in the documentation that this is a standard pattern, so presumably it will always be safe to hard-code the function name with LambdaFunction appended. I would still be interested to know whether there is another way to get the same string, such as through a variable.


#2

You’re doing it the right way.

The only improvement to this system (i.e. having a standardised naming convention) would be to add helper functions (probably as part of the variables system?) to convert serverless.yml references to CFN Logical IDs, but I doubt it’s worth the extra complexity at this stage.


#3

Note to future readers that if your function name key contains ‘-’, you would have to replace them by Dash.

For instance: hello-world -> HelloDashworldLambdaFunction`. (note the leading capitalized letter)