Skip to content

Commit e7d4ee0

Browse files
committed
wip
1 parent ab89802 commit e7d4ee0

17 files changed

Lines changed: 297 additions & 88 deletions

File tree

cdk/lib/constructs/webapp.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IgnoreMode, Duration } from 'aws-cdk-lib';
1+
import { IgnoreMode, Duration, CfnOutput, Stack } from 'aws-cdk-lib';
22
import { Platform } from 'aws-cdk-lib/aws-ecr-assets';
33
import { DockerImageFunction, DockerImageCode, Architecture } from 'aws-cdk-lib/aws-lambda';
44
import { Construct } from 'constructs';
@@ -12,6 +12,7 @@ import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
1212
import { Auth } from './auth';
1313
import { ContainerImageBuild } from 'deploy-time-build';
1414
import { join } from 'path';
15+
import { Trigger } from 'aws-cdk-lib/triggers';
1516

1617
export interface WebappProps {
1718
database: Database;
@@ -34,6 +35,7 @@ export class Webapp extends Construct {
3435

3536
const { database, hostedZone, auth, subDomain } = props;
3637

38+
// Use ContainerImageBuild to inject deploy-time values in the build environment
3739
const image = new ContainerImageBuild(this, 'Build', {
3840
directory: join('..', 'webapp'),
3941
platform: Platform.LINUX_ARM64,
@@ -48,14 +50,6 @@ export class Webapp extends Construct {
4850
USER_POOL_CLIENT_ID: auth.client.userPoolClientId,
4951
},
5052
});
51-
// HOST_DOMAIN='localhost'
52-
// COGNITO_DOMAIN=auth.mtomooka.people.aws.dev
53-
// AMPLIFY_APP_ORIGIN=http://localhost:3000
54-
// USER_POOL_CLIENT_ID=4a6eduvcg2jjjemc9qe8ub9va0
55-
// USER_POOL_ID=ap-northeast-1_VyB04AYbz
56-
// AWS_REGION=ap-northeast-1
57-
// NEXT_PUBLIC_AWS_REGION=ap-northeast-1
58-
5953

6054
const handler = new DockerImageFunction(this, 'Handler', {
6155
code: image.toLambdaDockerImageCode(),
@@ -93,5 +87,33 @@ export class Webapp extends Construct {
9387
`${this.baseUrl}/api/auth/sign-in-callback`,
9488
`${this.baseUrl}/api/auth/sign-out-callback`,
9589
);
90+
91+
const migrationRunner = new DockerImageFunction(this, 'MigrationRunner', {
92+
code: DockerImageCode.fromImageAsset(join('..', 'webapp'), {
93+
platform: Platform.LINUX_ARM64,
94+
cmd: ['migration-runner.handler'],
95+
file: 'jobs.Dockerfile',
96+
}),
97+
architecture: Architecture.ARM_64,
98+
timeout: Duration.minutes(5),
99+
environment: {
100+
...database.getLambdaEnvironment('main'),
101+
},
102+
vpc: database.cluster.vpc,
103+
memorySize: 256,
104+
});
105+
migrationRunner.connections.allowToDefaultPort(database);
106+
107+
// run database migration during CDK deployment
108+
const trigger = new Trigger(this, 'MigrationTrigger', {
109+
handler: migrationRunner,
110+
});
111+
// make sure migration is executed after the database cluster is available.
112+
trigger.node.addDependency(database.cluster);
113+
114+
new CfnOutput(Stack.of(this), 'MigrationFunctionName', { value: migrationRunner.functionName });
115+
new CfnOutput(Stack.of(this), 'MigrationCommand', {
116+
value: `aws lambda invoke --function-name ${migrationRunner.functionName} --payload '{"command":"deploy"}' --cli-binary-format raw-in-base64-out /dev/stdout`,
117+
});
96118
}
97119
}

webapp/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ yarn-error.log*
4040
*.tsbuildinfo
4141
next-env.d.ts
4242

43+
dist
44+
4345
!prisma/.env

webapp/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ ENV AWS_LWA_READINESS_CHECK_PATH="/api/health"
2121
ENV AWS_LWA_INVOKE_MODE="response_stream"
2222

2323
COPY --from=builder /build/.next/static ./.next/static
24-
COPY --from=builder /build/public ./public
2524
COPY --from=builder /build/.next/standalone ./
2625
COPY --from=builder /build/run.sh ./run.sh
2726

webapp/eslint.config.mjs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { dirname } from "path";
2-
import { fileURLToPath } from "url";
3-
import { FlatCompat } from "@eslint/eslintrc";
1+
import { dirname } from 'path';
2+
import { fileURLToPath } from 'url';
3+
import { FlatCompat } from '@eslint/eslintrc';
4+
import { defineConfig } from 'eslint/config';
45

56
const __filename = fileURLToPath(import.meta.url);
67
const __dirname = dirname(__filename);
@@ -9,8 +10,11 @@ const compat = new FlatCompat({
910
baseDirectory: __dirname,
1011
});
1112

12-
const eslintConfig = [
13-
...compat.extends("next/core-web-vitals", "next/typescript"),
14-
];
15-
16-
export default eslintConfig;
13+
export default defineConfig([
14+
...compat.extends('next/core-web-vitals', 'next/typescript'),
15+
{
16+
rules: {
17+
'@typescript-eslint/no-unused-vars': 'off',
18+
},
19+
},
20+
]);

webapp/jobs.Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM public.ecr.aws/lambda/nodejs:22 AS builder
2+
WORKDIR /build
3+
COPY package*.json ./
4+
RUN --mount=type=cache,target=/root/.npm npm ci
5+
COPY ./ ./
6+
RUN npx prisma generate
7+
RUN npx esbuild src/jobs/*.ts --bundle --outdir=dist --platform=node --charset=utf8 --external:@prisma/client
8+
9+
FROM public.ecr.aws/lambda/nodejs:22 AS runner
10+
11+
COPY package*.json ./
12+
COPY prisma ./prisma
13+
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
14+
RUN npx prisma generate --generator client
15+
COPY --from=builder /build/dist/. ./
16+
17+
CMD ["migration-runner.handler"]

webapp/public/file.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapp/public/globe.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapp/public/next.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapp/public/vercel.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

webapp/public/window.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)