使用 AWS CDK 中的自定义域名创建 Cognito 用户池

mad*_*adu 5 amazon-web-services amazon-cognito aws-cdk

我正在尝试通过 AWS CDK 创建具有自定义域名的 Cognito 用户池。我设法让一切正常工作,直到需要ARout53托管区域中创建记录为止。我搜索了所有文件,但找不到办法做到这一点。以下是我的代码。任何帮助都会非常有用。

      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
    */
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切正常。现在的问题是如何使用CfnUserPoolDomain

任何帮助都非常有用。

mul*_*008 8

2020 年 5 月更新

UserPoolDomain结构已经扩展和UserPoolDomainTarget加入到提供此功能。

现在,您需要做的就是:

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)),
});
Run Code Online (Sandbox Code Playgroud)


Con*_*ole 2

我遇到了同样的问题,看起来 CloudFormation 没有 CfnUserPoolDomain AliasTarget 的返回参数。这意味着cdk也不能提供这个参数。

我最终使用 AWS SDK ( npm install aws-sdk) 实现它并使用 API 获取值:


更新:更好的解决方案是使用AwsCustomResource您可以在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,
})
Run Code Online (Sandbox Code Playgroud)