Skip to content

Instantly share code, notes, and snippets.

@benagricola
Last active September 16, 2020 16:49
Show Gist options
  • Save benagricola/7a9382db2518947c2cd2e2cc84c0d95c to your computer and use it in GitHub Desktop.
Save benagricola/7a9382db2518947c2cd2e2cc84c0d95c to your computer and use it in GitHub Desktop.
Broken aws-cdk Cloudfront Distribution when referencing an S3 bucket from another stack
➜ cdk ✗ BROKEN=awwyiss cdk synth --app "npx ts-node test-stack.ts"
cdk/node_modules/@aws-cdk/core/lib/stack.ts:691
throw new Error(`'${target.node.path}' depends on '${this.node.path}' (${cycle.join(', ')}). Adding this dependency (${reason}) would create a cyclic reference.`);
^
Error: 'buckets' depends on 'distribution' (buckets -> distribution/web-distribution/Origin1/S3Origin/Resource.S3CanonicalUserId). Adding this dependency (distribution -> buckets/bucket/Resource.RegionalDomainName) would create a cyclic reference.
at DistributionStack._addAssemblyDependency (cdk/node_modules/@aws-cdk/core/lib/stack.ts:691:13)
at Object.addDependency (cdk/node_modules/@aws-cdk/core/lib/deps.ts:52:20)
at DistributionStack.addDependency (cdk/node_modules/@aws-cdk/core/lib/stack.ts:448:5)
at resolveValue (cdk/node_modules/@aws-cdk/core/lib/private/refs.ts:102:12)
at Object.resolveReferences (cdk/node_modules/@aws-cdk/core/lib/private/refs.ts:32:24)
at Object.prepareApp (cdk/node_modules/@aws-cdk/core/lib/private/prepare-app.ts:36:5)
at Object.synthesize (cdk/node_modules/@aws-cdk/core/lib/private/synthesis.ts:21:3)
at App.synth (cdk/node_modules/@aws-cdk/core/lib/stage.ts:175:23)
at process.<anonymous> (cdk/node_modules/@aws-cdk/core/lib/app.ts:112:45)
at Object.onceWrapper (events.js:300:26)
Subprocess exited with error 1
➜ cdk ✗ cdk synth --app "npx ts-node test-stack.ts"
Successfully synthesized to cdk/cdk.out
Supply a stack id (buckets, distribution) to display its template.
import cdk = require('@aws-cdk/core')
import s3 = require('@aws-cdk/aws-s3')
import cloudfront = require('@aws-cdk/aws-cloudfront')
import origins = require('@aws-cdk/aws-cloudfront-origins')
const app = new cdk.App();
// External bucket stack
class BucketStack extends cdk.Stack {
bucket: s3.IBucket
constructor(scope: cdk.Construct, id: string, props: cdk.StackProps) {
super(scope, id, props);
this.bucket = new s3.Bucket(this, 'bucket')
}
}
interface DistributionStackProps extends cdk.StackProps {
bucket?: s3.IBucket
}
// Distribution Stack
class DistributionStack extends cdk.Stack {
bucket: s3.IBucket
constructor(scope: cdk.Construct, id: string, props: DistributionStackProps) {
super(scope, id, props);
if(!props.bucket) {
this.bucket = new s3.Bucket(this, 'bucket')
} else {
this.bucket = props.bucket
}
const origin = new origins.S3Origin(this.bucket, {})
new cloudfront.Distribution(this, 'web-distribution', {
defaultBehavior: {
origin: origin,
}
});
}
}
if(process.env.BROKEN) {
// Create the bucket in a different stack - Does not work
const bucketStack = new BucketStack(app, 'buckets', {});
// Error: 'buckets' depends on 'distribution' (buckets -> distribution/web-distribution/Origin1/S3Origin/Resource.S3CanonicalUserId). Adding this dependency (distribution -> buckets/bucket/Resource.RegionalDomainName) would create a cyclic reference.
new DistributionStack(app, 'distribution', { bucket: bucketStack.bucket })
} else {
// Create the bucket in the same stack - Works
new DistributionStack(app, 'distribution', {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment