javascriptserverless-frameworkserverlessaws-serverless

serverless: function doesn't exist in this service


In serverless i have the following directory structure for my functions:

serverless.yml
functions -
    stories -
        create.js
        get.js

my serverless.yml then looks like this:

functions:
  stories:
    create:
      handler: functions/stories/create.main
      events:
        - http:
          path: stories/create
          method: post
          cors: true
          authorizer: aws_iam
    get:
      handler: functions/stories/get.main
      events:
        - http:
          path: stories/{id}
          method: get
          cors: true
          authorizer: aws_iam

however when i run a test to check the create: serverless invoke local --function create --path mocks/create-event.json i get the following error:

Serverless Error ---------------------------------------

  Function "create" doesn't exist in this Service

I managed to get one function working that looks like this:

functions:
  stories:
    handler: functions/stories/create.main
    events:
      - http:
        path: stories/create
        method: post
        cors: true
        authorizer: aws_iam

Since i added the get function, i decided i needed to add the create and get parts after stories, but no matter how i change the handler the functions never seem to exist.

I've tried changing the path to functions/stories/create/create.main with no difference, is there anything obvious i'm missing to allow multiple handlers within the same location?

I was looking at the following example, which uses one folder of "todos" which contains multiple functions, but i can't see any obvious difference between it and mine, except i've added an extra folder.


Solution

  • Your template is invalid. You can't just put your function under an arbitrary node to tell the framework that it applies to some object of your app. Your stories: node should be a comment.

    Try something like this:

    functions:
        # Stories related functions
        createStory:
          handler: functions/stories/create.main
          events:
            - http:
                path: stories   # You can post directly to stories to be more RESTful
                method: post
                cors: true
                authorizer: aws_iam
        getStory:
          handler: functions/stories/get.main
          events:
            - http:
                path: stories/{id}
                method: get
                cors: true
                authorizer: aws_iam
    
         # More examples to understand the routing
        getAllStories:
          handler: functions/stories/getAll.main # Returns a list of all stories
          events:
            - http:
                path: stories
                method: get
                cors: true
                authorizer: aws_iam
        deleteStory:
          handler: functions/stories/delete.main # Deletes a story
          events:
            - http:
                path: stories/{id}
                method: delete
                cors: true
                authorizer: aws_iam