How do I get cognitoIdentityId in lambda called by API Gateway?

My API gateway functions use aws_iam security. Using Postman I can setup the security and call my API gateway function. The lambda function is being called and it generates a cloudwatch log. Inside the lamdba function I try to access: context.identity.cognitoIdentityId. This fails because context.identity is null. I also tried checking “Invoke with caller credentials” on my function in APIGateway - does not help.

So how do I get cognitoIdentityId from the lambda function?

This was asked twice before with no response

Good question, @jonsmirl. I don’t think the AWS docs are clear on this.

The context object you’re referring to there is actually included within the event object under a key of requestContext. Thus, to get the context.identity.cognitoIdentityId, you’ll need to access event.requestContext.identity.cognitoIdentityId.

It’s hard to find this in the AWS docs, so I usually go to the Serverless docs here to see an example event from API Gateway.

Can you let me know if that works for you? If it doesn’t, can you let me know the integration type you’re using for API Gateway (e.g. lambda-proxy, http, http-proxy)?

1 Like

Thanks for that answer, it was exactly what I needed. I have no clue where that is in the Amazon docs, I have been looking for this answer for two days.

You’re welcome! Sorry we missed that the first two times you asked.

I did not ask, when you search the forum two others have asked the identical question.