Search code examples
amazon-s3aws-api-gatewayamazon-cognito

Can't access Cognito Identity in API Gateway to use as a path parameter


I have a PUT endpoint in API Gateway that puts an uploaded file directly into an S3 bucket.

The Method Request requires authorisation from my Cognito authorisation pool and works fine.

I want to save the uploaded file with the users Cognito User ID as the filename.

I've tried creating a URL Path Parameter using context.identity.cognitoIdentityId but it's always blank. I've also tried getting access to context.identity.user which is also blank. If I create a path parameter for context.requestId this works fine and I can use it in the Path override.

I've also tried setting a HTTP Header as X-Cognito-Id=context.identity.cognitoIdentityId then accessing method.request.header.X-Cognito-Id in the Path Parameters section, but again it's still blank.

Here's a screenshot of my current setup trying multiple things. This results in a filename that has the rid and wishid but nothing else.

enter image description here

My other endpoints that use Lambda functions can access the Cognito user just fine.


Solution

  • I believe what you are looking for is: context.authorizer.claims[cognito:username] or possibly context.authorizer.claims.email depending on how you set up Cognito.

    To see a list of all available properties you can temporarily add this to your mapping template: $context.authorizer.claims.keySet().