Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Cognito User Pool With Custom Domain name from AWS CDK

I'm trying to creating Cognito user pool with a custom domain name through AWS CDK. I manage to get everyting working untill to the point where I needed to create an A record in the Rout53 hosted zone. I searched through all the documents but coudn't find a way to do that. Following is my code. Any help would be much appriciated.

      const cfnUserPool = new CfnUserPool(this, 'MyCognitoUserPool', {
            userPoolName: 'MyCognitoUserPool',
            adminCreateUserConfig: {
                allowAdminCreateUserOnly: false
            },
            policies: {
                passwordPolicy: {
                    minimumLength: 8,
                    requireLowercase: true,
                    requireNumbers: true,
                    requireSymbols: true,
                    requireUppercase: true,
                    temporaryPasswordValidityDays: 30
                }
            },
            usernameAttributes: [
                UserPoolAttribute.EMAIL
            ],
            schema: [
                {
                    attributeDataType: 'String',
                    name: UserPoolAttribute.EMAIL,
                    mutable: true,
                    required: true
                },
                {
                    attributeDataType: 'String',
                    name: UserPoolAttribute.FAMILY_NAME,
                    mutable: false,
                    required: true
                },
                {
                    attributeDataType: 'String',
                    name: UserPoolAttribute.GIVEN_NAME,
                    mutable: false,
                    required: true
                }
            ]
        });

      const cognitoAppDomain = new CfnUserPoolDomain(this, "PigletAuthDomainName", {
            domain: authDomainName,
            userPoolId: cfnUserPool.ref,
            customDomainConfig: {
                certificateArn: 'ACM Certificate arn'
            }
        });

    /* 
      TODO: Create an A record from the created cnfUserPoolDomain
    */

Everything works up untill to this point. Now the question is how to create an A record using the CfnUserPoolDomain

Any help is much appriciated.

like image 425
madu Avatar asked Nov 27 '19 04:11

madu


People also ask

What is the main difference between Cognito user pool and Cognito identity Pool Mcq?

Short description. User pools are for authentication (identity verification). With a user pool, your app users can sign in through the user pool or federate through a third-party identity provider (IdP). Identity pools are for authorization (access control).


2 Answers

Update May 2020

The UserPoolDomain construct has been extended and a UserPoolDomainTarget was added to provide this functionality.

Now, all you need to do is the following:

const userPoolDomain = new cognito.UserPoolDomain(this, 'UserPoolDomain', {
  userPool,
  customDomain: {
    domainName: authDomainName,
    certificate,
  },
});

new route53.ARecord(this, 'UserPoolCloudFrontAliasRecord', {
  zone: hostedZone,
  recordName: authDomainName,
  target: route53.RecordTarget.fromAlias(new route53_targets.UserPoolDomainTarget(userPoolDomain)),
});
like image 132
mulles3008 Avatar answered Sep 24 '22 12:09

mulles3008


I had the same Problem, It looks like CloudFormation does not have a return parameter for the CfnUserPoolDomain AliasTarget. Which means the cdk can not provide this parameter either.

I ended up implementing it using the AWS SDK (npm install aws-sdk) and getting the value using the APIs:


Update: The better solution is to use the AwsCustomResource. You can see a detailed example in aws/aws-cdk (#6787):

const userPoolDomainDescription = new customResources.AwsCustomResource(this, 'user-pool-domain-description', {
  onCreate: {
    physicalResourceId: 'user-pool-domain-description',
    service: 'CognitoIdentityServiceProvider',
    action: 'describeUserPoolDomain',
    parameters: {
      Domain: userPoolDomain.domain
    }
  }
});

const dnsName = userPoolDomainDescription.getData('DomainDescription.CloudFrontDistribution').toString();

// Route53 alias record for the UserPoolDomain CloudFront distribution
new route53.ARecord(this, 'UserPoolDomainAliasRecord', {
  recordName: userPoolDomain.domain,
  target: route53.RecordTarget.fromAlias({
    bind: _record => ({
      hostedZoneId: 'Z2FDTNDATAQYW2', // CloudFront Zone ID
      dnsName: dnsName,
    }),
  }),
  zone,
})
like image 45
quadroid Avatar answered Sep 20 '22 12:09

quadroid