There is no way to use a existing cloud front distribution in aws cdk.
Reusing existing components is useful in several use cases. one of them is mentioned in the stack overflow question.
the solution should be as it is on other resources ex:
static fromUserPoolAttributes(scope, id, attrs)
for cognito UserPools.
This is a :rocket: Feature Request
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!
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 newDistribution
construct does. The CloudFront module is currently being redesigned and implemented to be cleaner and easier to use, and the newDistribution
construct is the up-and-coming (and currently experimental) version.The following will import an existing Distribution.