Skip to content

Commit 97c37f7

Browse files
authored
Merge pull request #54 from supabase-community/refactor-cfn-parameter
feat: fix static credentials for Studio
2 parents db7c8dd + ad14dc8 commit 97c37f7

File tree

3 files changed

+205
-73
lines changed

3 files changed

+205
-73
lines changed

src/supabase-stack.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import * as s3 from 'aws-cdk-lib/aws-s3';
99
import { Secret } from 'aws-cdk-lib/aws-secretsmanager';
1010
import { Construct } from 'constructs';
1111
import { SesSmtp } from './amazon-ses-smtp';
12-
import { AmplifyHosting } from './aws-amplify-hosting';
1312
import { PrefixList } from './aws-prefix-list';
1413
import { ForceDeployJob } from './ecs-force-deploy-job';
1514
import { AutoScalingFargateService } from './ecs-patterns';
1615
import { JwtSecret } from './json-web-token';
1716
import { SupabaseCdn } from './supabase-cdn';
1817
import { SupabaseDatabase } from './supabase-db';
18+
import { SupabaseStudio } from './supabase-studio';
1919

2020
export class FargateStack extends cdk.Stack {
2121
/** ECS Fargate task size mappings */
@@ -232,6 +232,8 @@ export class SupabaseStack extends FargateStack {
232232
const supabaseStorageAdminSecret = db.genUserPassword('supabase_storage_admin');
233233
/** Secret of authenticator user */
234234
const authenticatorSecret = db.genUserPassword('authenticator');
235+
/** Secret of dashboard user */
236+
const dashboardUserSecret = db.genUserPassword('dashboard_user');
235237
/** Secret of postgres user */
236238
const postgresSecret = db.genUserPassword('postgres');
237239

@@ -638,20 +640,12 @@ export class SupabaseStack extends FargateStack {
638640
});
639641

640642
/** Supabase Studio */
641-
const studio = new AmplifyHosting(this, 'Studio', {
642-
sourceRepo: 'https://github.com/supabase/supabase.git',
643+
const studio = new SupabaseStudio(this, 'Studio', {
643644
sourceBranch: studioBranch.valueAsString,
644-
appRoot: 'studio',
645-
environment: {
646-
STUDIO_PG_META_URL: `${apiExternalUrl}/pg`,
647-
POSTGRES_PASSWORD: supabaseAdminSecret.secretValueFromJson('password').toString(),
648-
//DEFAULT_ORGANIZATION: 'Default Organization',
649-
//DEFAULT_PROJECT: 'Default Project',
650-
SUPABASE_URL: `${apiExternalUrl}`,
651-
SUPABASE_PUBLIC_URL: `${apiExternalUrl}`,
652-
SUPABASE_ANON_KEY: anonKey.value,
653-
SUPABASE_SERVICE_KEY: serviceRoleKey.value,
654-
},
645+
supabaseUrl: apiExternalUrl,
646+
dbSecret: dashboardUserSecret,
647+
anonKey: anonKey.ssmParameter,
648+
serviceRoleKey: serviceRoleKey.ssmParameter,
655649
});
656650

657651
new cdk.CfnOutput(this, 'SupabaseUrl', {

src/aws-amplify-hosting.ts renamed to src/supabase-studio.ts

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,37 @@ import { BuildSpec } from 'aws-cdk-lib/aws-codebuild';
44
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
55
import * as iam from 'aws-cdk-lib/aws-iam';
66
import * 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';
79
import * as cr from 'aws-cdk-lib/custom-resources';
810
import { 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

Comments
 (0)