How can I apply an Authorizer to select routes in my API Gateway Proxy integration

Viewed 695

I'm deploying an application to AWS Lambda using the serverless framwework. My application is served by a single Lambda which is why I pass through the request path using {proxy+}:

functions:
  app:
    runtime: go1.x
    handler: bin/lambda
    events:
      - http:
         path: /{proxy+}
         method: any

Now, I'd like to use a custom authorizer Lambda to protect a certain portion of my application (think everything /admin/). I thought I could just add another event definition that uses the authorizer like this:

functions:
  app:
    runtime: go1.x
    handler: bin/lambda
    events:
      - http:
          path: /admin/{proxy+}
          method: any
          authorizer:
            name: authorizer
            resultTtlInSeconds: 0
            identitySource: method.request.header.Authorization
      - http:
         path: /{proxy+}
         method: any

yet this has no effect at all. If I apply my authorizer to the root level proxy, it will be applied as expected, so the authorizer itself works as expected.

How can I define my routes in such a scenario? Do I need to use different Lambdas or is there something I am missing?

2 Answers

There are two options that I can think of:

  1. If you really want the endpoints the way you wrote, you can use a general endpoint (root endpoint) and inside that handler check if the request is for /admin and then redirect or do some other special case for that.

  2. Give up on having a root endpoint and use two "normal" endpoints:

    functions:
      app:
        runtime: go1.x
        handler: bin/lambda
        events:
          - http:
              path: /admin/{proxy+}
              method: any
              authorizer:
                name: authorizer
                resultTtlInSeconds: 0
                identitySource: method.request.header.Authorization
          - http:
             path: /user/{proxy+}
             method: any

Related