Skip to content

Commit

Permalink
feat(core): verify CfnOutput has a value and fix VPC export
Browse files Browse the repository at this point in the history
Throw an error if `value` is undefined when defining a `CfnOutput`.

Fixes #2012 such that a VPC can be exported without a VPN gateway (there
is already unit test coverage).
  • Loading branch information
Elad Ben-Israel committed Apr 10, 2019
1 parent 22ed67c commit d250c0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,13 @@ export class VpcNetwork extends VpcNetworkBase {
const priv = new ExportSubnetGroup(this, 'PrivateSubnetIDs', this.privateSubnets, SubnetType.Private, this.availabilityZones.length);
const iso = new ExportSubnetGroup(this, 'IsolatedSubnetIDs', this.isolatedSubnets, SubnetType.Isolated, this.availabilityZones.length);

const vpnGatewayId = this.vpnGatewayId
? new cdk.CfnOutput(this, 'VpnGatewayId', { value: this.vpnGatewayId }).makeImportValue().toString()
: undefined;

return {
vpcId: new cdk.CfnOutput(this, 'VpcId', { value: this.vpcId }).makeImportValue().toString(),
vpnGatewayId: new cdk.CfnOutput(this, 'VpnGatewayId', { value: this.vpnGatewayId }).makeImportValue().toString(),
vpnGatewayId,
availabilityZones: this.availabilityZones,
publicSubnetIds: pub.ids,
publicSubnetNames: pub.names,
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/cdk/lib/cfn-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export class CfnOutput extends CfnElement {
constructor(scope: Construct, id: string, props: CfnOutputProps = {}) {
super(scope, id);

if (props.value === undefined) {
throw new Error(`Missing value for CloudFormation output at path "${this.node.path}"`);
}

this.description = props.description;
this._value = props.value;
this.condition = props.condition;
Expand Down

0 comments on commit d250c0b

Please sign in to comment.