Skip to content

Commit

Permalink
Adapted to allow VPCs that do not have isolated or private subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpatto committed Sep 7, 2023
1 parent b919565 commit a49f8af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
38 changes: 27 additions & 11 deletions packages/client/infrastructure-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArnComponents, aws_route53 as route53, Stack } from "aws-cdk-lib";
import { StringParameter } from "aws-cdk-lib/aws-ssm";
import { IVpc, SecurityGroup, Vpc } from "aws-cdk-lib/aws-ec2";
import { IVpc, SecurityGroup, Vpc, VpcAttributes } from "aws-cdk-lib/aws-ec2";
import {
HttpNamespace,
IHttpNamespace,
Expand Down Expand Up @@ -30,6 +30,10 @@ export interface DnsResult {
readonly certificate: ICertificate;
}

type Mutable<T> = {
-readonly [k in keyof T]: T[k];
};

export class InfrastructureClient {
constructor(protected infrastructureStackId: string) {}

Expand Down Expand Up @@ -74,7 +78,7 @@ export class InfrastructureClient {
return StringParameter.valueFromLookup(scope, parameterName).split(",");
};

return Vpc.fromVpcAttributes(scope, "VPC", {
const vpcAttrs: Mutable<VpcAttributes> = {
vpcId: StringParameter.valueFromLookup(
scope,
vpcIdParameterName(this.infrastructureStackId)
Expand All @@ -88,19 +92,31 @@ export class InfrastructureClient {
publicSubnetRouteTableIds: getStringListLookup(
vpcPublicSubnetRouteTableIdsParameterName(this.infrastructureStackId)
),
privateSubnetIds: getStringListLookup(
};

// try to bring in this private subnets if present
try {
vpcAttrs.privateSubnetIds = getStringListLookup(
vpcPrivateSubnetIdsParameterName(this.infrastructureStackId)
),
privateSubnetRouteTableIds: getStringListLookup(
);
vpcAttrs.privateSubnetRouteTableIds = getStringListLookup(
vpcPrivateSubnetRouteTableIdsParameterName(this.infrastructureStackId)
),
isolatedSubnetIds: getStringListLookup(
);
} catch (e) {}

// try to bring in the isolated subnets if present
try {
vpcAttrs.isolatedSubnetIds = getStringListLookup(
vpcIsolatedSubnetIdsParameterName(this.infrastructureStackId)
),
isolatedSubnetRouteTableIds: getStringListLookup(
);

vpcAttrs.isolatedSubnetRouteTableIds = getStringListLookup(
vpcIsolatedSubnetRouteTableIdsParameterName(this.infrastructureStackId)
),
});
);
} catch (e) {}

// actually make the VPC object
return Vpc.fromVpcAttributes(scope, "VPC", vpcAttrs);
}

/**
Expand Down
42 changes: 24 additions & 18 deletions packages/stack/infrastructure-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,33 @@ export class InfrastructureStack extends Stack {
stringListValue: vpc.publicSubnets.map((a) => a.routeTable.routeTableId),
});

new StringListParameter(this, "PrivateSubnetIdsParameter", {
parameterName: vpcPrivateSubnetIdsParameterName(id),
stringListValue: vpc.privateSubnets.map((a) => a.subnetId),
});
if (vpc.privateSubnets && vpc.privateSubnets.length > 0) {
new StringListParameter(this, "PrivateSubnetIdsParameter", {
parameterName: vpcPrivateSubnetIdsParameterName(id),
stringListValue: vpc.privateSubnets.map((a) => a.subnetId),
});

new StringListParameter(this, "PrivateSubnetRouteTableIdsParameter", {
parameterName: vpcPrivateSubnetRouteTableIdsParameterName(id),
stringListValue: vpc.privateSubnets.map((a) => a.routeTable.routeTableId),
});
new StringListParameter(this, "PrivateSubnetRouteTableIdsParameter", {
parameterName: vpcPrivateSubnetRouteTableIdsParameterName(id),
stringListValue: vpc.privateSubnets.map(
(a) => a.routeTable.routeTableId
),
});
}

new StringListParameter(this, "IsolatedSubnetIdsParameter", {
parameterName: vpcIsolatedSubnetIdsParameterName(id),
stringListValue: vpc.isolatedSubnets.map((a) => a.subnetId),
});
if (vpc.isolatedSubnets && vpc.isolatedSubnets.length > 0) {
new StringListParameter(this, "IsolatedSubnetIdsParameter", {
parameterName: vpcIsolatedSubnetIdsParameterName(id),
stringListValue: vpc.isolatedSubnets.map((a) => a.subnetId),
});

new StringListParameter(this, "IsolatedSubnetRouteTableIdsParameter", {
parameterName: vpcIsolatedSubnetRouteTableIdsParameterName(id),
stringListValue: vpc.isolatedSubnets.map(
(a) => a.routeTable.routeTableId
),
});
new StringListParameter(this, "IsolatedSubnetRouteTableIdsParameter", {
parameterName: vpcIsolatedSubnetRouteTableIdsParameterName(id),
stringListValue: vpc.isolatedSubnets.map(
(a) => a.routeTable.routeTableId
),
});
}

{
const sg = new SecurityGroup(this, "SecurityGroup", {
Expand Down

0 comments on commit a49f8af

Please sign in to comment.