Skip to content

Commit 004077f

Browse files
rix0rrrmergify[bot]
authored andcommitted
fix(ec2): fix error when using Tokens in Vpc.fromLookup() (#3740)
`fromLookup()` will not support lazy values. Throw a clear error message when that is done, and update the documentation to make it clear what should be done instead. Fixes #3600.
1 parent c39d659 commit 004077f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/@aws-cdk/aws-ec2/lib/vpc.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,22 @@ export class Vpc extends VpcBase {
698698

699699
/**
700700
* Import an existing VPC from by querying the AWS environment this stack is deployed to.
701+
*
702+
* Calling this method will lead to a lookup when the CDK CLI is executed.
703+
* You can therefore not use any values that will only be available at
704+
* CloudFormation execution time (i.e., Tokens).
705+
*
706+
* If you are looking to share a VPC between stacks, you can pass the `Vpc`
707+
* object between stacks and use it as normal.
701708
*/
702709
public static fromLookup(scope: Construct, id: string, options: VpcLookupOptions): IVpc {
710+
if (Token.isUnresolved(options.vpcId)
711+
|| Token.isUnresolved(options.vpcName)
712+
|| Object.values(options.tags || {}).some(Token.isUnresolved)
713+
|| Object.keys(options.tags || {}).some(Token.isUnresolved)) {
714+
throw new Error(`All arguments to Vpc.fromLookup() must be concrete (no Tokens)`);
715+
}
716+
703717
const filter: {[key: string]: string} = makeTagFilter(options.tags);
704718

705719
// We give special treatment to some tags

packages/@aws-cdk/aws-ec2/test/test.vpc.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,20 @@ export = {
728728
test.done();
729729
}
730730
},
731+
732+
'fromLookup() requires concrete values'(test: Test) {
733+
// GIVEN
734+
const stack = new Stack();
735+
736+
test.throws(() => {
737+
Vpc.fromLookup(stack, 'Vpc', {
738+
vpcId: Lazy.stringValue({ produce: () => 'some-id' })
739+
});
740+
741+
}, 'All arguments to Vpc.fromLookup() must be concrete');
742+
743+
test.done();
744+
},
731745
};
732746

733747
function getTestStack(): Stack {

0 commit comments

Comments
 (0)