Skip to content

Commit 4a20f5a

Browse files
committed
run database migration on deploy
1 parent 40521ef commit 4a20f5a

3 files changed

Lines changed: 252 additions & 6 deletions

File tree

cdk/lib/constructs/webapp.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ export class Webapp extends Construct {
145145
});
146146
migrationRunner.connections.allowToDefaultPort(database);
147147

148-
// // run database migration during CDK deployment
149-
// const trigger = new Trigger(this, 'MigrationTrigger', {
150-
// handler: migrationRunner,
151-
// });
152-
// // make sure migration is executed after the database cluster is available.
153-
// trigger.node.addDependency(database.cluster);
148+
// run database migration during CDK deployment
149+
const trigger = new Trigger(this, 'MigrationTrigger', {
150+
handler: migrationRunner,
151+
});
152+
// make sure migration is executed after the database cluster is available.
153+
trigger.node.addDependency(database.cluster);
154154

155155
new CfnOutput(Stack.of(this), 'MigrationFunctionName', { value: migrationRunner.functionName });
156156
new CfnOutput(Stack.of(this), 'MigrationCommand', {

cdk/test/__snapshots__/serverless-fullstack-webapp-starter-kit-without-domain.test.ts.snap

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,82 @@ Object {
457457
},
458458
"Type": "AWS::IAM::Role",
459459
},
460+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderHandler97BECD91": Object {
461+
"DependsOn": Array [
462+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderRoleE18FAF0A",
463+
],
464+
"Properties": Object {
465+
"Code": Object {
466+
"S3Bucket": "cdk-hnb659fds-assets-123456789012-us-west-2",
467+
"S3Key": "REDACTED",
468+
},
469+
"Handler": "__entrypoint__.handler",
470+
"MemorySize": 128,
471+
"Role": Object {
472+
"Fn::GetAtt": Array [
473+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderRoleE18FAF0A",
474+
"Arn",
475+
],
476+
},
477+
"Runtime": "nodejs22.x",
478+
"Timeout": 900,
479+
},
480+
"Type": "AWS::Lambda::Function",
481+
},
482+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderRoleE18FAF0A": Object {
483+
"Properties": Object {
484+
"AssumeRolePolicyDocument": Object {
485+
"Statement": Array [
486+
Object {
487+
"Action": "sts:AssumeRole",
488+
"Effect": "Allow",
489+
"Principal": Object {
490+
"Service": "lambda.amazonaws.com",
491+
},
492+
},
493+
],
494+
"Version": "2012-10-17",
495+
},
496+
"ManagedPolicyArns": Array [
497+
Object {
498+
"Fn::Sub": "arn:\${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
499+
},
500+
],
501+
"Policies": Array [
502+
Object {
503+
"PolicyDocument": Object {
504+
"Statement": Array [
505+
Object {
506+
"Action": Array [
507+
"lambda:InvokeFunction",
508+
],
509+
"Effect": "Allow",
510+
"Resource": Array [
511+
Object {
512+
"Fn::Join": Array [
513+
"",
514+
Array [
515+
Object {
516+
"Fn::GetAtt": Array [
517+
"WebappMigrationRunnerAC67C012",
518+
"Arn",
519+
],
520+
},
521+
":*",
522+
],
523+
],
524+
},
525+
],
526+
},
527+
],
528+
"Version": "2012-10-17",
529+
},
530+
"PolicyName": "Inline",
531+
},
532+
],
533+
},
534+
"Type": "AWS::IAM::Role",
535+
},
460536
"AccessLogBucketAutoDeleteObjectsCustomResource01AB31E8": Object {
461537
"DeletionPolicy": "Delete",
462538
"DependsOn": Array [
@@ -3638,6 +3714,22 @@ sudo service iptables save",
36383714
},
36393715
"Type": "AWS::Lambda::Function",
36403716
},
3717+
"WebappMigrationRunnerCurrentVersionREDACTED": Object {
3718+
"DependsOn": Array [
3719+
"VpcPrivateSubnet1DefaultRouteBE02A9ED",
3720+
"VpcPrivateSubnet1RouteTableAssociation70C59FA6",
3721+
"VpcPrivateSubnet2DefaultRoute060D2087",
3722+
"VpcPrivateSubnet2RouteTableAssociationA89CAD56",
3723+
"VpcPrivateSubnet3DefaultRoute94B74F0D",
3724+
"VpcPrivateSubnet3RouteTableAssociation16BDDC43",
3725+
],
3726+
"Properties": Object {
3727+
"FunctionName": Object {
3728+
"Ref": "WebappMigrationRunnerAC67C012",
3729+
},
3730+
},
3731+
"Type": "AWS::Lambda::Version",
3732+
},
36413733
"WebappMigrationRunnerSecurityGroup7F0DF264": Object {
36423734
"DependsOn": Array [
36433735
"VpcPrivateSubnet1DefaultRouteBE02A9ED",
@@ -3713,6 +3805,37 @@ sudo service iptables save",
37133805
},
37143806
"Type": "AWS::IAM::Role",
37153807
},
3808+
"WebappMigrationTrigger42AFC1D9": Object {
3809+
"DeletionPolicy": "Delete",
3810+
"DependsOn": Array [
3811+
"DatabaseCluster5B53A178",
3812+
"DatabaseClusterSecretAttachmentDC8466C0",
3813+
"DatabaseClusterSecretD1FB634F",
3814+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackAsyncJobHandlerSecurityGroup5220DFB3IndirectPort9323962E",
3815+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackDatabaseBastionHostInstanceSecurityGroup4F0DD25BIndirectPort8AFD9922",
3816+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackWebappHandlerSecurityGroupA009CF4AIndirectPort7311063E",
3817+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackWebappMigrationRunnerSecurityGroupC0959349IndirectPortDCF4A356",
3818+
"DatabaseClusterSecurityGroupFEF1426A",
3819+
"DatabaseClusterSubnets5540150D",
3820+
"DatabaseClusterWriterD43085C6",
3821+
],
3822+
"Properties": Object {
3823+
"ExecuteOnHandlerChange": true,
3824+
"HandlerArn": Object {
3825+
"Ref": "WebappMigrationRunnerCurrentVersionREDACTED",
3826+
},
3827+
"InvocationType": "RequestResponse",
3828+
"ServiceToken": Object {
3829+
"Fn::GetAtt": Array [
3830+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderHandler97BECD91",
3831+
"Arn",
3832+
],
3833+
},
3834+
"Timeout": "120000",
3835+
},
3836+
"Type": "Custom::Trigger",
3837+
"UpdateReplacePolicy": "Delete",
3838+
},
37163839
"WebappOrigin1FunctionUrlOriginAccessControlEA98B600": Object {
37173840
"Properties": Object {
37183841
"OriginAccessControlConfig": Object {

cdk/test/__snapshots__/serverless-fullstack-webapp-starter-kit.test.ts.snap

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,82 @@ Object {
491491
},
492492
"Type": "AWS::IAM::Role",
493493
},
494+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderHandler97BECD91": Object {
495+
"DependsOn": Array [
496+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderRoleE18FAF0A",
497+
],
498+
"Properties": Object {
499+
"Code": Object {
500+
"S3Bucket": "cdk-hnb659fds-assets-123456789012-us-west-2",
501+
"S3Key": "REDACTED",
502+
},
503+
"Handler": "__entrypoint__.handler",
504+
"MemorySize": 128,
505+
"Role": Object {
506+
"Fn::GetAtt": Array [
507+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderRoleE18FAF0A",
508+
"Arn",
509+
],
510+
},
511+
"Runtime": "nodejs22.x",
512+
"Timeout": 900,
513+
},
514+
"Type": "AWS::Lambda::Function",
515+
},
516+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderRoleE18FAF0A": Object {
517+
"Properties": Object {
518+
"AssumeRolePolicyDocument": Object {
519+
"Statement": Array [
520+
Object {
521+
"Action": "sts:AssumeRole",
522+
"Effect": "Allow",
523+
"Principal": Object {
524+
"Service": "lambda.amazonaws.com",
525+
},
526+
},
527+
],
528+
"Version": "2012-10-17",
529+
},
530+
"ManagedPolicyArns": Array [
531+
Object {
532+
"Fn::Sub": "arn:\${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
533+
},
534+
],
535+
"Policies": Array [
536+
Object {
537+
"PolicyDocument": Object {
538+
"Statement": Array [
539+
Object {
540+
"Action": Array [
541+
"lambda:InvokeFunction",
542+
],
543+
"Effect": "Allow",
544+
"Resource": Array [
545+
Object {
546+
"Fn::Join": Array [
547+
"",
548+
Array [
549+
Object {
550+
"Fn::GetAtt": Array [
551+
"WebappMigrationRunnerAC67C012",
552+
"Arn",
553+
],
554+
},
555+
":*",
556+
],
557+
],
558+
},
559+
],
560+
},
561+
],
562+
"Version": "2012-10-17",
563+
},
564+
"PolicyName": "Inline",
565+
},
566+
],
567+
},
568+
"Type": "AWS::IAM::Role",
569+
},
494570
"AccessLogBucketAutoDeleteObjectsCustomResource01AB31E8": Object {
495571
"DeletionPolicy": "Delete",
496572
"DependsOn": Array [
@@ -3457,6 +3533,22 @@ sudo service iptables save",
34573533
},
34583534
"Type": "AWS::Lambda::Function",
34593535
},
3536+
"WebappMigrationRunnerCurrentVersionREDACTED": Object {
3537+
"DependsOn": Array [
3538+
"VpcPrivateSubnet1DefaultRouteBE02A9ED",
3539+
"VpcPrivateSubnet1RouteTableAssociation70C59FA6",
3540+
"VpcPrivateSubnet2DefaultRoute060D2087",
3541+
"VpcPrivateSubnet2RouteTableAssociationA89CAD56",
3542+
"VpcPrivateSubnet3DefaultRoute94B74F0D",
3543+
"VpcPrivateSubnet3RouteTableAssociation16BDDC43",
3544+
],
3545+
"Properties": Object {
3546+
"FunctionName": Object {
3547+
"Ref": "WebappMigrationRunnerAC67C012",
3548+
},
3549+
},
3550+
"Type": "AWS::Lambda::Version",
3551+
},
34603552
"WebappMigrationRunnerSecurityGroup7F0DF264": Object {
34613553
"DependsOn": Array [
34623554
"VpcPrivateSubnet1DefaultRouteBE02A9ED",
@@ -3532,6 +3624,37 @@ sudo service iptables save",
35323624
},
35333625
"Type": "AWS::IAM::Role",
35343626
},
3627+
"WebappMigrationTrigger42AFC1D9": Object {
3628+
"DeletionPolicy": "Delete",
3629+
"DependsOn": Array [
3630+
"DatabaseCluster5B53A178",
3631+
"DatabaseClusterSecretAttachmentDC8466C0",
3632+
"DatabaseClusterSecretD1FB634F",
3633+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackAsyncJobHandlerSecurityGroup5220DFB3IndirectPort9323962E",
3634+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackDatabaseBastionHostInstanceSecurityGroup4F0DD25BIndirectPort8AFD9922",
3635+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackWebappHandlerSecurityGroupA009CF4AIndirectPort7311063E",
3636+
"DatabaseClusterSecurityGroupfromServerlessWebappStarterKitStackWebappMigrationRunnerSecurityGroupC0959349IndirectPortDCF4A356",
3637+
"DatabaseClusterSecurityGroupFEF1426A",
3638+
"DatabaseClusterSubnets5540150D",
3639+
"DatabaseClusterWriterD43085C6",
3640+
],
3641+
"Properties": Object {
3642+
"ExecuteOnHandlerChange": true,
3643+
"HandlerArn": Object {
3644+
"Ref": "WebappMigrationRunnerCurrentVersionREDACTED",
3645+
},
3646+
"InvocationType": "RequestResponse",
3647+
"ServiceToken": Object {
3648+
"Fn::GetAtt": Array [
3649+
"AWSCDKTriggerCustomResourceProviderCustomResourceProviderHandler97BECD91",
3650+
"Arn",
3651+
],
3652+
},
3653+
"Timeout": "120000",
3654+
},
3655+
"Type": "Custom::Trigger",
3656+
"UpdateReplacePolicy": "Delete",
3657+
},
35353658
"WebappOrigin1FunctionUrlOriginAccessControlEA98B600": Object {
35363659
"Properties": Object {
35373660
"OriginAccessControlConfig": Object {

0 commit comments

Comments
 (0)