Skip to content
Merged
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 src/supabase-cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class SupabaseCdn extends Construct {
webAclArn: cdk.CfnParameter;
};

/** Construct for CloudFront and WAF */
constructor(scope: Construct, id: string, props: SupabaseCdnProps) {
super(scope, id);

Expand Down
9 changes: 7 additions & 2 deletions src/supabase-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class SupabaseDatabase extends Construct {
instanceCount: cdk.CfnParameter;
};

/** PosthreSQL cluster with aurora-postgresql engine */
constructor(scope: Construct, id: string, props: SupabaseDatabaseProps) {
super(scope, id);

Expand Down Expand Up @@ -107,6 +108,7 @@ export class SupabaseDatabase extends Construct {
maxCapacity: this.cfnParameters.maxCapacity.valueAsNumber,
};

// Replace instance class in the DB cluster
const updateDBInstance = (index: number, parentCondition?: cdk.CfnCondition) => {
const expression = (typeof parentCondition == 'undefined')
? cdk.Fn.conditionEquals(this.cfnParameters.instanceCount, index)
Expand All @@ -123,7 +125,7 @@ export class SupabaseDatabase extends Construct {

this.secret = this.cluster.secret!;

// Sync to SSM Parameter Store for database_url ---------------------------------------
// Sync to SSM Parameter Store for database_url
const username = this.secret.secretValueFromJson('username').toString();
const password = this.secret.secretValueFromJson('password').toString();
const dbname = this.secret.secretValueFromJson('dbname').toString();
Expand Down Expand Up @@ -192,8 +194,8 @@ export class SupabaseDatabase extends Construct {
},
targets: [new targets.LambdaFunction(syncSecretFunction)],
});
// Sync to SSM Parameter Store for database_url ---------------------------------------

// Password rotation
const rotationSecurityGroup = new ec2.SecurityGroup(this, 'RotationSecurityGroup', { vpc });
this.secret.addRotationSchedule('Rotation', {
automaticallyAfter: cdk.Duration.days(30),
Expand All @@ -206,6 +208,7 @@ export class SupabaseDatabase extends Construct {
});
this.cluster.connections.allowDefaultPortFrom(rotationSecurityGroup, 'Lambda to rotate secrets');

/** Custom resource function for database initialization */
const initFunction = new NodejsFunction(this, 'InitFunction', {
description: 'Supabase - Database init function',
entry: './src/functions/db-init/index.ts',
Expand Down Expand Up @@ -233,8 +236,10 @@ export class SupabaseDatabase extends Construct {
this.cluster.connections.allowDefaultPortFrom(initFunction);
this.secret.grantRead(initFunction);

/** Custom resource provider for database initialization */
const initProvider = new cr.Provider(this, 'InitProvider', { onEventHandler: initFunction });

/** Modify schema and initial data */
const init = new cdk.CustomResource(this, 'InitData', {
serviceToken: initProvider.serviceToken,
resourceType: 'Custom::SupabaseInitData',
Expand Down
3 changes: 3 additions & 0 deletions src/supabase-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { SupabaseCdn } from './supabase-cdn';
import { SupabaseDatabase } from './supabase-db';

export class FargateStack extends cdk.Stack {
/** ECS Fargate task size mappings */
readonly taskSizeMapping: cdk.CfnMapping;

constructor(scope: Construct, id: string, props: cdk.StackProps = {}) {
Expand All @@ -36,6 +37,8 @@ export class FargateStack extends cdk.Stack {
}

export class SupabaseStack extends FargateStack {

/** Supabase Construct */
constructor(scope: Construct, id: string, props: cdk.StackProps = {}) {
super(scope, id, props);

Expand Down