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

feat(cognito): new cloudFrontEndpoint method for user pool domain without custom resource #31402

Merged
merged 21 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"S3Bucket": {
"Fn::Sub": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}"
},
"S3Key": "746da84b10e215c552e68b6d2061024e4429f0386f43a35ef5e4d2940655692e.zip"
"S3Key": "9e8936ba1db43e0919ba2fc8265d50686eeaca82830c471ff8b7b0672c5970ec.zip"
},
"Handler": "index.handler",
"Role": {
Expand Down Expand Up @@ -186,6 +186,14 @@
"DomainDescription.CloudFrontDistribution"
]
}
},
"CloudFrontEndpoint": {
"Value": {
"Fn::GetAtt": [
"UserPoolDomainD0EA232A",
"CloudFrontDistribution"
]
}
}
},
"Mappings": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { App, CfnOutput, RemovalPolicy, Stack } from 'aws-cdk-lib';
import { UserPool } from 'aws-cdk-lib/aws-cognito';
import { LOG_API_RESPONSE_DATA_PROPERTY_TRUE_DEFAULT } from 'aws-cdk-lib/cx-api';

/*
* Stack verification steps:
* * Verify that the CloudFrontDistribution stack output is of the format 'xxxxxxxxxxxxxx.cloudfront.net'
*/

const app = new App();
const app = new App({ postCliContext: { [LOG_API_RESPONSE_DATA_PROPERTY_TRUE_DEFAULT]: false } });
go-to-k marked this conversation as resolved.
Show resolved Hide resolved
const stack = new Stack(app, 'integ-user-pool-domain-cfdist');

const userpool = new UserPool(stack, 'UserPool', {
Expand All @@ -26,3 +27,7 @@ new CfnOutput(stack, 'Domain', {
new CfnOutput(stack, 'CloudFrontDomainName', {
value: domain.cloudFrontDomainName,
});

new CfnOutput(stack, 'CloudFrontEndpoint', {
value: domain.cloudFrontEndpoint,
});
18 changes: 17 additions & 1 deletion packages/aws-cdk-lib/aws-cognito/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aw
- [Table of Contents](#table-of-contents)
- [User Pools](#user-pools)
- [Sign Up](#sign-up)
- [Code Verification](#code-verification)
- [Link Verification](#link-verification)
GavinZZ marked this conversation as resolved.
Show resolved Hide resolved
- [Sign In](#sign-in)
- [Attributes](#attributes)
- [Attribute verification](#attribute-verification)
Expand Down Expand Up @@ -921,7 +923,6 @@ const fullAccessClient = pool.addClient('full-access-client', {
});
```


### Domains

After setting up an [app client](#app-clients), the address for the user pool's sign-up and sign-in webpages can be
Expand Down Expand Up @@ -990,6 +991,21 @@ Existing domains can be imported into CDK apps using `UserPoolDomain.fromDomainN
const myUserPoolDomain = cognito.UserPoolDomain.fromDomainName(this, 'my-user-pool-domain', 'domain-name');
```

To get the domain name of the CloudFront distribution associated with the user pool domain, use `cloudFrontEndpoint` method.

```ts
const userpool = new cognito.UserPool(this, 'UserPool');
const domain = userpool.addDomain('Domain', {
cognitoDomain: {
domainPrefix: 'my-awesome-app',
},
});

new CfnOutput(this, 'CloudFrontEndpoint', {
value: domain.cloudFrontEndpoint,
});
```

### Deletion protection

Deletion protection can be enabled on a user pool to prevent accidental deletion:
Expand Down
16 changes: 14 additions & 2 deletions packages/aws-cdk-lib/aws-cognito/lib/user-pool-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class UserPoolDomain extends Resource implements IUserPoolDomain {
private isCognitoDomain: boolean;

private cloudFrontCustomResource?: AwsCustomResource;
private resource: CfnUserPoolDomain;

constructor(scope: Construct, id: string, props: UserPoolDomainProps) {
super(scope, id);
Expand All @@ -114,18 +115,29 @@ export class UserPoolDomain extends Resource implements IUserPoolDomain {
this.isCognitoDomain = !!props.cognitoDomain;

const domainName = props.cognitoDomain?.domainPrefix || props.customDomain?.domainName!;
const resource = new CfnUserPoolDomain(this, 'Resource', {
this.resource = new CfnUserPoolDomain(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
domain: domainName,
customDomainConfig: props.customDomain ? { certificateArn: props.customDomain.certificate.certificateArn } : undefined,
});

this.domainName = resource.ref;
this.domainName = this.resource.ref;
}

/**
* The domain name of the CloudFront distribution associated with the user pool domain.
*/
public get cloudFrontEndpoint(): string {
Copy link
Contributor Author

@go-to-k go-to-k Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method name is based on the description in CFn doc.

The Amazon CloudFront endpoint that you use as the target of the alias that you set up with your Domain Name Service (DNS) provider.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpooldomain.html#aws-resource-cognito-userpooldomain-return-values

return this.resource.getAtt('CloudFrontDistribution').toString();
}

/**
* The domain name of the CloudFront distribution associated with the user pool domain.
*
* This method creates a custom resource internally to get the CloudFront domain name.
*
* @deprecated use `cloudFrontEndpoint` method instead.
*/
public get cloudFrontDomainName(): string {
if (!this.cloudFrontCustomResource) {
const sdkCall: AwsSdkCall = {
Expand Down
26 changes: 24 additions & 2 deletions packages/aws-cdk-lib/aws-cognito/test/user-pool-domain.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import { Template } from '../../assertions';
import { Certificate } from '../../aws-certificatemanager';
import { CfnParameter, Stack } from '../../core';
Expand Down Expand Up @@ -103,7 +104,7 @@ describe('User Pool Domain', () => {
})).not.toThrow();
});

test('custom resource is added when cloudFrontDomainName property is used', () => {
testDeprecated('custom resource is added when cloudFrontDomainName property is used', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');
Expand Down Expand Up @@ -137,7 +138,7 @@ describe('User Pool Domain', () => {
});
});

test('cloudFrontDomainName property can be called multiple times', () => {
testDeprecated('cloudFrontDomainName property can be called multiple times', () => {
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');
const domain = pool.addDomain('Domain', {
Expand All @@ -152,6 +153,27 @@ describe('User Pool Domain', () => {
expect(cfDomainNameSecond).toEqual(cfDomainNameFirst);
});

test('cloudFrontEndpoint property can be called without custom resource', () => {
const stack = new Stack();
const pool = new UserPool(stack, 'Pool');
const domain = pool.addDomain('Domain', {
cognitoDomain: {
domainPrefix: 'cognito-domain-prefix',
},
});

const cloudFrontEndpoint = domain.cloudFrontEndpoint;

expect(stack.resolve(cloudFrontEndpoint)).toEqual({
'Fn::GetAtt': [
'PoolDomainCFC71F56',
'CloudFrontDistribution',
],
});

Template.fromStack(stack).resourceCountIs('Custom::UserPoolCloudFrontDomainName', 0);
});

test('import', () => {
// GIVEN
const stack = new Stack();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Fixture with packages imported, but nothing else
import { Duration, Stack } from 'aws-cdk-lib';
import { CfnOutput, Duration, Stack } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as certificatemanager from 'aws-cdk-lib/aws-certificatemanager';
import * as cognito from 'aws-cdk-lib/aws-cognito';
Expand Down
Loading