Skip to content

Feature Request: API Gateway Authorizer support in SAM Local #137

Closed

Description

@azcdev

I'm using the tool to spin up locally an API gateway and run my lambda functions. However, I don't know how to configure an authorizer in template.yml

In my template.yml file, I have the following configuration:

JwtAuthorizerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          JWT_SECRET: '<<secret>>'
      Handler: authorizers.jwt
      Runtime: nodejs6.10
      Role:
        Fn::ImportValue:
          !Join ['-', [!Ref 'ProjectId', !Ref 'AWS::Region', 'LambdaTrustRole']]

If I understand the authorizers documentation correctly, I should be able to add the following configurations in order to configure my authorizer

Authorizer: 
  Type: "AWS::ApiGateway::Authorizer"
  Properties: 
    AuthorizerCredentials: 
      Fn::GetAtt: 
        - "LambdaInvocationRole"
        - "Arn"
    AuthorizerResultTtlInSeconds: "300"
    AuthorizerUri: "arn:aws:lambda:us-east-1:<<myarn>>:function:<<lambda function arn>>"
    Type: "TOKEN"
    IdentitySource: "method.request.header.Authorization" 
    Name: "DefaultAuthorizer"
    RestApiId: 
      Ref: "RestApi"

Everything up until that point works fine. If I run
sam validate the output is Valid!
sam local start-api executes without a problem.

However, I don't know how to configure the custom authorizer in my lambda functions. I've read the AWS::Serverless::Function documentation but I couldn't find anything relevant to my issue. Furthermore, there are two issues about adding support for AuthorizationType to the API but it's unclear whether this support will ever be added, or if it has been added already.

Any help pointing in the right direction is greatly appreciated!

Activity

  1. PaulMaddox commented on Sep 25, 2017

    @PaulMaddox
    Contributor

    Hi @alanchavez88,

    Currently configuring a custom authorizer isn't supported when running locally with SAM Local.

    I'll leave this issue open as a feature request for our backlog.

    Thanks

  2. azcdev commented on Sep 25, 2017

    @azcdev
    Author

    Thanks @PaulMaddox for the response!

    If I understand the model correctly, it shouldn't matter if my custom authorizer does not run locally, right? In AWS, only authorized requests can trigger the lambda function, correct?

  3. sanathkr commented on Sep 25, 2017

    @sanathkr

    Yes, an authorization request should trigger the Lambda in AWS cloud

  4. bradwoods commented on Jan 5, 2018

    @bradwoods

    I'm also looking for examples of how to integrate a Cognito Authorizer with lambdas made via SAM templates.

  5. added
    type/feature
    and removed on Jan 7, 2018
  6. mohithg commented on Jan 10, 2018

    @mohithg

    This is a good to have feature in SAM Local, so we can test the authorization functions before deploying

  7. sujithsnair86 commented on Jan 19, 2018

    @sujithsnair86

    Hi is there a resolution yet for including Custom Authoriser lambda over SAM template itself or is this still a feature request AWS SAM Team is working on?

  8. ThomasSmWatson commented on Jan 25, 2018

    @ThomasSmWatson

    My database url will not work as it contains '=' and other special characters, int he env variables.
    How would I get around this? This is a reliant to my company

  9. gregaeq commented on Mar 22, 2018

    @gregaeq

    Interested in this feature as well. Attempting to use sam-local to auth with Cognito before making subsequent requests.

  10. kanchen commented on Mar 30, 2018

    @kanchen

    Same here. Anyone knows the timeline to support SAM local custom authorizer ?

  11. margic commented on Apr 7, 2018

    @margic

    @alanchavez88 did you ever figure out how to configure an authorizer, I notice that the question was never answered. It went off on a tangent about Sam local.

  12. azcdev commented on Apr 7, 2018

    @azcdev
    Author

    @margic no, I never figured it out. I ended up assuming that it worked and moved on :( If you find a way to do it please post it here.

  13. 153 remaining items

  14. lucashuy commented on Mar 15, 2023

    @lucashuy
    Contributor

    Hi, thanks for raising your concerns. While I unfortunately do not have a date for when this will be complete, implementation of custom Lambda authorizer support for sam local start-api is currently underway.

  15. alb3rtuk commented on Apr 9, 2023

    @alb3rtuk

    Would love to see this when it's built and ready to use. @lucashuy Please keep us updated :)

  16. lucashuy commented on Apr 17, 2023

    @lucashuy
    Contributor

    Hi all, custom Lambda authorizer support for sam local start-api has been released as of SAM CLI v1.80.0. Feel free to create new issues or discussion threads if there are any problems or features that would fit for SAM CLI.

  17. github-actions commented on Apr 17, 2023

    @github-actions
    Contributor

    ⚠️COMMENT VISIBILITY WARNING⚠️

    Comments on closed issues are hard for our team to see.
    If you need more assistance, please either tag a team member or open a new issue that references this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  18. nickls commented on Apr 21, 2023

    @nickls

    For anyone trying to get this working:

    • You'll need to explicitly add FunctionPayloadType: TOKEN even though that is the default value.
    • Alias function ARN do not work for the FunctionArn as they fail a format sanity check.
  19. ChronosMasterOfAllTime commented on May 15, 2023

    @ChronosMasterOfAllTime

    @lucashuy nice work! Can we please get a usage tutorial on this new feature? I have tried getting this to work and am referencing a local authorizer, yet it is still "not found". Usage details would be great 👍

  20. supagu commented on Jul 25, 2023

    @supagu

    Is there a proper example on how to do this?

  21. ChronosMasterOfAllTime commented on Jul 25, 2023

    @ChronosMasterOfAllTime

    Is there a proper example on how to do this?

    There isn't. I requested documentation in issue #5172

  22. le0kar0ub1 commented on Sep 26, 2023

    @le0kar0ub1

    Hi, for those which are struggling to make the thing works, I just found how to after some long hours.
    The problems seems to be that:

    • you MUST NOT provide a FunctionName field in your lambda authorizer.
    • you MUST provide FunctionPayloadType: TOKEN in the API Auth despite the fact that it's the default value.

    Minimal example:

    ApiGateway:
        Type: AWS::Serverless::Api
        Properties:
          StageName: dev
          Auth:
            AddDefaultAuthorizerToCorsPreflight: false
            DefaultAuthorizer: Authorizer
            Authorizers:
              Authorizer:
                FunctionPayloadType: TOKEN
                FunctionArn: !GetAtt Authorizer.Arn
                Identity:
                  Header: Authorization
                  ReauthorizeEvery: 300
    
    Authorizer:
        Type: AWS::Serverless::Function
        Properties:
          Runtime: nodejs18.x
          CodeUri: path/to/Authorizer
          Handler: app.handler
          Timeout: 5
    

    SAM CLI version 1.97.0
    AWS CLI version 2.7.21

    Hope this can save some hours of many !

  23. Willis0826 commented on Sep 26, 2023

    @Willis0826

    a working TypeScript example can be found here as well.

  24. CeccoCQ commented on Sep 27, 2023

    @CeccoCQ

    Hi @Willis0826,
    I'm currently working on a project that uses AWS Serverless HttpApi (Type: AWS::Serverless::HttpApi). I was wondering if you have any experience with integrating Lambda Authorizer in this V2 version of the API Gateway?

  25. Willis0826 commented on Oct 2, 2023

    @Willis0826

    @CeccoCQ Hey, sorry for the late reply. I tried HTTP APIs (V2), and it works great. Please refer to this example

  26. CeccoCQ commented on Oct 2, 2023

    @CeccoCQ

    Hi @Willis0826 , thanks for you example!
    I have a doubt regarding my case, following your sample.

    With this template.yml and these resources:

      ApiGateway:
        Type: AWS::Serverless::HttpApi
        Properties:
          StageName: !Ref AppEnv
          CorsConfiguration: ........
          AccessLogSettings: ...........
          Auth:
            DefaultAuthorizer: CustomLambdaAuthorizer
            Authorizers:
              CustomLambdaAuthorizer:
                AuthorizerPayloadFormatVersion: 2.0
                EnableSimpleResponses: True
                FunctionArn: !GetAtt AuthorizerFunction.Arn
                Identity:
                  Headers:
                    - Authorization
                  ReauthorizeEvery: 5
          Domain:.........
    
      AuthorizerFunction:
        Type: AWS::Serverless::Function
        Properties:
          CodeUri: ./app-identity/authorizer
          Runtime: nodejs16.x
          Handler: service.handler
          FunctionName: "app-authorizer"
          Layers:
            - !Ref CommonsLayer
          Environment: .......
          
      AuthorizerResourcePolicy:
        Type: AWS::Lambda::Permission
        Properties:
          Action: lambda:InvokeFunction
          FunctionName: !GetAtt AuthorizerFunction.Arn
          Principal: apigateway.amazonaws.com
          SourceArn: !Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGateway}/*      
          
      ApiUsersFunction:
        Type: AWS::Serverless::Function
        Properties:
          Role: ...
          Runtime: nodejs16.x
          CodeUri: ./app-identity/users-api
          FunctionName: users-api
          Handler: service.handler
          Layers:
            - !Ref CommonsLayer      
          Environment: ......
          Events:
            ReadItems:
              Type: HttpApi
              Properties:
                ApiId: !Ref ApiGateway
                Path: /api/users
                Method: GET
    

    When running sam local start-api -n envs.json --skip-pull-image --warm-containers EAGER, if I use curl -H "Authorization: abc" http://127.0.0.1:3000/api/users or curl http://127.0.0.1:3000/api/users, it seems that the AuthorizerFunction isn't invoked (I have a console.log("Hello authorizer") inside), but the request is processed directly by ApiUsersFunction.

    What is wrong?

  27. Willis0826 commented on Oct 2, 2023

    @Willis0826

    @CeccoCQ the AuthorizerPayloadFormatVersion field should be "2.0" (a string) instead of 2.0. If you run sam build with AuthorizerPayloadFormatVersion: 2.0, you should see an error message Error: 'AuthorizerPayloadFormatVersion' must be of type string for Lambda Authorizer 'LambdaTokenAuthorizer'..

    Am thinking maybe you can create an issue in my example repository. we can figure it out there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions