How to pass Cognito user info to a Lambda function

0

Hello,

I am using a Cognito user pool authorizer in my REST API.

I am trying to pass the Cognito user information (e.g. username, user group) from the API Gateway to a Lambda function, i.e. passing it in the 'event' or 'context' of the request.

My goal is to query different databases (test/dev/etc.) according to which user group the caller belongs to.

What is a good way to proceed in this case?

I saw the 'invoke with caller credentials' checkbox in the API Gateway, but it cannot be checked with a Cognito UP authorizer. I also tried switching from a Cognito authorizer to IAM, but don't know what execution role to select.

Any help would be greatly appreciated.

已提問 4 年前檢視次數 3587 次
1 個回答
0

I found out: I had to check the "Lambda proxy integration" checkbox in the Integration request of the method.

Then the Lambda can fetch the user info from the event object of the lambda handler (e.g. in Python):

event['requestContext']['authorizer']['claims']['cognito:groups']

(or: 'cognito:username', etc.)

Then I could query different databases according to which group the user belongs to.

Additional info: it is also possible to assume the user role from within the Lambda function, to make sure to access only what's the user is allowed to (by default only the Lambda function's role is assumed to execute actions); this is very well explained at https://www.youtube.com/watch?v=GDkkDUnICrs

已回答 4 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南