Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add function to remove the default VPC #94

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/aws/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type VpcConfig = {
export type SubnetConfigs = {
public: SubnetConfig;
dmz: SubnetConfig;
remove_default: boolean;
};

export type SubnetConfig = {
Expand Down
4 changes: 4 additions & 0 deletions lib/aws/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ export class AWSStack extends cdk.Stack {
elasticache.cluster.attrRedisEndpointAddress,
);
}

let _ = new DefaultRemoverStack(this, config);


let eks = new EksStack(
this,
config,
Expand Down
40 changes: 29 additions & 11 deletions lib/aws/subnet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import { Construct } from 'constructs';
import { Config } from './config';
import * as ec2 from "aws-cdk-lib/aws-ec2";
import { Construct } from "constructs";
import { Config } from "./config";
import { RemovalPolicy } from "aws-cdk-lib";

export class SubnetStack {
constructor(scope: Construct , vpc: ec2.Vpc, config: Config) {
config.extra_subnets.forEach((subnetConfig) => {
const subnet = new ec2.Subnet(scope, subnetConfig.id, {
vpcId: vpc.vpcId,
cidrBlock: subnetConfig.cidr,
availabilityZone: vpc.availabilityZones[0],
});
});
constructor(scope: Construct, vpc: ec2.Vpc, config: Config) {
config.extra_subnets.forEach((subnetConfig) => {
const subnet = new ec2.Subnet(scope, subnetConfig.id, {
vpcId: vpc.vpcId,
cidrBlock: subnetConfig.cidr,
availabilityZone: vpc.availabilityZones[0],
});
});
}
}

export class DefaultRemoverStack extends Construct {
constructor(scope: Construct, config: Config) {
super(scope, "DefaultRemovalStack");

/// Early return if the default subnet is not to be removed
if (!config.subnet.remove_default) {
return;
nitesh-balla marked this conversation as resolved.
Show resolved Hide resolved
}

const vpc = ec2.Vpc.fromLookup(this, "VPC", {
isDefault: true,
});

vpc.applyRemovalPolicy(RemovalPolicy.DESTROY);
}
}