TerraformPilot

DevOps

Fix Terraform Error - Error Creating Cognito User Pool - UsernameExistsException

Fix UsernameExistsException when creating Cognito User Pools and users in Terraform. Handle pre-existing users, duplicate pool names, and import existing...

LLuca Berton1 min read

Quick Answer

#

A Cognito user with that username already exists in the user pool. Import the existing user, use a unique username, or delete the duplicate. This error can also occur if the user pool itself has a name conflict in the same region.

The Error

#
Error: error creating Cognito User: UsernameExistsException: 
User account already exists

What Causes This

#
  • User created outside Terraform (Console, CLI, or application sign-up)
  • Previous apply created the user but state was lost
  • User pool name collision (pool names are unique per region per account)

How to Fix It

#

Solution 1: Import the Existing User

#
# Import format: user_pool_id/username
terraform import aws_cognito_user.admin us-east-1_ABC123/admin@example.com

Solution 2: Check for Existing Users

#
aws cognito-idp admin-get-user \
  --user-pool-id us-east-1_ABC123 \
  --username admin@example.com

Solution 3: Create User Pool with Unique Name

#
resource "aws_cognito_user_pool" "main" {
  name = "${var.project}-${var.environment}-users"
 
  password_policy {
    minimum_length    = 12
    require_uppercase = true
    require_lowercase = true
    require_numbers   = true
    require_symbols   = true
  }
 
  auto_verified_attributes = ["email"]
 
  schema {
    name                = "email"
    attribute_data_type = "String"
    required            = true
    mutable             = true
  }
}
 
resource "aws_cognito_user" "admin" {
  user_pool_id = aws_cognito_user_pool.main.id
  username     = "admin@example.com"
 
  attributes = {
    email          = "admin@example.com"
    email_verified = true
  }
}

Troubleshooting Checklist

#
  1. ✅ Does the user already exist in the pool?
  2. ✅ Was the user created by application sign-up flow?
  3. ✅ Can you import the existing user?
  4. ✅ Is the user pool name unique in this region?
#

Conclusion

#

UsernameExistsException means the user already exists — often from application sign-ups or manual creation. Import existing users into Terraform state, and use environment-prefixed pool names to avoid collisions.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article