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.
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.
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.
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.
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.