Skip to content

Commit

Permalink
feat(core): can use Constructs to model applications (#1940)
Browse files Browse the repository at this point in the history
It is now possible for a Construct to contain Stacks. This allows
modeling your application as a single Construct, and instantiating
it multiple times for every environment.

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.
  • Loading branch information
rix0rrr authored Mar 20, 2019
1 parent de296de commit 32c2377
Show file tree
Hide file tree
Showing 42 changed files with 2,084 additions and 1,139 deletions.
2,783 changes: 1,804 additions & 979 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/@aws-cdk/assert/lib/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export class StackPathInspector extends Inspector {
}

public get value(): { [key: string]: any } | undefined {
const md = this.stack.metadata[`/${this.stack.name}${this.path}`];
// The names of paths in metadata in tests are very ill-defined. Try with the full path first,
// then try with the stack name preprended for backwards compat with most tests that happen to give
// their stack an ID that's the same as the stack name.
const md = this.stack.metadata[this.path] || this.stack.metadata[`/${this.stack.name}${this.path}`];
if (md === undefined) { return undefined; }
const resourceMd = md.find(entry => entry.type === 'aws:cdk:logicalId');
if (resourceMd === undefined) { return undefined; }
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/assert/test/test.assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ passingExample('expect <synthStack> at <some path> to have <some type>', () => {
});
expect(synthStack).at('/TestResource').to(haveType(resourceType));
});
passingExample('expect <synthStack> at <some path> to have <some type>', () => {
passingExample('expect non-synthesized stack at <some path> to have <some type>', () => {
const resourceType = 'Test::Resource';
const stack = new cdk.Stack();
new TestResource(stack, 'TestResource', { type: resourceType });

expect(stack).at('/TestResource').to(haveType(resourceType));
// '//' because the stack has no name, which leads to an empty path entry here.
expect(stack).at('//TestResource').to(haveType(resourceType));
});
passingExample('expect <synthStack> at <some path> *not* to have <some type>', () => {
const resourceType = 'Test::Resource';
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-apigateway/test/test.restapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ export = {
stack.node.prepareTree();
test.deepEqual(stack._toCloudFormation().Outputs.MyRestApiRestApiIdB93C5C2D, {
Value: { Ref: 'MyRestApi2D1F47A9' },
Export: { Name: 'MyRestApiRestApiIdB93C5C2D' }
Export: { Name: 'Stack:MyRestApiRestApiIdB93C5C2D' }
});
test.deepEqual(imported.node.resolve(imported.restApiId), 'api-rxt4498f');
test.deepEqual(imported.node.resolve(exported), { restApiId: { 'Fn::ImportValue': 'MyRestApiRestApiIdB93C5C2D' } });
test.deepEqual(imported.node.resolve(exported), { restApiId: { 'Fn::ImportValue': 'Stack:MyRestApiRestApiIdB93C5C2D' } });
test.done();
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@
{
"Ref": "AWS::Partition"
},
":cloudformation:us-west-2:12345678:stack/aws-cdk-codepipeline-cross-region-deploy-stack/*"
":cloudformation:us-west-2:",
{
"Ref": "AWS::AccountId"
},
":stack/aws-cdk-codepipeline-cross-region-deploy-stack/*"
]
]
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecr/test/test.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ export = {

// THEN
test.deepEqual(repo2.node.resolve(repo2.repositoryArn), {
'Fn::ImportValue': 'RepoRepositoryArn7F2901C9'
'Fn::ImportValue': 'Stack:RepoRepositoryArn7F2901C9'
});

test.deepEqual(repo2.node.resolve(repo2.repositoryName), {
'Fn::ImportValue': 'RepoRepositoryName58A7E467'
'Fn::ImportValue': 'Stack:RepoRepositoryName58A7E467'
});

test.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export = {
CidrIp: "0.0.0.0/0",
Description: "Open to the world",
IpProtocol: "tcp",
FromPort: { "Fn::ImportValue": "LBListenerPort7A9266A6" },
ToPort: { "Fn::ImportValue": "LBListenerPort7A9266A6" },
FromPort: { "Fn::ImportValue": "Stack:LBListenerPort7A9266A6" },
ToPort: { "Fn::ImportValue": "Stack:LBListenerPort7A9266A6" },
GroupId: IMPORTED_LB_SECURITY_GROUP
}));

Expand All @@ -198,7 +198,7 @@ export = {
};

const LB_SECURITY_GROUP = { "Fn::GetAtt": [ "LBSecurityGroup8A41EA2B", "GroupId" ] };
const IMPORTED_LB_SECURITY_GROUP = { "Fn::ImportValue": "LBSecurityGroupSecurityGroupId0270B565" };
const IMPORTED_LB_SECURITY_GROUP = { "Fn::ImportValue": "Stack:LBSecurityGroupSecurityGroupId0270B565" };

function expectSameStackSGRules(stack: cdk.Stack) {
expectSGRules(stack, LB_SECURITY_GROUP);
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-events/test/test.rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export = {
});

// THEN
test.deepEqual(stack.node.resolve(exportedRule), { eventRuleArn: { 'Fn::ImportValue': 'MyRuleRuleArnDB13ADB1' } });
test.deepEqual(stack.node.resolve(exportedRule), { eventRuleArn: { 'Fn::ImportValue': 'Stack:MyRuleRuleArnDB13ADB1' } });
test.deepEqual(importedRule.ruleArn, 'arn:of:rule');

test.done();
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/aws-glue/test/test.database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ export = {
]
},
Export: {
Name: "DatabaseCatalogArnE5C8063F"
Name: "Stack:DatabaseCatalogArnE5C8063F"
}
},
DatabaseCatalogId509C6547: {
Value: {
Ref: "AWS::AccountId"
},
Export: {
Name: "DatabaseCatalogId509C6547"
Name: "Stack:DatabaseCatalogId509C6547"
}
},
DatabaseDatabaseArn157B38E0: {
Expand Down Expand Up @@ -173,15 +173,15 @@ export = {
]
},
Export: {
Name: "DatabaseDatabaseArn157B38E0"
Name: "Stack:DatabaseDatabaseArn157B38E0"
}
},
DatabaseDatabaseName925B74A8: {
Value: {
Ref: "DatabaseB269D8BB"
},
Export: {
Name: "DatabaseDatabaseName925B74A8"
Name: "Stack:DatabaseDatabaseName925B74A8"
}
},
DatabaseLocationURIF74653AF: {
Expand All @@ -198,7 +198,7 @@ export = {
]
},
Export: {
Name: "DatabaseLocationURIF74653AF"
Name: "Stack:DatabaseLocationURIF74653AF"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-glue/test/test.table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export = {
Ref: "AWS::AccountId"
},
DatabaseName: {
"Fn::ImportValue": "ExportsOutputRefDatabaseB269D8BB88F4B1C4"
"Fn::ImportValue": "Stack:ExportsOutputRefDatabaseB269D8BB88F4B1C4"
},
TableInput: {
Name: "table",
Expand Down Expand Up @@ -107,7 +107,7 @@ export = {
Ref: "AWS::AccountId"
},
DatabaseName: {
"Fn::ImportValue": "ExportsOutputRefDatabaseB269D8BB88F4B1C4"
"Fn::ImportValue": "Stack:ExportsOutputRefDatabaseB269D8BB88F4B1C4"
},
TableInput: {
Name: "table",
Expand Down Expand Up @@ -1005,7 +1005,7 @@ export = {
Ref: "AWS::AccountId"
},
DatabaseName: {
"Fn::ImportValue": "ExportsOutputRefDatabaseB269D8BB88F4B1C4"
"Fn::ImportValue": "Stack:ExportsOutputRefDatabaseB269D8BB88F4B1C4"
},
TableInput: {
Description: "table generated by CDK",
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-iam/test/test.auto-cross-stack-refs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export = {
User00B015A1: {
Type: "AWS::IAM::User",
Properties: {
Groups: [ { "Fn::ImportValue": "ExportsOutputRefGroupC77FDACD8CF7DD5B" } ]
Groups: [ { "Fn::ImportValue": "Stack:ExportsOutputRefGroupC77FDACD8CF7DD5B" } ]
}
}
}
Expand All @@ -38,7 +38,7 @@ export = {
Outputs: {
ExportsOutputRefGroupC77FDACD8CF7DD5B: {
Value: { Ref: "GroupC77FDACD" },
Export: { Name: "ExportsOutputRefGroupC77FDACD8CF7DD5B" }
Export: { Name: "Stack:ExportsOutputRefGroupC77FDACD8CF7DD5B" }
}
},
Resources: {
Expand Down
10 changes: 5 additions & 5 deletions packages/@aws-cdk/aws-iam/test/test.role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,18 @@ export = {

// THEN
test.deepEqual(stack.node.resolve(exportedRole), {
roleArn: { 'Fn::ImportValue': 'MyRoleRoleArn3388B7E2' },
roleId: { 'Fn::ImportValue': 'MyRoleRoleIdF7B258D8' }
roleArn: { 'Fn::ImportValue': 'Stack:MyRoleRoleArn3388B7E2' },
roleId: { 'Fn::ImportValue': 'Stack:MyRoleRoleIdF7B258D8' }
});

test.deepEqual(stack.node.resolve(importedRole.roleArn), { 'Fn::ImportValue': 'MyRoleRoleArn3388B7E2' });
test.deepEqual(stack.node.resolve(importedRole.roleId), { 'Fn::ImportValue': 'MyRoleRoleIdF7B258D8' });
test.deepEqual(stack.node.resolve(importedRole.roleArn), { 'Fn::ImportValue': 'Stack:MyRoleRoleArn3388B7E2' });
test.deepEqual(stack.node.resolve(importedRole.roleId), { 'Fn::ImportValue': 'Stack:MyRoleRoleIdF7B258D8' });
test.deepEqual(stack.node.resolve(importedRole.roleName), {
'Fn::Select': [ 1, {
'Fn::Split': [ '/', {
'Fn::Select': [ 5, {
'Fn::Split': [ ':', {
'Fn::ImportValue': 'MyRoleRoleArn3388B7E2'
'Fn::ImportValue': 'Stack:MyRoleRoleArn3388B7E2'
} ]
} ]
} ]
Expand Down
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-kinesis/test/test.stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ export = {
]
},
"Export": {
"Name": "MyStreamStreamArn495BAFC1"
"Name": "Stack:MyStreamStreamArn495BAFC1"
}
}
}
Expand All @@ -853,7 +853,7 @@ export = {
],
"Effect": "Allow",
"Resource": {
"Fn::ImportValue": "MyStreamStreamArn495BAFC1"
"Fn::ImportValue": "Stack:MyStreamStreamArn495BAFC1"
}
}
],
Expand Down Expand Up @@ -960,7 +960,7 @@ export = {
]
},
"Export": {
"Name": "MyStreamKeyKeyArn967BCB03"
"Name": "Stack:MyStreamKeyKeyArn967BCB03"
}
},
"MyStreamStreamArn495BAFC1": {
Expand All @@ -971,7 +971,7 @@ export = {
]
},
"Export": {
"Name": "MyStreamStreamArn495BAFC1"
"Name": "Stack:MyStreamStreamArn495BAFC1"
}
}
}
Expand All @@ -995,14 +995,14 @@ export = {
],
"Effect": "Allow",
"Resource": {
"Fn::ImportValue": "MyStreamStreamArn495BAFC1"
"Fn::ImportValue": "Stack:MyStreamStreamArn495BAFC1"
}
},
{
"Action": "kms:Decrypt",
"Effect": "Allow",
"Resource": {
"Fn::ImportValue": "MyStreamKeyKeyArn967BCB03"
"Fn::ImportValue": "Stack:MyStreamKeyKeyArn967BCB03"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-kms/test/test.key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export = {
]
},
Export: {
Name: "MyKeyKeyArn317F1332"
Name: "Stack:MyKeyKeyArn317F1332"
}
}
}
Expand All @@ -374,7 +374,7 @@ export = {
Properties: {
AliasName: "alias/hello",
TargetKeyId: {
"Fn::ImportValue": "MyKeyKeyArn317F1332"
"Fn::ImportValue": "Stack:MyKeyKeyArn317F1332"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-lambda/test/test.vpc-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export = {

// THEN: SomeSecurityGroup accepts connections from Lambda
expect(stack2).to(haveResource("AWS::EC2::SecurityGroupEgress", {
GroupId: { "Fn::ImportValue": "LambdaSecurityGroupId9A2717B3" },
GroupId: { "Fn::ImportValue": "Stack:LambdaSecurityGroupId9A2717B3" },
IpProtocol: "tcp",
Description: "Lambda can call connectable",
DestinationSecurityGroupId: { "Fn::GetAtt": [ "SomeSecurityGroupEF219AD6", "GroupId" ] },
Expand All @@ -100,7 +100,7 @@ export = {
Description: "Lambda can call connectable",
FromPort: 0,
GroupId: { "Fn::GetAtt": [ "SomeSecurityGroupEF219AD6", "GroupId" ] },
SourceSecurityGroupId: { "Fn::ImportValue": "LambdaSecurityGroupId9A2717B3" },
SourceSecurityGroupId: { "Fn::ImportValue": "Stack:LambdaSecurityGroupId9A2717B3" },
ToPort: 65535
}));

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-rds/test/test.cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ export = {
const imported = ClusterParameterGroup.import(stack, 'ImportParams', exported);

// THEN
test.deepEqual(stack.node.resolve(exported), { parameterGroupName: { 'Fn::ImportValue': 'ParamsParameterGroupNameA6B808D7' } });
test.deepEqual(stack.node.resolve(imported.parameterGroupName), { 'Fn::ImportValue': 'ParamsParameterGroupNameA6B808D7' });
test.deepEqual(stack.node.resolve(exported), { parameterGroupName: { 'Fn::ImportValue': 'Stack:ParamsParameterGroupNameA6B808D7' } });
test.deepEqual(stack.node.resolve(imported.parameterGroupName), { 'Fn::ImportValue': 'Stack:ParamsParameterGroupNameA6B808D7' });
test.done();
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-route53/test/test.route53.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ export = {
Outputs: {
ZoneHostedZoneId413B8768: {
Value: { Ref: "ZoneA5DE4B68" },
Export: { Name: "ZoneHostedZoneId413B8768" }
Export: { Name: "Stack:ZoneHostedZoneId413B8768" }
}
}
}));

expect(stack2).to(haveResource("AWS::Route53::RecordSet", {
HostedZoneId: { "Fn::ImportValue": "ZoneHostedZoneId413B8768" },
HostedZoneId: { "Fn::ImportValue": "Stack:ZoneHostedZoneId413B8768" },
Name: "lookHere.cdk.local.",
ResourceRecords: [ "\"SeeThere\"" ],
Type: "TXT"
Expand Down
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-s3/test/test.bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,15 +960,15 @@ export = {
]
},
"Export": {
"Name": "MyBucketBucketArnE260558C"
"Name": "Stack:MyBucketBucketArnE260558C"
}
},
"MyBucketBucketName8A027014": {
"Value": {
"Ref": "MyBucketF68F3FF0"
},
"Export": {
"Name": "MyBucketBucketName8A027014"
"Name": "Stack:MyBucketBucketName8A027014"
}
},
"MyBucketDomainNameF76B9A7A": {
Expand All @@ -979,7 +979,7 @@ export = {
]
},
"Export": {
"Name": "MyBucketDomainNameF76B9A7A"
"Name": "Stack:MyBucketDomainNameF76B9A7A"
}
},
"MyBucketWebsiteURL9C222788": {
Expand All @@ -989,7 +989,7 @@ export = {
"WebsiteURL"
]
},
"Export": {"Name": "MyBucketWebsiteURL9C222788"}
"Export": {"Name": "Stack:MyBucketWebsiteURL9C222788"}
}
}
});
Expand All @@ -1013,14 +1013,14 @@ export = {
"Effect": "Allow",
"Resource": [
{
"Fn::ImportValue": "MyBucketBucketArnE260558C"
"Fn::ImportValue": "Stack:MyBucketBucketArnE260558C"
},
{
"Fn::Join": [
"",
[
{
"Fn::ImportValue": "MyBucketBucketArnE260558C"
"Fn::ImportValue": "Stack:MyBucketBucketArnE260558C"
},
"/*"
]
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ses/test/test.receipt-rule-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export = {
"Ref": "RuleSetE30C6C48"
},
"Export": {
"Name": "RuleSetReceiptRuleSetNameBA4266DD"
"Name": "Stack:RuleSetReceiptRuleSetNameBA4266DD"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ses/test/test.receipt-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export = {
"Ref": "RuleSetRule0B1D6BCA"
},
"Export": {
"Name": "RuleSetRuleReceiptRuleName5620D98F"
"Name": "Stack:RuleSetRuleReceiptRuleName5620D98F"
}
}
}
Expand Down
Loading

0 comments on commit 32c2377

Please sign in to comment.