Dev1ce
Dev1ce

Reputation: 5954

How to provide AWS API Gateway Custom Authorizer a Lambda Permission?

I have an AWS Lambda function, to which I am trying to provide permission after successfully setting it as an Authorizer.

Basically I want to achieve the following in CloudFromation -

enter image description here

Following is my CloudFormation resource, which is unable to set the permission -

GWAuthPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt AuthTest.Arn
      Principal: "apigateway.amazonaws.com"
      SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGatewayRestApi}/authorizers/${AuthTest}"

ApiGatewayRestApi - is the logical Id of the Gateway
AuthTest - is the logical Id of the Custom Auth lambda function

Upvotes: 13

Views: 8943

Answers (1)

Dev1ce
Dev1ce

Reputation: 5954

I was able to resolve it with the following -

Added AWS::ApiGateway::Authorizer resource,
And referred it to AWS::Lambda::Permission

Code -

GWAuth:
    Type: AWS::ApiGateway::Authorizer
    Properties: 
      AuthorizerUri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AuthLambda.Arn}/invocations"
      RestApiId: !Ref ApiGatewayRestApi
      Type: "REQUEST"
      IdentitySource: method.request.header.authorization
      Name: custom_auth

  GWAuthPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt AuthLambda.Arn
      Principal: "apigateway.amazonaws.com"
      SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGatewayRestApi}/authorizers/${GWAuth}"

Upvotes: 12

Related Questions