I am trying to call Cognito adminCreateUser via aw...
# help
f
I am trying to call Cognito adminCreateUser via aws.CognitoIdentityServiceProvider, but i got error
UnrecognizedClientException: The security token included in the request is not valid
, i can do that in terminal bit not in lambda.
f
Hey @Fadi saadeldin, are you getting this on ur local, ie.
npm run start
?
f
yes
f
I see. The Lambda doesn’t use ur local credentials. They are using the real IAM credentials assigned by AWS Lambda.
What permissions does ur Lambda have?
(btw, if u ever wanted to read up on why local credentials are not used, it’s talked about here https://docs.serverless-stack.com/live-lambda-development)
f
Copy code
const api = new sst.ApolloApi (this, 'Api', {
  
    });

    api.attachPermissions([
      "cognito-identity:*",
      "cognito-idp:*",
      "cognito-sync:*",
      "iam:ListRoles",
      "iam:ListOpenIdConnectProviders",
      "iam:GetRole",
      "iam:ListSAMLProviders",
      "iam:GetSAMLProvider",
      "kinesis:ListStreams",
      "lambda:GetPolicy",
      "lambda:ListFunctions",
      "sns:GetSMSSandboxAccountStatus",
      "sns:ListPlatformApplications",
      "ses:ListIdentities",
      "ses:GetIdentityVerificationAttributes",
      "mobiletargeting:GetApps",
      "acm:ListCertificates"
    ])
I added the above permissions to my ApolloApi
f
Hmm.. that looks right to me. Do you mind also share two things: • a screenshot of the error in your terminal; • a snippet of how you are calling the
adminCreateUser
in ur Lambda code.
f
Copy code
const cognitoIdentityService = new aws.CognitoIdentityServiceProvider ({ apiVersion: '2016-04-19' });
    const userData = {
      UserPoolId: app.aws.cognito.userPoolId,
      Username: email,
      DesiredDeliveryMediums: ['EMAIL'],
      UserAttributes: [
        { Name: 'email', Value: email },
        { Name: 'email_verified', Value: 'true' },
      ],
    };
    const data = await cognitoIdentityService.adminCreateUser (userData).promise ();
    await cognitoIdentityService.adminAddUserToGroup ({
      UserPoolId: app.aws.cognito.userPoolId,
      Username: email,
      GroupName: group,
    }).promise ();
a
I'm facing the same issue, any help would be appreciated
f
@Fadi saadeldin sorry for the delay. Looking at the screenshot you share, are you returning the
{ data: { invitedUser: null }, error: ..}
? (cc @Jay do you recognize these errors? Are they coming from Cognito?)
f
No, i got this error from catch error
catch (error) {throw new ApolloError (error);}
f
@Fadi saadeldin @Abdul Taleb I was able to get this to work. Here’s a sample repo I created https://github.com/fwang/sst-triage-admin-create-user
The repo creates an
Api
and an
Auth
, and when curling the Api’s endpoint, the Lambda function calls
adminCreateUser
, and returns the created user.
It uses
Api
instead of
ApolloApi
, but the idea is the same.
Give it a try and let me know if it works for you.
a
I'll give it a try, thanks!
f
I’ll give it a try as well, thanks!
a
It's still not working
the only difference is the following: • I'm using ApolloApi • I'm using
new cognito.UserPool
construct and then I pass it to the
Auth
construct
Copy code
const auth = new sst.Auth(this, 'Auth', {
  cognito: { userPoolId: myUserPool.userPoolId, clientId: .... }
});
f
Hey @Abdul Taleb Did the
sst-triage-admin-create-user
repo work for you (without changing anything)?
a
yes when we created a simple
const auth = new Auth(this, 'auth', { cognito: true }):
it worked
f
I see, that’s good. So from there, can you create a
new cognito.UserPool
and pass that into the
Auth
construct (while keeping the Api, don’t make the ApolloApi change yet).
Can you see if that works for you?
a
nope doesn't work
Copy code
const tempAuth = new sst.Auth(this, 'temp-auth', {
      cognito: {
        userPool: new cognito.UserPool(this, 'test-user-pool'),
      }
    })
f
I see. Let me give it a try in a bit.
a
Okay I think it may have to do with the ApolloApi construct
I used the same userpool in the Api code you shared and also in my ApolloApi and it worked in the API lambda but not the ApolloApi one
So I upgraded the aws-sdk from v2 to v3 and it worked. strange error.
f
Oh I see. That’s a weird one 🤔