Aws-cdk: aws-cloudfront needs fromDistributionAttributes support

Created on 2 Jan 2020  路  6Comments  路  Source: aws/aws-cdk

There is no way to use a existing cloud front distribution in aws cdk.

Use Case

Reusing existing components is useful in several use cases. one of them is mentioned in the stack overflow question.

https://stackoverflow.com/questions/58286081/re-use-existing-cloudfront-distribution-with-aws-cdk/59561890#59561890

Proposed Solution

the solution should be as it is on other resources ex:

static fromUserPoolAttributes(scope, id, attrs) for cognito UserPools.

Other

  • [ ] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

@aws-cdaws-cloudfront efformedium feature-request in-progress p1

Most helpful comment

For all those following this issue, it's worth noting that while the existing CloudFrontWebDistribution does not support importing a distribution, the new Distribution construct does. The CloudFront module is currently being redesigned and implemented to be cleaner and easier to use, and the new Distribution construct is the up-and-coming (and currently experimental) version.

The following will import an existing Distribution.

const distribution = cloudfront.Distribution.fromDistributionAttributes(scope, 'ImportedDist', {
  domainName: 'd111111abcdef8.cloudfront.net',
  distributionId: '012345ABCDEF',
});

All 6 comments

This would be nice as our CI/CD uses CDK to deploy an isolated stack for each branch.

Spinning up a new CloudFront distribution takes a long time so we'd like to just register a new origin on an existing CloudFront distribution outside of the stack. This would improves developer experience as the branch CI pipelines would complete much faster.

Any news as to whether this feature is in the plans or not ?

For all those following this issue, it's worth noting that while the existing CloudFrontWebDistribution does not support importing a distribution, the new Distribution construct does. The CloudFront module is currently being redesigned and implemented to be cleaner and easier to use, and the new Distribution construct is the up-and-coming (and currently experimental) version.

The following will import an existing Distribution.

const distribution = cloudfront.Distribution.fromDistributionAttributes(scope, 'ImportedDist', {
  domainName: 'd111111abcdef8.cloudfront.net',
  distributionId: '012345ABCDEF',
});

Hi @njlynch, when utilizing fromDistributionAttributes, is there already a way (or a plan to add a way) to do something like existingDistribution.addBehavior()? I unfortunately wasn't able to find the right information helping me with this. Is the goal to have a returned class similar to the one from User.fromUserName (which provides some functions to the developer) in the end?

@HannesT117 - Unfortunately not. Generally, imported resources can't be themselves modified; they can have other dependent resources added to them (e.g., new listener for a load balancer) and can be referenced by other constructs (e.g., use the distribution for a Route53 target, reference ARNs or identifiers in policies). The distribution and all of its properties -- including the origins and behaviors -- are all part of a single entity and so can't be modified on import.

Ah, that's a pity! Thanks for the fast reply and information though!

Was this page helpful?
0 / 5 - 0 ratings