Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mats16 committed Nov 6, 2022
1 parent 3ea73b5 commit 8ea69e0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 70 deletions.
39 changes: 34 additions & 5 deletions src/supabase-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ const excludeCharacters = '%+~`#$&*()|[]{}:;<>?!\'/@\"\\=^'; // for Password

interface SupabaseDatabaseProps {
vpc: ec2.IVpc;
multiAzEnabled: cdk.CfnCondition;
minCapacity: number;
maxCapacity: number;
}

export class SupabaseDatabase extends Construct {
Expand All @@ -27,11 +24,39 @@ export class SupabaseDatabase extends Construct {
writerSearchPathAuth: ssm.StringParameter;
reader: ssm.StringParameter;
};
minCapacity: cdk.CfnParameter;
maxCapacity: cdk.CfnParameter;
multiAz: cdk.CfnParameter;

constructor(scope: Construct, id: string, props: SupabaseDatabaseProps) {
super(scope, id);

const { vpc, multiAzEnabled, minCapacity, maxCapacity } = props;
const { vpc } = props;

this.multiAz = new cdk.CfnParameter(this, 'MultiAz', {
description: 'Create a replica at another Availability Zone',
type: 'String',
default: 'false',
allowedValues: ['true', 'false'],
});

this.minCapacity = new cdk.CfnParameter(this, 'MinCapacity', {
description: 'The minimum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.',
type: 'Number',
default: 0.5,
minValue: 0.5,
maxValue: 128,
});

this.maxCapacity = new cdk.CfnParameter(this, 'MaxCapacity', {
description: 'The maximum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.',
type: 'Number',
default: 32,
minValue: 0.5,
maxValue: 128,
});

const multiAzEnabled = new cdk.CfnCondition(this, 'MultiAzEnabled', { expression: cdk.Fn.conditionEquals(this.multiAz, 'true') });

const engine = rds.DatabaseClusterEngine.auroraPostgres({
version: rds.AuroraPostgresEngineVersion.VER_14_4,
Expand Down Expand Up @@ -69,7 +94,11 @@ export class SupabaseDatabase extends Construct {
defaultDatabaseName: 'postgres',
});

(this.cluster.node.defaultChild as rds.CfnDBCluster).serverlessV2ScalingConfiguration = { minCapacity, maxCapacity };
(this.cluster.node.defaultChild as rds.CfnDBCluster).serverlessV2ScalingConfiguration = {
minCapacity: this.minCapacity.valueAsNumber,
maxCapacity: this.maxCapacity.valueAsNumber,
};

(this.cluster.node.findChild('Instance2') as rds.CfnDBInstance).addOverride('Condition', multiAzEnabled.logicalId);

this.secret = this.cluster.secret!;
Expand Down
38 changes: 7 additions & 31 deletions src/supabase-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,29 +105,6 @@ export class SupabaseStack extends cdk.Stack {
allowedValues: ['true', 'false'],
});

const dbMultiAz = new cdk.CfnParameter(this, 'DatabaseMultiAz', {
description: 'Create a replica at another Availability Zone',
type: 'String',
default: 'false',
allowedValues: ['true', 'false'],
});

const minAcu = new cdk.CfnParameter(this, 'MinAuroraCapacityUnit', {
description: 'The minimum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.',
type: 'Number',
default: 0.5,
minValue: 0.5,
maxValue: 128,
});

const maxAcu = new cdk.CfnParameter(this, 'MaxAuroraCapacityUnit', {
description: 'The maximum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.',
type: 'Number',
default: 32,
minValue: 0.5,
maxValue: 128,
});

const kongTaskSize = new cdk.CfnParameter(this, 'KongTaskSize', {
description: 'Fargare task size for API Gateway (Kong)',
type: 'String',
Expand Down Expand Up @@ -295,7 +272,6 @@ export class SupabaseStack extends cdk.Stack {

// Condition
const workMailEnabled = new cdk.CfnCondition(this, 'WorkMailEnabled', { expression: cdk.Fn.conditionEquals(enableWorkMail, 'true') });
const dbMultiAzEnabled = new cdk.CfnCondition(this, 'DatabaseMultiAzEnabled', { expression: cdk.Fn.conditionEquals(dbMultiAz, 'true') });

// Resources
const vpc = new Vpc(this, 'VPC', { natGateways: 1 });
Expand Down Expand Up @@ -331,7 +307,7 @@ export class SupabaseStack extends cdk.Stack {
const smtpHost = cdk.Fn.conditionIf(workMailEnabled.logicalId, `smtp.mail.${sesRegion.valueAsString}.awsapps.com`, `email-smtp.${sesRegion.valueAsString}.amazonaws.com`);
const smtpUser = cdk.Fn.conditionIf(workMailEnabled.logicalId, workMailUser.getAtt('Email'), mail.secret.secretValueFromJson('username').unsafeUnwrap());

const db = new SupabaseDatabase(this, 'Database', { vpc, multiAzEnabled: dbMultiAzEnabled, minCapacity: minAcu.valueAsNumber, maxCapacity: maxAcu.valueAsNumber });
const db = new SupabaseDatabase(this, 'Database', { vpc });

const jwt = new SupabaseJwt(this, 'SupabaseJwt', { issuer: 'supabase', expiresIn: '10y' });

Expand Down Expand Up @@ -727,9 +703,9 @@ export class SupabaseStack extends cdk.Stack {
{
Label: { default: 'Infrastructure - Database' },
Parameters: [
dbMultiAz.logicalId,
minAcu.logicalId,
maxAcu.logicalId,
db.multiAz.logicalId,
db.minCapacity.logicalId,
db.maxCapacity.logicalId,
],
},
{
Expand Down Expand Up @@ -813,9 +789,9 @@ export class SupabaseStack extends cdk.Stack {
[enableWorkMail.logicalId]: { default: 'Enable Amazon WorkMail (Test E-mail Domain)' },
[cdn.webAclArn.logicalId]: { default: 'Web ACL ARN (AWS WAF)' },

[dbMultiAz.logicalId]: { default: 'Database Multi-AZ' },
[minAcu.logicalId]: { default: 'Minimum Aurora Capacity Units' },
[maxAcu.logicalId]: { default: 'Maximum Aurora Capacity Units' },
[db.multiAz.logicalId]: { default: 'Database Multi-AZ' },
[db.minCapacity.logicalId]: { default: 'Minimum ACUs' },
[db.maxCapacity.logicalId]: { default: 'Maximum ACUs' },

[kongTaskSize.logicalId]: { default: 'Fargate Task Size' },
[kongMinTasks.logicalId]: { default: 'Minimum Fargate Task Count' },
Expand Down
70 changes: 36 additions & 34 deletions test/__snapshots__/main.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8ea69e0

Please sign in to comment.