modify SES config with CDK for existing Cognito user pool

0

Hi Team,

I'm configuring my existing cognito user pool to use SES, by adding this block of code to my existing CDK code that define the user pool:

.....
email: cognito.UserPoolEmail.withSES({
    sesRegion: 'us-east-1',
    fromEmail: 'noreply@myawesomeapp.com',
    fromName: 'Awesome App',
    replyTo: 'support@myawesomeapp.com',
    **sesVerifiedDomain**: 'myawesomeapp.com',
  }),
.....
.....

but when I deploy the stack it failed with this error:

there is already a construct with name : my-construct-id

is not possible to change the SES config for an existing user pool?

it seems to try to create a new user pool when i tried to deploy not changing the existing one.

Thank you!

1 Answer
0

Hello,

The error “there is already a construct with name” occurs when you have two constructs with the same construct Id in the same cdk project.

I’m assuming you have created a user pool earlier and now to configure it with SES, you have created one more construct with the same construct Id to define these

email: cognito.UserPoolEmail.withSES({
    sesRegion: 'us-east-1',
    fromEmail: 'noreply@myawesomeapp.com',
    fromName: 'Awesome App',
    replyTo: 'support@myawesomeapp.com',
    **sesVerifiedDomain**: 'myawesomeapp.com',
  }),

However, you can just add these properties to the user pool construct which already exists in your cdk code, as that would update that existing user pool with the above email configurations.

const userPool = new cognito.UserPool(this, 'userpool', {
      userPoolName: 'my-user-pool',
      selfSignUpEnabled: true,
      signInAliases: {
        email: true,
      },
      autoVerify: {
        email: true,
      },
      standardAttributes: {
        givenName: {
          required: true,
          mutable: true,
        },
        familyName: {
          required: true,
          mutable: true,
        },
      },
      passwordPolicy: {
        minLength: 6,
        requireLowercase: true,
        requireDigits: true,
        requireUppercase: false,
        requireSymbols: false,
      },
      accountRecovery: cognito.AccountRecovery.EMAIL_ONLY,
      removalPolicy: cdk.RemovalPolicy.RETAIN,
      email: cognito.UserPoolEmail.withSES({
            sesRegion: 'us-east-1',
            fromEmail: 'noreply@myawesomeapp.com',
            fromName: 'Awesome App',
            replyTo: 'support@myawesomeapp.com',
          }),
    });

If this is not the case, then do check for constructs with the same construct Id in your cdk project and rename any one of them.

Please note that, if the user pool was created through console then you would have to use CDK Custom Resources to add the email configurations and pass the parameters you wish to update to the “UpdateUserPool” API call handled by that custom resource. Kindly refer to the custom resource documentation [1] and API reference documentation [2] to know more about the same.

If the above suggestions don’t suffice your use case, please feel free to elaborate more on this issue.

[1] https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.custom_resources-readme.html#custom-resource-examples

[2] https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserPool.html

AWS
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions