Skip to content

Instantly share code, notes, and snippets.

@dariodjuric
Created December 24, 2022 08:33
Show Gist options
  • Save dariodjuric/6e7f7557d96183620bc73bdb311ea819 to your computer and use it in GitHub Desktop.
Save dariodjuric/6e7f7557d96183620bc73bdb311ea819 to your computer and use it in GitHub Desktop.
Add a new Cognito user via API
const aws = require('aws-sdk');
const { fromSSO } = require('@aws-sdk/credential-provider-sso');
const credentials = await fromSSO({ profile: 'AdministratorAccess-xxx' })();
aws.config.update({
region: 'us-east-1',
credentials,
});
const cognito = new aws.CognitoIdentityServiceProvider({
apiVersion: '2016-04-18',
});
const createCognitoUser = async (email, password, userPoolId, groupName = null) => {
await cognito
.adminCreateUser({
UserPoolId: userPoolId,
Username: email,
UserAttributes: [
{
Name: 'email',
Value: email,
},
{
Name: 'email_verified',
Value: 'true',
},
],
})
.promise();
await cognito
.adminSetUserPassword({
Password: password,
UserPoolId: userPoolId,
Username: email,
Permanent: true,
})
.promise();
if (groupName) {
await cognito
.adminAddUserToGroup({
GroupName: groupName,
UserPoolId: userPoolId,
Username: email,
})
.promise();
}
};
createCognitoUser('email@address.com', 'somePassword', 'ADMINS', 'us-east-1_xxx');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment