How do I maintain session state in an AWS Lambda? For example, if I need to query DynamoDb for subscription information for a logged-in user, how do I do that from the Lambda function if the user is using an AngularJS web app?
I have the user logging in with Auth0 and a custom authorizer that verifies the user on AWS. But then I want to use the logged-in user's CognitoID
to query the DynamoDB.
According to AWS documentation for Lambda (node.js) (https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html) you only have the Information about the Amazon Cognito identity provider when invoked through the AWS Mobile SDK.
identity.cognitoIdentityId
identity.cognitoIdentityPoolId
But what if I use a web app that uses AngularJS and Auth0?
Ok, maybe the simplest solution to store user info in the DynamoDB is just to extract the JWT on the client side, in AngularJS, and send the extracted Auth0 user_id
—such as facebook|12345
— along in the API request to the Lambda, which queries if the user exist in DynamoDB and if not creates a new record.
Then in each request to the API that has to query user info I send the user_id
and in the Lambda I query the db with that id. It should be safe as I verify the users token in the Custom Authorizer and deny or allow the request before it hits the Lambda.