AWS Cognito Tutorial

AWS Cognito Tutorial


1. Creating and Configuring an AWS Cognito User Pool

Example 1: Create a Cognito User Pool

aws cognito-idp create-user-pool --pool-name MyUserPool

Explanation

  1. Creates a new Cognito user pool for authentication.

    • aws cognito-idp create-user-pool
  2. Specifies a unique name for the user pool.

    • --pool-name MyUserPool
  3. Stores and manages users securely in AWS Cognito.

    • create-user-pool
  4. User pool supports authentication, authorization, and identity federation.

    • create-user-pool

Example 2: Get Details of a User Pool

aws cognito-idp describe-user-pool --user-pool-id us-east-1_example123

Explanation

  1. Retrieves details about a specific user pool.

    • aws cognito-idp describe-user-pool
  2. Requires the user pool ID for identification.

    • --user-pool-id us-east-1_example123
  3. Provides metadata like pool name, status, and creation date.

    • describe-user-pool
  4. Useful for checking configurations and debugging issues.

    • describe-user-pool

Example 3: List All User Pools

aws cognito-idp list-user-pools --max-results 10

Explanation

  1. Lists all available user pools in an AWS account.

    • aws cognito-idp list-user-pools
  2. Limits results to a maximum of 10 pools per request.

    • --max-results 10
  3. Useful for managing multiple authentication systems.

    • list-user-pools
  4. Returns user pool names and IDs in JSON format.

    • list-user-pools

Example 4: Delete a User Pool

aws cognito-idp delete-user-pool --user-pool-id us-east-1_example123

Explanation

  1. Deletes an existing Cognito user pool permanently.

    • aws cognito-idp delete-user-pool
  2. Requires a valid user pool ID.

    • --user-pool-id us-east-1_example123
  3. Removes all stored users and configurations.

    • delete-user-pool
  4. Cannot be undone once deleted.

    • delete-user-pool

2. Managing Users in AWS Cognito

Example 1: Create a New User

aws cognito-idp admin-create-user --user-pool-id us-east-1_example123 --username newuser --temporary-password TempPass123!

Explanation

  1. Adds a new user to the Cognito user pool.

    • aws cognito-idp admin-create-user
  2. Requires a unique username for the new user.

    • --username newuser
  3. Sets an initial temporary password for login.

    • --temporary-password TempPass123!
  4. User must reset password upon first login.

    • admin-create-user

Example 2: Authenticate a User

aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH --client-id exampleclientid --auth-parameters USERNAME=newuser,PASSWORD=TempPass123!

Explanation

  1. Initiates authentication for a user in Cognito.

    • aws cognito-idp initiate-auth
  2. Uses USER_PASSWORD_AUTH authentication flow.

    • --auth-flow USER_PASSWORD_AUTH
  3. Requires Cognito app client ID for authentication.

    • --client-id exampleclientid
  4. Provides user credentials (USERNAME, PASSWORD) for login.

    • --auth-parameters USERNAME=newuser,PASSWORD=TempPass123!

Example 3: List Users in a User Pool

aws cognito-idp list-users --user-pool-id us-east-1_example123

Explanation

  1. Retrieves all registered users in a user pool.

    • aws cognito-idp list-users
  2. Requires a user pool ID to query users.

    • --user-pool-id us-east-1_example123
  3. Returns user attributes such as email, status, and creation date.

    • list-users
  4. Useful for user management and audit purposes.

    • list-users

Example 4: Delete a User

aws cognito-idp admin-delete-user --user-pool-id us-east-1_example123 --username newuser

Explanation

  1. Removes a specific user from Cognito permanently.

    • aws cognito-idp admin-delete-user
  2. Requires the user pool ID for identification.

    • --user-pool-id us-east-1_example123
  3. Specifies the username of the user to delete.

    • --username newuser
  4. Prevents the deleted user from accessing services.

    • admin-delete-user

3. Configuring Multi-Factor Authentication (MFA)

Example 1: Enable MFA for a User Pool

aws cognito-idp set-user-pool-mfa-config --user-pool-id us-east-1_example123 --mfa-configuration ON

Explanation

  1. Enforces Multi-Factor Authentication (MFA) for users.

    • aws cognito-idp set-user-pool-mfa-config
  2. Requires a valid user pool ID.

    • --user-pool-id us-east-1_example123
  3. Sets MFA to ON for security enhancement.

    • --mfa-configuration ON
  4. Users must verify identity using additional authentication factors.

    • set-user-pool-mfa-config

Example 2: Associate a Phone Number for MFA

aws cognito-idp admin-update-user-attributes --user-pool-id us-east-1_example123 --username newuser --user-attributes Name=phone_number,Value="+1234567890"

Explanation

  1. Adds a phone number for SMS-based MFA verification.

    • aws cognito-idp admin-update-user-attributes
  2. Requires a valid user pool ID and username.

    • --user-pool-id us-east-1_example123 --username newuser
  3. Associates a phone number with the user account.

    • --user-attributes Name=phone_number,Value="+1234567890"
  4. Users receive a verification code during authentication.

    • admin-update-user-attributes

Example 3: Generate an MFA Code for Authentication

aws cognito-idp associate-software-token --session example-session-token

Explanation

  1. Generates a one-time authentication token for MFA.

    • aws cognito-idp associate-software-token
  2. Requires an active session token.

    • --session example-session-token
  3. Used in conjunction with TOTP-based authentication apps.

    • associate-software-token
  4. Ensures enhanced security during login.

    • associate-software-token

Example 4: Verify an MFA Token

aws cognito-idp verify-software-token --session example-session-token --user-code 123456

Explanation

  1. Validates a user's one-time MFA code.

    • aws cognito-idp verify-software-token
  2. Requires a session token for verification.

    • --session example-session-token
  3. Takes a user-generated code from their MFA device.

    • --user-code 123456
  4. Allows authentication only if the code is valid.

    • verify-software-token