Hey guys, Is it possible to reuse an existing Clou...
# help
m
Hey guys, Is it possible to reuse an existing CloudFront distribution? I have 2 separate CDK apps the first creates a CloudFront dist which also has a custom domain associated with it, serving a static website. I have another s3 website that I want behind the same CloudFront (and use the same custom domain name) and I want to associate a behavior (admin & admin/*) to it. I am able to achieve the desired result manually by updating Cloudfront from the console. Unable to do it from CDK.
Copy code
const distribution = cloudfront.Distribution.fromDistributionAttributes(this, name('cdn'), {
            distributionId: cdk.Fn.getAtt(name('distName'), 'app1devDistID'),
            domainName: siteDomain
        });
The above code works but this
distribution
object doesn't have any methods. only a few properties.
if www.zyx.com -> website1 (bucket1) if www.xyz.com/admin -> website2 (bucket2)
One hack is to create the bucket with the first CDK app and populate the contents with the second CDK app. But what if this was an API gateway resource instead of s3🤔
f
Try using
bucket1
as the
defaultBehavior
, and add
bucket2
as an
additionalBehavior
The code roughly looks like this:
Copy code
new cloudfront.Distribution(this, 'myDist', {
  defaultBehavior: {
    origin: bucket1Origin,
    viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
  },
  additionalBehaviors: {
    '/admin': {
      origin: bucket2Origin,
      viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
    },
  },
});
m
.addBehavior()
might work, I will try and let you know.
@Frank, Reusing CDN didn't work.
f
Can you structure the stacks such that
bucket2
is imported to the stack that’s creating the Distribution? So you aren’t importing the Distribution.
m
Yep, doing that!