From 8ea69e01d56a70e52795a0e8e7926ed6b93f055e Mon Sep 17 00:00:00 2001 From: Kazuki Matsuda Date: Sun, 6 Nov 2022 02:47:29 +0000 Subject: [PATCH] refactoring --- src/supabase-db.ts | 39 ++++++++++++++-- src/supabase-stack.ts | 38 +++------------ test/__snapshots__/main.test.ts.snap | 70 ++++++++++++++-------------- 3 files changed, 77 insertions(+), 70 deletions(-) diff --git a/src/supabase-db.ts b/src/supabase-db.ts index 398877f..0a231f1 100644 --- a/src/supabase-db.ts +++ b/src/supabase-db.ts @@ -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 { @@ -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, @@ -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!; diff --git a/src/supabase-stack.ts b/src/supabase-stack.ts index 217aad0..4f30ddd 100644 --- a/src/supabase-stack.ts +++ b/src/supabase-stack.ts @@ -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', @@ -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 }); @@ -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' }); @@ -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, ], }, { @@ -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' }, diff --git a/test/__snapshots__/main.test.ts.snap b/test/__snapshots__/main.test.ts.snap index ab48268..e4e21fd 100644 --- a/test/__snapshots__/main.test.ts.snap +++ b/test/__snapshots__/main.test.ts.snap @@ -63,10 +63,10 @@ Object { "", ], }, - "DatabaseMultiAzEnabled": Object { + "DatabaseMultiAzEnabledFB5AD26C": Object { "Fn::Equals": Array [ Object { - "Ref": "DatabaseMultiAz", + "Ref": "DatabaseMultiAz04923704", }, "true", ], @@ -376,9 +376,9 @@ Object { "default": "Infrastructure - Database", }, "Parameters": Array [ - "DatabaseMultiAz", - "MinAuroraCapacityUnit", - "MaxAuroraCapacityUnit", + "DatabaseMultiAz04923704", + "DatabaseMinCapacity82563E98", + "DatabaseMaxCapacityDD9828CB", ], }, Object { @@ -430,6 +430,8 @@ Object { "Parameters": Array [ "RealtimeApiVersion", "RealtimeTaskSize", + "RealtimeMinTasks", + "RealtimeMaxTasks", ], }, Object { @@ -556,7 +558,13 @@ Object { "CdnWebAclArn5923748A": Object { "default": "Web ACL ARN (AWS WAF)", }, - "DatabaseMultiAz": Object { + "DatabaseMaxCapacityDD9828CB": Object { + "default": "Maximum ACUs", + }, + "DatabaseMinCapacity82563E98": Object { + "default": "Minimum ACUs", + }, + "DatabaseMultiAz04923704": Object { "default": "Database Multi-AZ", }, "DisableSignup": Object { @@ -586,12 +594,6 @@ Object { "KongTaskSize": Object { "default": "Fargate Task Size", }, - "MaxAuroraCapacityUnit": Object { - "default": "Maximum Aurora Capacity Units", - }, - "MinAuroraCapacityUnit": Object { - "default": "Minimum Aurora Capacity Units", - }, "PasswordMinLength": Object { "default": "Min password length", }, @@ -882,7 +884,21 @@ Object { "Description": "Web ACL for CloudFront.", "Type": "String", }, - "DatabaseMultiAz": Object { + "DatabaseMaxCapacityDD9828CB": Object { + "Default": 32, + "Description": "The maximum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.", + "MaxValue": 128, + "MinValue": 0.5, + "Type": "Number", + }, + "DatabaseMinCapacity82563E98": Object { + "Default": 0.5, + "Description": "The minimum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.", + "MaxValue": 128, + "MinValue": 0.5, + "Type": "Number", + }, + "DatabaseMultiAz04923704": Object { "AllowedValues": Array [ "true", "false", @@ -911,13 +927,13 @@ Object { }, "GqlMaxTasks": Object { "Default": 0, - "Description": "Maximum fargate task count for Rest API (PostgREST)", + "Description": "Maximum fargate task count for GraphQL API (PostGraphile)", "MinValue": 0, "Type": "Number", }, "GqlMinTasks": Object { "Default": 0, - "Description": "Minimum fargate task count for Rest API (PostgREST)", + "Description": "Minimum fargate task count for GraphQL API (PostGraphile)", "MinValue": 0, "Type": "Number", }, @@ -933,7 +949,7 @@ Object { "4xlarge", ], "Default": "medium", - "Description": "Fargare task size for Rest API (PostgREST)", + "Description": "Fargare task size for GraphQL API (PostGraphile)", "Type": "String", }, "JwtExpiryLimit": Object { @@ -970,20 +986,6 @@ Object { "Description": "Fargare task size for API Gateway (Kong)", "Type": "String", }, - "MaxAuroraCapacityUnit": Object { - "Default": 32, - "Description": "The maximum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.", - "MaxValue": 128, - "MinValue": 0.5, - "Type": "Number", - }, - "MinAuroraCapacityUnit": Object { - "Default": 0.5, - "Description": "The minimum number of Aurora capacity units (ACUs) for a DB instance in an Aurora Serverless v2 cluster.", - "MaxValue": 128, - "MinValue": 0.5, - "Type": "Number", - }, "PasswordMinLength": Object { "Default": "8", "Description": "When signup is disabled the only way to create new users is through invites. Defaults to false, all signups enabled.", @@ -1056,7 +1058,7 @@ Object { "4xlarge", ], "Default": "medium", - "Description": "Fargare task size for Realtime API)", + "Description": "Fargare task size for Realtime API", "Type": "String", }, "RedirectUrls": Object { @@ -3208,10 +3210,10 @@ Object { "Port": 5432, "ServerlessV2ScalingConfiguration": Object { "MaxCapacity": Object { - "Ref": "MaxAuroraCapacityUnit", + "Ref": "DatabaseMaxCapacityDD9828CB", }, "MinCapacity": Object { - "Ref": "MinAuroraCapacityUnit", + "Ref": "DatabaseMinCapacity82563E98", }, }, "StorageEncrypted": true, @@ -3251,7 +3253,7 @@ Object { "UpdateReplacePolicy": "Delete", }, "DatabaseClusterInstance291B00375": Object { - "Condition": "DatabaseMultiAzEnabled", + "Condition": "DatabaseMultiAzEnabledFB5AD26C", "DeletionPolicy": "Delete", "DependsOn": Array [ "VPCPrivateSubnet1DefaultRouteAE1D6490",