@@ -4,31 +4,37 @@ import { BuildSpec } from 'aws-cdk-lib/aws-codebuild';
44import * as codecommit from 'aws-cdk-lib/aws-codecommit' ;
55import * as iam from 'aws-cdk-lib/aws-iam' ;
66import * as lambda from 'aws-cdk-lib/aws-lambda' ;
7+ import { ISecret } from 'aws-cdk-lib/aws-secretsmanager' ;
8+ import { StringParameter } from 'aws-cdk-lib/aws-ssm' ;
79import * as cr from 'aws-cdk-lib/custom-resources' ;
810import { Construct } from 'constructs' ;
911
10- interface AmplifyHostingProps {
11- sourceRepo : string ;
12- sourceBranch : string ;
13- appRoot : string ;
14- environment ?: {
15- [ name : string ] : string ;
16- } ;
12+ interface SupabaseStudioProps {
13+ sourceBranch ? : string ;
14+ appRoot ? : string ;
15+ supabaseUrl : string ;
16+ dbSecret : ISecret ;
17+ anonKey : StringParameter ;
18+ serviceRoleKey : StringParameter ;
1719}
1820
19- export class AmplifyHosting extends Construct {
21+ export class SupabaseStudio extends Construct {
2022 /** App in Amplify Hosting. It is a collection of branches. */
2123 readonly app : amplify . App ;
2224 /** Production branch */
2325 readonly prodBranch : amplify . Branch ;
2426 /** URL of production branch */
2527 readonly prodBranchUrl : string ;
2628
27- /** Next.js App Hosting */
28- constructor ( scope : Construct , id : string , props : AmplifyHostingProps ) {
29+ /** Next.js app on Amplify Hosting */
30+ constructor ( scope : Construct , id : string , props : SupabaseStudioProps ) {
2931 super ( scope , id ) ;
3032
31- const { sourceRepo, sourceBranch, appRoot, environment = { } } = props ;
33+ const buildImage = 'public.ecr.aws/sam/build-nodejs18.x:latest' ;
34+ const sourceRepo = 'https://github.com/supabase/supabase.git' ;
35+ const sourceBranch = props . sourceBranch ?? 'master' ;
36+ const appRoot = props . appRoot ?? 'studio' ;
37+ const { supabaseUrl, dbSecret, anonKey, serviceRoleKey } = props ;
3238
3339 /** CodeCommit - Source Repository for Amplify Hosting */
3440 const repository = new Repository ( this , 'Repository' , {
@@ -46,8 +52,10 @@ export class AmplifyHosting extends Construct {
4652 assumedBy : new iam . ServicePrincipal ( 'amplify.amazonaws.com' ) ,
4753 } ) ;
4854
49- /** Keys of environment variables */
50- const envKeys = Object . keys ( environment ) ;
55+ // Allow the role to access Secret and Parameter
56+ dbSecret . grantRead ( role ) ;
57+ anonKey . grantRead ( role ) ;
58+ serviceRoleKey . grantRead ( role ) ;
5159
5260 /** BuildSpec for Amplify Hosting */
5361 const buildSpec = BuildSpec . fromObjectToYaml ( {
@@ -58,11 +66,14 @@ export class AmplifyHosting extends Construct {
5866 phases : {
5967 preBuild : {
6068 commands : [
61- `env | grep ${ envKeys . map ( key => `-e ${ key } ` ) . join ( ' ' ) } >> .env.production` ,
69+ 'echo POSTGRES_PASSWORD=$(aws secretsmanager get-secret-value --secret-id $DB_SECRET_ARN --query SecretString | jq -r . | jq -r .password) >> .env.production' ,
70+ 'echo SUPABASE_ANON_KEY=$(aws ssm get-parameter --region $SSM_REGION --name $ANON_KEY_NAME --query Parameter.Value) >> .env.production' ,
71+ 'echo SUPABASE_SERVICE_KEY=$(aws ssm get-parameter --region $SSM_REGION --name $SERVICE_KEY_NAME --query Parameter.Value) >> .env.production' ,
72+ 'env | grep -e STUDIO_PG_META_URL >> .env.production' ,
73+ 'env | grep -e SUPABASE_ >> .env.production' ,
6274 'env | grep -e NEXT_PUBLIC_ >> .env.production' ,
63- 'yum install -y rsync' ,
6475 'cd ../' ,
65- 'npx turbo@1.7.0 prune --scope=studio' ,
76+ 'npx turbo@1.10.3 prune --scope=studio' ,
6677 'npm clean-install' ,
6778 ] ,
6879 } ,
@@ -104,19 +115,27 @@ export class AmplifyHosting extends Construct {
104115 sourceCodeProvider : new amplify . CodeCommitSourceCodeProvider ( { repository } ) ,
105116 buildSpec,
106117 environmentVariables : {
107- ... environment ,
118+ // for Amplify Hosting Build
108119 NODE_OPTIONS : '--max-old-space-size=4096' ,
109120 AMPLIFY_MONOREPO_APP_ROOT : appRoot ,
110121 AMPLIFY_DIFF_DEPLOY : 'false' ,
122+ _CUSTOM_IMAGE : buildImage ,
123+ // for Supabase
124+ STUDIO_PG_META_URL : `${ supabaseUrl } /pg` ,
125+ SUPABASE_URL : `${ supabaseUrl } ` ,
126+ SUPABASE_PUBLIC_URL : `${ supabaseUrl } ` ,
127+ DB_SECRET_ARN : dbSecret . secretArn ,
128+ SSM_REGION : anonKey . env . region ,
129+ ANON_KEY_NAME : anonKey . parameterName ,
130+ SERVICE_KEY_NAME : serviceRoleKey . parameterName ,
111131 } ,
132+ customRules : [
133+ { source : '/<*>' , target : '/index.html' , status : amplify . RedirectStatus . NOT_FOUND_REWRITE } ,
134+ ] ,
112135 } ) ;
113- ( this . app . node . defaultChild as cdk . CfnResource ) . addPropertyOverride ( 'Platform' , 'WEB_COMPUTE' ) ;
114136
115- this . app . addEnvironment ( 'NODE_OPTIONS' , '--max-old-space-size=4096' ) ;
116- this . app . addEnvironment ( 'AMPLIFY_MONOREPO_APP_ROOT' , appRoot ) ;
117- this . app . addEnvironment ( 'AMPLIFY_DIFF_DEPLOY' , 'false' ) ;
118-
119- this . app . addCustomRule ( { source : '/<*>' , target : '/index.html' , status : amplify . RedirectStatus . NOT_FOUND_REWRITE } ) ;
137+ /** SSR v2 */
138+ ( this . app . node . defaultChild as cdk . CfnResource ) . addPropertyOverride ( 'Platform' , 'WEB_COMPUTE' ) ;
120139
121140 this . prodBranch = this . app . addBranch ( 'ProdBranch' , {
122141 branchName : 'main' ,
0 commit comments