aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.55k stars 3.87k forks source link

cognito: Remove custom resource from UserPoolDomain to get `CloudFrontDistribution` attribute #31342

Open tmokmss opened 3 weeks ago

tmokmss commented 3 weeks ago

Describe the feature

Currently we use an AwsCutstomResource to get cloudFrontDomainName attribute for a UserPoolDomain resource.

https://github.com/aws/aws-cdk/blob/4b9643f28edc2c530809931ccd7a17a811891af2/packages/aws-cdk-lib/aws-cognito/lib/user-pool-domain.ts#L129-L136

But actually CFn exposes the attribute natively. We can remove the unnecessary custom resource usage.

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

Use Case

To get a CloudFront domain for a user pool domain without using a custom resource. c.f. https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-add-custom-domain.html

Proposed Solution

We can just replace it with a getAtt call.

  public get cloudFrontDomainName(): string {
    return resource.getAtt('CloudFrontDistribution').toString(),
  }

Other Information

No response

Acknowledgements

CDK version used

2.155.0

Environment details (OS name and version, etc.)

macOS

ashishdhingra commented 2 weeks ago

Yes, it makes sense to remove usage of custom resource given that CloudFrontDistribution could be obtained from Fn::GetAtt. We would also need to store resource reference in a private field.

The following code works:

import * as cdk from 'aws-cdk-lib';
import * as cognito from 'aws-cdk-lib/aws-cognito';

export class CdktestStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const userPool = cognito.UserPool.fromUserPoolId(this, 'userpool', 'us-east-2_G27QXonjy');

    const resource = new cognito.CfnUserPoolDomain(this, 'Resource', {
      userPoolId: userPool.userPoolId,
      domain: 'myexampledomain'
    });

    const cloudFrontDistribution = resource.getAtt('CloudFrontDistribution');
    new cdk.CfnOutput(this, 'CloudFrontDistribution', { value: cloudFrontDistribution.toString()});
  }
}

produces the below output during cdk deploy:

✨  Synthesis time: 4.44s

CdktestStack:  start: Building 3d7f64ff11c6c4f29991c464279067ff6118a2f56e5e24e568922a6fc616da65:139480602983-us-east-2
CdktestStack:  success: Built 3d7f64ff11c6c4f29991c464279067ff6118a2f56e5e24e568922a6fc616da65:139480602983-us-east-2
CdktestStack:  start: Publishing 3d7f64ff11c6c4f29991c464279067ff6118a2f56e5e24e568922a6fc616da65:139480602983-us-east-2
CdktestStack:  success: Published 3d7f64ff11c6c4f29991c464279067ff6118a2f56e5e24e568922a6fc616da65:139480602983-us-east-2
CdktestStack: deploying... [1/1]
CdktestStack: creating CloudFormation changeset...

 ✅  CdktestStack

✨  Deployment time: 18.79s

Outputs:
CdktestStack.CloudFrontDistribution = d1lcia0inyjsq.cloudfront.net
Stack ARN:
arn:aws:cloudformation:us-east-2:139480602983:stack/CdktestStack/ad8fa120-6f9b-11ef-8dd4-02a04ad84bdd

✨  Total time: 23.24s