-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): automatic cross-stack refs for CFN resources (#1510)
The "toCloudFormation" method of generated CFN resources invoke "resolve" so that any lazy tokens are evaluated. This escaped the global settings set by `findTokens` which collect tokens so they can be reported as references by `StackElement.prepare`. To solve this, findTokens now accepts a function instead of an object and basically just wraps it's invocation with settings that will collect all tokens resolved within that scope. Not an ideal solution but simple enough. This was not discovered because we didn't have any tests that validated the behavior of the generated CFN resources (they are not accessible from the @aws-cdk/cdk library). We add a unit test in the IAM library to cover this use case.
- Loading branch information
Elad Ben-Israel
authored
Jan 10, 2019
1 parent
b909dcd
commit ca5ee35
Showing
3 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/@aws-cdk/aws-iam/test/test.auto-cross-stack-refs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { expect } from '@aws-cdk/assert'; | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { Test } from 'nodeunit'; | ||
import iam = require('../lib'); | ||
|
||
export = { | ||
'automatic exports are created when attributes are referneced across stacks'(test: Test) { | ||
// GIVEN | ||
const stackWithUser = new cdk.Stack(); | ||
const stackWithGroup = new cdk.Stack(); | ||
const user = new iam.User(stackWithUser, 'User'); | ||
const group = new iam.Group(stackWithGroup, 'Group'); | ||
|
||
// WHEN | ||
group.addUser(user); | ||
|
||
// | ||
// `group.addUser` adds the group to the user resource definition, so we expect | ||
// that an automatic export will be created for the group and the user's stack | ||
// to use ImportValue to import it. | ||
// note that order of "expect"s matters. we first need to synthesize the user's | ||
// stack so that the cross stack reference will be reported and only then the | ||
// group's stack. in the real world, App will take care of this. | ||
// | ||
|
||
// THEN | ||
expect(stackWithUser).toMatch({ | ||
Resources: { | ||
User00B015A1: { | ||
Type: "AWS::IAM::User", | ||
Properties: { | ||
Groups: [ { "Fn::ImportValue": "ExportsOutputRefGroupC77FDACD8CF7DD5B" } ] | ||
} | ||
} | ||
} | ||
}); | ||
expect(stackWithGroup).toMatch({ | ||
Outputs: { | ||
ExportsOutputRefGroupC77FDACD8CF7DD5B: { | ||
Value: { Ref: "GroupC77FDACD" }, | ||
Export: { Name: "ExportsOutputRefGroupC77FDACD8CF7DD5B" } | ||
} | ||
}, | ||
Resources: { | ||
GroupC77FDACD: { | ||
Type: "AWS::IAM::Group" | ||
} | ||
} | ||
}); | ||
test.done(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters