Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): can use Constructs to model collections of Stacks #1940

Merged
merged 12 commits into from
Mar 20, 2019

Conversation

rix0rrr
Copy link
Contributor

@rix0rrr rix0rrr commented Mar 4, 2019

Stacks no longer accept a cdk.App as scope, but a cdk.Construct like
other constructs. This makes it possible define a Construct to
represent your entire application, which can consist of a collection of
Stacks.

Stack names will automatically be derived from their location in the
construct tree, but it's also possible to specify the deployed name of
the stack by specifying the stackName property.

ALSO IN THIS CHANGE

  • Calling app.run() is no longer necessary, but can still safely
    be done.
  • Update output message of awslint so that the linter rule suppression key
    can easily be copy pasted.

Fixes #1479. This PR replaces #1582.


Pull Request Checklist

  • Testing
    • Unit test added (prefer not to modify an existing test, otherwise, it's probably a breaking change)
    • CLI change?: coordinate update of integration tests with team
    • cdk-init template change?: coordinated update of integration tests with team
  • Docs
    • jsdocs: All public APIs documented
    • README: README and/or documentation topic updated
  • Title and Description
    • Change type: title prefixed with fix, feat will appear in changelog
    • Title: use lower-case and doesn't end with a period
    • Breaking?: last paragraph: "BREAKING CHANGE: <describe what changed + link for details>"
    • Issues: Indicate issues fixed via: "Fixes #xxx" or "Closes #xxx"
  • Sensitive Modules (requires 2 PR approvers)
    • IAM Policy Document (in @aws-cdk/aws-iam)
    • EC2 Security Groups and ACLs (in @aws-cdk/aws-ec2)
    • Grant APIs (only if not based on official documentation with a reference)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.

Stacks no longer accept a `cdk.App` as scope, but a `cdk.Construct` like
other constructs. This makes it possible define a Construct to
represent your entire application, which can consist of a collection of
Stacks.

Stack names will automatically be derived from their location in the
construct tree, but it's also possible to specify the deployed name of
the stack by specifying the `stackName` property.

Calling `app.run()` is no longer necessary, but can still safely
be done.

Fixes #1479.
@rix0rrr
Copy link
Contributor Author

rix0rrr commented Mar 4, 2019

cc @Doug-AWS, the user guide is going to need an update after this PR has been merged.

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor comments

packages/@aws-cdk/cdk/lib/app.ts Show resolved Hide resolved
packages/@aws-cdk/cdk/lib/app.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/cdk/lib/app.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/cdk/lib/cloudformation/stack.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/cdk/lib/cloudformation/stack.ts Outdated Show resolved Hide resolved
/**
* Return the path of components up to but excluding the root
*/
public rootPath(): IConstruct[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can just delete this. Is this really a useful thing? I also don't like that it's called path because it doesn't return a path (paths are strings), it returns an array of ancestors without the first one, which brings me to ask, what's rong with construct.node.ancestors().slice(1)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also make ancestors a property please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"root path" is the term for the nodes in a tree that lie between a given node and the root of the trie. I wouldn't say a path is a string, maybe that a path can be represented as a string.

rootPath could (and should) have more logic for the purposes of IDs, but honestly our tree structure and which things exist and have ids and whether those ids should be taken into account is completely messed up and inconsistent between real applications and unit tests, insofar as that anything that deals with names and paths is a tangle of messy ifs.

So sure, I'll inline the definition of rootPath() everywhere.

ancestors() takes an optional argument, so it cannot be made a property.

@@ -4,7 +4,7 @@ import { Construct, Resource, Stack, StackProps } from '../lib';
import { App } from '../lib/app';

function withApp(context: { [key: string]: any } | undefined, block: (app: App) => void): cxapi.SynthesizeResponse {
const app = new App(context);
const app = new App({ context, autoRun: false });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make autoRun: false the default when running outside the context of the toolkit (e.g. CDK_OUT is not set)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autoRun false is not needed now i guess

@Doug-AWS
Copy link
Contributor

Doug-AWS commented Mar 4, 2019

Are we also going to change the templates to use this new approach?

@rix0rrr rix0rrr self-assigned this Mar 15, 2019
@rix0rrr
Copy link
Contributor Author

rix0rrr commented Mar 19, 2019

Are we also going to change the templates to use this new approach?

This is only for applications that consist of multiple stacks, our templates are single-stack applications. So, no.

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yey! looking forward for this to be merged

*
* If you set this, you don't have to call `run()` anymore.
*
* @default true if running via CDK toolkit, false otherwise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be more specific about the mechanism: true if running via the toolkit (CDK_OUTDIR is set)

@@ -4,7 +4,7 @@ import { Construct, Resource, Stack, StackProps } from '../lib';
import { App } from '../lib/app';

function withApp(context: { [key: string]: any } | undefined, block: (app: App) => void): cxapi.SynthesizeResponse {
const app = new App(context);
const app = new App({ context, autoRun: false });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autoRun false is not needed now i guess

@@ -5,4 +5,3 @@ import { %name.PascalCased%Stack } from '../lib/%name%-stack';

const app = new cdk.App();
new %name.PascalCased%Stack(app, '%name.PascalCased%Stack');
app.run();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about other langs?

Copy link
Contributor

@eladb eladb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect some integ tests to fail (no idea which one, but ...)

@rix0rrr rix0rrr merged commit 32c2377 into master Mar 20, 2019
@rix0rrr rix0rrr deleted the huijbers/constructs-can-represent-apps branch March 20, 2019 09:42
@otterley
Copy link
Contributor

Do you have some examples of how one might use this new feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for multiple instances of a cdk.App
5 participants