How do I get a user's Cognito Identity that they use to login?

I want this because I want to be able to write to a user’s private S3 bucket using a Lambda function (which always has path ‘bucketname/private/{CognitoIdentity}’

I have tried having the user invoke a Lambda function through an App Sync GraphQL api but this gives me Cognito Client ID and User ID and not the Cognito Identity.

AppSync resolvers that make use of Cognito can get the username from ctx.identity.username (see https://docs.aws.amazon.com/appsync/latest/devguide/security-authz.html)

This object does not contain what I’m looking for. It contains:

  • User Pool Username
  • Cognito Client ID

It does not contain Cognito Identity ID.

Congrats, you’ve stumbled into another gotcha of cognito; You cannot establish a mapping of a user pool user to an identity pool ID without passing it from the client into your API.

https://github.com/aws-amplify/amplify-js/issues/54

Is there a reason you cannot use the cognito username (aka: “sub”)? It uniquely identifies each user.

Am I right in saying the workaround described in that thread is:

  1. Have the user write to S3 from the app frontend and store the Cognito Identity
  2. Have the user read back that information then pass it to the backend

The S3 file path that the user writes to is always bucketname/private/{Cognito Identity}.

I want a backend app to be able to read and write files to and from users S3 folders. So I need a way to link users to their folders in the backend.

{
“accountId” : “12321434323”,
“cognitoIdentityPoolId” : “”,
“cognitoIdentityId” : “”,
“sourceIP” : “”,
“caller” : “ThisistheprincipalARN”,
“username” : “username”,
“userArn” : “Sameasabove”
}

That’s the object listed on the page, what’s in the cognitoIdentityId field in your case?

`import { Auth, Storage } from ‘aws-amplify’;

export const uploadFileToS3 = async (props) => {
  if (!props) return
  try {
    const credentials = await Auth.currentCredentials();
    const fileobj = props.file
    
    const [file, extension] = fileobj.name.split('.');
    const mimeType = fileobj.type;
    const bucket = props.bucket
    const fileKey = uuidv4()+'.'+extension
    const identityId = credentials.identityId;

    // rest of code`

This is a snippet from some working React code. The identityId is included in Auth.currentCredentials() and can be used when reading/writing to S3 buckets.

Right. Is there a reason you cannot use Cognito Username instead? Including when you are writing the data? I realize that’s not the question you asked but it may be an easier path forward. The Cognito username is a GUID that always represents that user.

As far as I know you can’t edit that prefix of the S3 file path.

You can definitely make the path whatever you want it to be when you store an object in S3. Do you mean you would have difficulty going back and change existing object? Are you perhaps using a library that is setting that path for you?

Your life in the long term will be much easier if you just use the Cognito sub / username.

I’m not 100% on this because the whole Cognito thing is super confusing. But I think you can’t, because identity pool identities are not always mapped 1:1 to Cognito use pool users. I think it’s possible to create a user without them actually having an associated identity pool identity until they log in. So there will be times when it isn’t possible to find it.

You may want to just keep an index in DynamoDB of both user sub and identity id. Plus any other user metadata you might want to store not on the user.

Why are you trying to use the identity? That will change depending on how the user logged in. Use the user ID.

He’s probably using Amplify, which has file uploading with the identity as part of the default file prefix:

https://docs.amplify.aws/gen1/javascript/build-a-backend/storage/upload/

I really hate that so many people new to AWS think Amplify is the only way to interact with Cognito and AppSync. It’s great that there is an approachable entry point but I really wish that the possibility / capability of using the backing services without Amplify were discussed a bit more.

For an internal project I researched Amplify for a day and immediately rejected it, it felt way too opinionated and overengineered to me, just using Cognito directly proved to be so much easier long term.

Yep, sounds like you’re looking for the inverse relationship so you’ll have to model that too. If you are using both IAM and JWTs for access-control it’s security sensitive. Be sure to verify both before linking the two.