Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP API authorizer does not set "Invoke permissions" #2933

Closed
mrichman opened this issue Feb 23, 2023 · 5 comments
Closed

HTTP API authorizer does not set "Invoke permissions" #2933

mrichman opened this issue Feb 23, 2023 · 5 comments
Assignees

Comments

@mrichman
Copy link

mrichman commented Feb 23, 2023

This is largely a duplicate of #2005 but I am opening a new issue for visibility.

Put simply, REST API behaves as expected, but HTTP API does not. This setting, "Invoke permissions" does not seem to be settable via SAM, regardless of whether FunctionInvokeRole is set.

image

Here is a snippet of my template:

Resources:

  HttpApi:
    Type: AWS::Serverless::HttpApi
    Properties:
      Description: My HTTP API
      StageName: !Ref StageName
      Auth:
        DefaultAuthorizer: LambdaAuthorizer
        Authorizers:
          LambdaAuthorizer:
            FunctionArn: !GetAtt AuthFunction.Arn
            # FunctionInvokeRole: !GetAtt AuthFunctionRole.Arn
          ...

  AuthFunction:
    Type: AWS::Serverless::Function
    ...

  # AuthFunctionRole:
  #   Type: AWS::IAM::Role
  #   Properties:
  #     AssumeRolePolicyDocument:
  #       Version: "2012-10-17"
  #       Statement:
  #         - Effect: "Allow"
  #           Principal:
  #             Service:
  #               - "apigateway.amazonaws.com"
  #           Action:
  #             - sts:AssumeRole
  #     Policies:
  #       - PolicyName: "InvokeAuthFunction"
  #         PolicyDocument:
  #           Version: "2012-10-17"
  #           Statement:
  #             - Effect: "Allow"
  #               Action:
  #                 - lambda:InvokeAsync
  #                 - lambda:InvokeFunction
  #               Resource: !GetAtt AuthFunction.Arn

If I do not set a function role explicitly, I expect one to be created for me, setting the proper policy for API GW to invoke the function, similar to this:

image

If I uncomment the function role above, the role is created, but the above "Invoke permissions" setting is still not active.

@mrichman mrichman added stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. type/bug labels Feb 23, 2023
@aaythapa aaythapa self-assigned this Feb 23, 2023
@aaythapa
Copy link
Contributor

Thanks for reporting the issues, we'll look into the problem and work on a fix

@aaythapa
Copy link
Contributor

Looks like SAM HttpApi doesn't automatically create a Lambda Permissions resource that allows the API resource to invoke the authorizer Lambda (SAM RestApi creates this permissions resource automatically when FunctionArn is defined). Adding the following resource to your template will allow the API to invoke the authorizer lambda.

 LambdaPermissions:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt <auth-function-logical-id>.Arn
      Principal: apigateway.amazonaws.com
      SourceArn: !Sub 
          - "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiID}/authorizers/*"
          - ApiID: !Ref <http-api-logical-id>

You shouldn't need to define the AuthFunctionRole resource or set FunctionInvokeRole after adding the permissions resource. This is a workaround for now and we'll add an opt-in property that will automatically create this resource for the user.

@aaythapa aaythapa added stage/in-progress A fix is being worked on area/resource/http-api and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Feb 28, 2023
@aaythapa
Copy link
Contributor

The PR is merged and the change will be releasing in the coming weeks.

@ryan-alley
Copy link

Looks like SAM HttpApi doesn't automatically create a Lambda Permissions resource that allows the API resource to invoke the authorizer Lambda (SAM RestApi creates this permissions resource automatically when FunctionArn is defined). Adding the following resource to your template will allow the API to invoke the authorizer lambda.

 LambdaPermissions:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt <auth-function-logical-id>.Arn
      Principal: apigateway.amazonaws.com
      SourceArn: !Sub 
          - "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiID}/authorizers/*"
          - ApiID: !Ref <http-api-logical-id>

You shouldn't need to define the AuthFunctionRole resource or set FunctionInvokeRole after adding the permissions resource. This is a workaround for now and we'll add an opt-in property that will automatically create this resource for the user.

This was a huge help. Took forever for me to figure out this issue. Especially since, if you run it locally, everything works, and it isn't very intuitive because none of the SAM templates you see in examples have an additional role or permission.

@andiradulescu
Copy link

For anyone looking for a quick solution (using the mentioned merged PR), just put this in your Type: AWS::Serverless::HttpApi Resource -> Properties -> Auth:

...
EnableFunctionDefaultPermissions: true

FunctionInvokeRole can be deleted.

My SAM template was written with the help of:

This is how my HttpApi Resource looks like:

Resources:
  Api:
    Type: AWS::Serverless::HttpApi
    Properties:
      Auth:
        DefaultAuthorizer: Authorizer
        Authorizers:
          Authorizer:
            FunctionArn: !GetAtt AuthorizerFunction.Arn
            Identity:
              Headers:
                - Authorization
              ReauthorizeEvery: 300
            AuthorizerPayloadFormatVersion: 2.0
            EnableFunctionDefaultPermissions: true
            EnableSimpleResponses: true

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

No branches or pull requests

4 participants