Skip to content

Instantly share code, notes, and snippets.

@rix0rrr
Created March 4, 2019 10:37
Show Gist options
  • Select an option

  • Save rix0rrr/c62ac7c5bbc2f6f16b695865bb76c30e to your computer and use it in GitHub Desktop.

Select an option

Save rix0rrr/c62ac7c5bbc2f6f16b695865bb76c30e to your computer and use it in GitHub Desktop.
apps apps apps
// Simple case, my "app" is just a stack
const app = new cdk.App();
new MyStack(app, 'MyFrobulizerConsumer');
//---------------------------------------------------------------------------
// More complex case, my "app" is a subclass of App because it contains multiple
// stacks and I can organize them
new MyApp('TweetFrobulizer');
//---------------------------------------------------------------------------
// Instantiating multiple apps or the same app multiple times
new MyApp('TweetFrobulizerEU', { ... });
new MyApp('TweetFrobulizerUS', { ... });
new OpTools('TweetFrobulizerTools');
// Simple case, my "app" is just a stack
// ==> This is the same
const app = new cdk.App();
new MyStack(app, 'MyFrobulizerConsumer');
//---------------------------------------------------------------------------
// More complex case
// I still need to instantiate a boilerplate thing that has nothing to
// do with *MY* code
const app = new cdk.App();
new MyApp(app, 'TweetFrobulizer');
//---------------------------------------------------------------------------
// Instantiating multiple apps or the same app multiple times
// Again, this boilerplate thing is here
const app = new cdk.App();
new MyApp(app, 'TweetFrobulizerEU', { ... });
new MyApp(app, 'TweetFrobulizerUS', { ... });
new OpTools(app, 'TweetFrobulizerTools');
// So the difference as I see it is in the simple case, we can retcon that
// the App you instantiate is actually *your* app, and we can hide the
// machinery of coming up with a proper (shared) Root instance inside that class.
//
// Whereas in the other scenario, you HAVE to instantiate that Root object,
// but it's obviously framework machinery sticking out.
//
// Now the downsides of making users subclass App is that:
// 1) The signature will be slightly different from construct, which will be
// confusing.
// 2) Their stuff isn't as obviously reusble as when it was a cdk.Construct.
//
// Mitigations for making the `App` class stick out like that:
// 1) Rename it? But to what?
// 2) Static singleton accessor? But named what? Probably shouldn't involve
// the word "root" anywhere or we're going to get back into tree building
// discussions that we don't want to have.
const host = new cdk.Host();
// or
const model = new cdk.AppModel();
// or
new MyStack(cdk.Host.root, 'MyStack');
new MyStack(cdk.App.appScope, 'MyStack');
@thomascharlie2500-cmyk
Copy link

“This comparison nicely highlights the practical difference between using App as your own application wrapper versus treating it as pure CDK boilerplate. The first approach feels cleaner for simple architectures, while the second exposes more framework machinery and can look heavier. Both patterns work it really depends on how reusable or structured you want your stacks and constructs to be.”

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment