1- import { IgnoreMode , Duration } from 'aws-cdk-lib' ;
1+ import { IgnoreMode , Duration , CfnOutput , Stack } from 'aws-cdk-lib' ;
22import { Platform } from 'aws-cdk-lib/aws-ecr-assets' ;
33import { DockerImageFunction , DockerImageCode , Architecture } from 'aws-cdk-lib/aws-lambda' ;
44import { Construct } from 'constructs' ;
@@ -12,6 +12,7 @@ import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
1212import { Auth } from './auth' ;
1313import { ContainerImageBuild } from 'deploy-time-build' ;
1414import { join } from 'path' ;
15+ import { Trigger } from 'aws-cdk-lib/triggers' ;
1516
1617export 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}
0 commit comments