title: Maintain session state in AWS Lambda
body: <p>How do I maintain session state in an <strong>AWS Lambda</strong>? For example, if I need to query <strong>DynamoDb</strong> for subscription information for a logged-in user, how do I do that from the Lambda function if the user is using an <strong>AngularJS</strong> web app?</p>
  <p>I have the user logging in with <strong>Auth0</strong> and a custom authorizer that verifies the user on AWS. But then I want to use the logged-in user's <code>CognitoID</code> to query the DynamoDB.</p>
  <p>According to AWS documentation for Lambda (node.js) (<a href="https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html</a>) you only have the Information about the Amazon Cognito identity provider when invoked through the AWS Mobile SDK.</p>
  <pre><code>identity.cognitoIdentityId
  
  identity.cognitoIdentityPoolId
  </code></pre>
  <p>But what if I use a web app that uses AngularJS and Auth0?</p>
  <p>Ok, maybe the simplest solution to store user info in the DynamoDB is just to extract the <strong>JWT</strong> on the client side, in AngularJS, and send the extracted Auth0 <code>user_id</code> —such as <code>facebook|12345</code>— along in the API request to the Lambda, which queries if the user exist in DynamoDB and if not creates a new record.</p>
  <p>Then in each request to the API that has to query user info I send the <code>user_id</code> 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.</p>