0

AWS/Cognito when creating a user pool through CDK, how can I set string length for standard attributes.

I tried to find it but had no luck like there is none for that. I am using Typescript.

My user pool looks like this:

const userPool = new cognito.UserPool(this, `name-of-user-pool-${stage}`, {
  signInAliases: {
    email: true,
    username: false,
  },
  standardAttributes: {
    fullname: { required: true, mutable: true },
  },
  passwordPolicy: {
    minLength: 8,
    requireDigits: true,
    requireLowercase: true,
    requireUppercase: true,
    requireSymbols: true,
  },
  selfSignUpEnabled: true,
  userVerification: {
    emailSubject: 'Verify your email !',
    emailBody: 'Thank you for signing up to our app! Your verification code is {####}',
    emailStyle: cognito.VerificationEmailStyle.CODE,
  },
  accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
});

1 Answers1

0

I am not sure if there are obscure implications in doing so but you can create a custom attribute with length limitations and use that instead. CDK does not complain if using the same name as a standard attribute (assuming its not used).

const name: ICustomAttribute = {
  bind: (): CustomAttributeConfig => 
    mutable: true,
    dataType: 'String',
    stringConstraints: {
      maxLen: 30,
      minLen: 10
    }
  })
}

const userPool = new UserPool(this, 'TestUserPool', {
  ...
  customAttributes: {
    name
  }
})
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • The CDK can indeed synth and deploy a template with `minLength` and `maxLength` on `name`. I used escape hatch syntax to achieve the same result. The template deploys without error. However, the `name` length constraints are \*not actually applied\* when creating a test user. Nor are the constraints on `name` visible in the console. As far as I could see, the Cognito console has no UI to display constraints for standard attributes, only for custom attributes. All this circumstantial evidence led me to my earlier comment... I don't think it's supported at all. – fedonev Jan 30 '23 at 18:21
  • Well darn. Sad panda. – cyberwombat Jan 30 '23 at 19:29
  • The other alternative I can think of is the pre sign up Lambda validation. https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html#aws-lambda-triggers-pre-registration-example-3 – cyberwombat Jan 30 '23 at 19:53