Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create route53 ARecord for a user pool domain #6787

Closed
sblackstone opened this issue Mar 18, 2020 · 4 comments · Fixed by #7224
Closed

create route53 ARecord for a user pool domain #6787

sblackstone opened this issue Mar 18, 2020 · 4 comments · Fixed by #7224
Assignees
Labels
@aws-cdk/aws-cognito Related to Amazon Cognito effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed.

Comments

@sblackstone
Copy link
Contributor

sblackstone commented Mar 18, 2020

I'm using CfnUserPoolDomain to create a custom domain for my cognito auth.

How can I access the CloudfrontDistribution - it doesn't seem to be a property? I need this so I can create the CNAME.

If I use describe-user-pool-domain with the cli, I can see the value, but I haven't been able to find this anywhere in the CDK.

{
    "DomainDescription": {
        "UserPoolId": "xxx",
        "AWSAccountId": "xxx",
        "Domain": "xxx",
        "S3Bucket": "aws-cognito-prod-iad-assets",
        "CloudFrontDistribution": "d2i7auyy0uax3k.cloudfront.net",
        "Version": "20200318030027",
        "Status": "ACTIVE",
        "CustomDomainConfig": {
            "CertificateArn": "xxxx"
        }
    }
}
@sblackstone sblackstone added the needs-triage This issue or PR still needs to be triaged. label Mar 18, 2020
@SomayaB SomayaB added the @aws-cdk/aws-cloudfront Related to Amazon CloudFront label Mar 19, 2020
@SomayaB SomayaB added guidance Question that needs advice or information. @aws-cdk/aws-cognito Related to Amazon Cognito and removed @aws-cdk/aws-cloudfront Related to Amazon CloudFront labels Mar 19, 2020
@SomayaB SomayaB assigned nija-at and unassigned iliapolo Mar 19, 2020
@0xdevalias
Copy link
Contributor

0xdevalias commented Mar 19, 2020

I actually hit (and solved) this issue yesterday! Posted my code snippet at the following (copied below for reference):

Basically makes use of AwsCustomResource to create a custom resource backed by an AWS SDK call:

I also found I had to hack around the route53.RecordTarget.fromAlias a bit to be able to pass just the CloudFront URL directly as a string (also below).

const cdk = require('@aws-cdk/core')
const cognito = require('@aws-cdk/aws-cognito')
const cr = require('@aws-cdk/custom-resources')
const route53 = require('@aws-cdk/aws-route53')

/**
 * Configures the UserPool domain used for authentication.
 *
 * @see https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-cognito.CfnUserPoolDomain.html
 * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpooldomain.html
 * @see https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-assign-domain.html
 */
const userPoolDomain = new cognito.CfnUserPoolDomain(
  this,
  'UserPoolDomain',
  {
    userPoolId: userPool.userPoolId,
    domain: authDomain,
    customDomainConfig: {
      certificateArn,
    },
  }
)
userPoolDomain.node.addDependency(userPool)
new cdk.CfnOutput(this, 'UserPoolDomainValue', {
  value: userPoolDomain.domain,
})

// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#describeUserPoolDomain-property
const describeCognitoUserPoolDomain = new cr.AwsCustomResource(
  this,
  'DescribeCognitoUserPoolDomain',
  {
    resourceType: 'Custom::DescribeCognitoUserPoolDomain',
    onCreate: {
      region: 'us-east-1',
      service: 'CognitoIdentityServiceProvider',
      action: 'describeUserPoolDomain',
      parameters: {
        Domain: userPoolDomain.domain,
      },
      physicalResourceId: cr.PhysicalResourceId.of(userPoolDomain.domain),
    },
    // TODO: can we restrict this policy more? Get the ARN for the user pool domain? Or the user pool maybe?
    policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
      resources: cr.AwsCustomResourcePolicy.ANY_RESOURCE,
    }),
  }
)
describeCognitoUserPoolDomain.node.addDependency(userPoolDomain)

const userPoolDomainDistribution = describeCognitoUserPoolDomain.getResponseField(
  'DomainDescription.CloudFrontDistribution'
)
new cdk.CfnOutput(this, 'UserPoolDomainDistribution', {
  value: userPoolDomainDistribution,
})

// 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: userPoolDomainDistribution,
    }),
  }),
  zone,
})

Hope it helps!

@sblackstone
Copy link
Contributor Author

@0xdevalias Thanks... Hopefully, they can just add a property and we can delete out all of that!

@0xdevalias
Copy link
Contributor

I expect they will, there’s a lot of active work on improving Cognito constructs/functionality in CDK at the moment!

@nija-at
Copy link
Contributor

nija-at commented Mar 20, 2020

Thanks for the code snippet @0xdevalias. I'm marking this issue as a feature request.

@nija-at nija-at changed the title How to access CloudfrontDistribution for UserPoolDomain with custom domain create route53 ARecord for a user pool domain Mar 20, 2020
@nija-at nija-at added feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed. and removed guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Mar 20, 2020
@nija-at nija-at added the effort/medium Medium work item – several days of effort label Mar 25, 2020
@mergify mergify bot closed this as completed in #7224 Apr 22, 2020
mergify bot pushed a commit that referenced this issue Apr 22, 2020
Support for user pool domains in the Cognito module.
Domains can be explicitly configured for either custom domain or Cognito
hosted prefix domains.

Added 'cloudFrontDomainName' property that gets the CloudFront domain
name by calling `DescribeUserPoolDomain` API via a custom resource.

closes #6787.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cognito Related to Amazon Cognito effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. needs-cfn This issue is waiting on changes to CloudFormation before it can be addressed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants