Adding Cognito user via AWS API
Cognito is a fully managed identity service provided by AWS. It allows developers to easily add user sign-up, sign-in, and access control to their applications. When it comes to user registration, users can be added through the AWS console, the AWS CLI, or the AWS API. Some services, like AWS Amplify, come with user registration out of the box with an easy to use API.
What if you wanted to add more Cognito users in a bulk? It would make sense to write a script for that, and use the AWS API. This blog post will show you how.
Start off by installing the AWS SDK.
Then, create a JavaScript file and import the SDK:
Before using the API, you need to set up your credentials. If you store your AWS credentials in the AWS config file, create a new instance of SharedIniFileCredentials
:
If you use AWS SSO, you will need to npm install @aws-sdk/credential-provider-sso
and then use its fromSSO
function:
Set the credentials and the region, and create and instance of CognitoIdentityServiceProvider
:
Next, invoke the Cognito API to create the user, set the password, and optionally add the user to a Cognito group:
That's it. You can wrap the above code into its own function, especially if you want to create many users in bulk. See this GitHub gist for the entire code.