Asur
Asur

Reputation: 2057

Serverless Framework, setting up rest api lambda

I wanted to make a lambda available at dev-api.example.com/auth/*. The lambda will act like an auth service. So it will have urls like

Like wise more lambdas will be hooked to single ApiGateway.

With that design decision, I wrote following serverless.yml file.

// serverless.yml
...
custom:
  customDomain:
    domainName: dev-api.example.com
    stage: prod
    basePath: ''
...

functions:
  auth:
    handler: src/index.handler
    events:
      - http:
          method: ANY
          path: /{auth+}

It does not seem to work. Whenever I visit dev-api.example.com/auth/register it returns Not Found error.

Upvotes: 0

Views: 256

Answers (2)

Asur
Asur

Reputation: 2057

Thanks @hoangdv , your suggestion almost fixed the problem.

The issue was with path. It should have been path: auth/{proxy+} instead of path: /{auth+}

functions:
  auth:
    handler: src/index.handler
    events:
      - http:
          method: ANY
          path: auth/{proxy+}

Upvotes: 0

hoangdv
hoangdv

Reputation: 16127

AWS API Gateway only accepts {proxy+} syntax (Link), then I think serverless fw just support {proxy+} and {any+}.

If you want to just create a function to handle 2 api endpoint, in this case, the endpoints are

POST /auth/register (I think so)

POST /auth/login

Then you have setting in serverless.yml like

...
functions:
  auth:
    handler: src/index.handler
    events:
      - http:
          method: ANY
          path: auth/{any+} # this matches any path, the token 'any' doesn't mean anything special
...

Upvotes: 1

Related Questions