serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Issue with Lambda-Permission for API-Gateway

Open gpuettmann opened this issue 9 years ago • 37 comments

All,

it seems to me that there is a bug with the created LAMBDA-Policy when creating a "ANY"- API-Gateway-Mapping to a Lambda-Function, see the template here:

app_spec.txt

The created Lambda policy includes (for Resource analytics the following condition), and therefore a Lambda permission Error occurs when testing the API-Gateway:

"Condition":{"ArnLike":{"AWS:SourceArn":"arn:aws:execute-api:us-east-1:310073746091:yi1i6eckj4/*/ANY/analytics/*"}}

instead of (resource manual has been created manually afterwards via API-Gateway-Gui):

"Condition":{"ArnLike":{"AWS:SourceArn":"arn:aws:execute-api:us-east-1:310073746091:yi1i6eckj4/*/*/manual/*"}}

Any Idea?

Best Regards,

Gregor [email protected]

gpuettmann avatar Jan 11 '17 17:01 gpuettmann

Where is the actual error? Does it happen when you make HTTP request to the API, say via Curl?

sanathkr avatar Jan 26 '17 20:01 sanathkr

...yes, the error occurs, when I test the function, Error-Message: "Tue Jan 31 14:52:20 UTC 2017 : Execution failed due to configuration error: Invalid permissions on Lambda function Tue Jan 31 14:52:20 UTC 2017 : Method completed with status: 500"

gpuettmann avatar Jan 31 '17 15:01 gpuettmann

This error is on the APIGateway Web Console UI right? If so, I think this is a known problem

sanathkr avatar Feb 02 '17 21:02 sanathkr

Hi, yes it seams that permissions for api gateway to call laambdas are not given to certain endpoints, I have seen it randomly in some resources of mine too

ghost avatar Apr 23 '17 08:04 ghost

Hi @sanathkr , I'm experiencing the same problems as the others here. Hopefully I can provide some more info.

I have a SAM with an API and 3 lambdas. Everything is deployed successfully and connected using stage variables as well. My API cannot talk to the lambda because of the permissions issue mentioned above.

I was able to click to change the lambda: image

and then this is the modal that popped:

image

After I said "OK", the lambda was reachable via API Gateway.

In addition, since I'm using a stage variable, it provided an extra message:

You defined your Lambda function as a stage variable; you must manually give permissions to all the functions you will use. You can do this by running the below AWS CLI command for each function, replacing the stage variable in the function-name parameter with the necessary function name.

aws lambda add-permission --function-name arn:aws:lambda:us-east-1:1234567890:function:${stageVariables.SignupFunctionName} --source-arn 'arn:aws:execute-api:us-east-1:1234567890:aeleq5o0z0/*/GET/v1/accounts/*/groups' --principal apigateway.amazonaws.com --statement-id 123456-96c3-4362-b900-860d14191d9f --action lambda:InvokeFunction

Is there a step missing when the AWS::Serverless::Api is created to access the lambdas?

ryanlitalien avatar Apr 24 '17 14:04 ryanlitalien

Corresponding AWS link regarding this issue: https://forums.aws.amazon.com/thread.jspa?threadID=217143

ryanlitalien avatar Apr 24 '17 15:04 ryanlitalien

@gpuettmann, @Squareomatic Hey, I was able to get this to work by applying permissions on the lambda. I added this config to the sam yaml:

  ConfigLambdaPermission:
    Type: "AWS::Lambda::Permission"
    DependsOn:
    - MyAwesomeApi
    - ConfigHandlerLambdaFunction
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !Ref ConfigHandlerLambdaFunction
      Principal: apigateway.amazonaws.com

ryanlitalien avatar Apr 26 '17 14:04 ryanlitalien

@ryanlitalien I'm trying to do the same (API GW + Lambda) but via cloudformation. Would you mind posting a sharable version of your cloudformation template ? thnx

BadriRaghunathan avatar Oct 25 '17 18:10 BadriRaghunathan

@ryanlitalien I am too facing difficulties with Lambda permission for the API Gateway. Would you be able to post a complete sam yaml for the permission with a proxy-any example?

haiko avatar Nov 20 '17 12:11 haiko

@BradNation4Eva @haiko Unfortunately I don't have access to the source anymore. Essentially you'll need to explicitly add and set permissions within the sam, with the DependsOn so it's run after the API GW and Lambda are created. Good luck 👍

ryanlitalien avatar Nov 20 '17 15:11 ryanlitalien

I ran into the same issue and fixed it thanks to @ryanlitalien . You can see a simple example here: https://github.com/luebken/hello-sam

Now my question would be if this permission is always needed in an API / Lambda example. And if that is the case if it shouldn't be added to the examples.

luebken avatar Nov 30 '17 11:11 luebken

Works for me, thanks for sharing!

haiko avatar Dec 02 '17 15:12 haiko

If you are using APIGW Console Test button, then yes this problem still exists. But if you Curl the API or go using browser default SAM settings will work. This is a bug, and the workaround you suggested is only temporarily needed until we fix this.

If you are using separate Swagger, make sure you add the APIs to the events section as well. This will make SAM add correct Lambda::Permissions. See https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/api_swagger_cors/template.yaml#L24-L35

sanathkr avatar Dec 02 '17 18:12 sanathkr

@sanathkr I've been experiencing the same issue with an ANY method*, when using a function name with a stage variable. That is when using the configuration just as in the api_swagger_cors example in the documentation, and not just from the test button in the console, but when querying externally as well.

The resolution has been using the explicit ConfigLambdaPermission as described by ryanliatien above.

*no issues when using an explicit POST instead of ANY (this could be a red herring though)

jsancho avatar Dec 30 '17 11:12 jsancho

I am reaching out to the relevant teams internally to see what we can do about this. Will update when more information is available.

jfuss avatar Jan 10 '18 20:01 jfuss

I'm having the problem with nodejs "aws-sdk". while testing the invoking the test getting the internal server error with 500. How can we enable the permission from nodejs "aws-sdk"

AnilRo-Kore avatar Jan 22 '18 07:01 AnilRo-Kore

I am also getting this issue when I use an ANY method for an endpoint.

tqhoughton avatar Feb 01 '18 16:02 tqhoughton

Edit: My suggestion below is incorrect, please see @ryanlitalien workaround suggestion above. We are working on getting this addressed. Thanks and sorry for any trouble my suggestion caused.

All,

I spoke with the relevant team and have a path forward. If you are seeing problems calling your API Gateway endpoint (outside of the Test Button on the console), update the Method: ANY to Method: "*" until we can get out the update to this policy.

jfuss avatar Feb 12 '18 16:02 jfuss

@jfuss the above didn't work for us and sadly we found that the issue still occurred when using curl. The exact scenario was:

  • We had two lambda functions (different handlers) defined
  • We didn't have an API defined (we allowed the transform to handle this)
  • We used package / deploy to deploy

jjmschofield avatar Feb 24 '18 01:02 jjmschofield

@jjmschofield Looks like I didn’t test deep enough before suggesting. My fault! You are correct, that doesn’t seem to work. Looks like the only option may be to do what was suggested above by others by applying permissions manually in the template. There is an example above. I will edit my above comment to reflect that it will not work. Sorry for the troubles and thank you for pointing it out.

jfuss avatar Feb 24 '18 03:02 jfuss