最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - serverless: function doesn't exist in this service - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jun 24, 2018 at 21:41 gardnigardni 1,4243 gold badges26 silver badges53 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

Took so much time on this! Just figured out that when I typed the command below, I mentioned the function name in the handle.js file which is wrong! I should call the handler name itself that exists in the serverless.yml file instead

For example This was wrong:

sls invoke local --function testFunction

This is right:

sls invoke local --function test

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
发布评论

评论列表(0)

  1. 暂无评论