Skip to content

Commit dad0742

Browse files
konokenjremote-swe-app[bot]
andauthored
docs: add documentation for migration runner and deployment process (#73)
## Overview This PR adds comprehensive documentation for the database migration runner and deployment process to improve developer experience and clarify how the system works. ## Changes ### 1. Enhanced Migration Runner Documentation (`webapp/src/jobs/migration-runner.ts`) - Added detailed comments explaining the two invocation contexts: - **CDK Trigger**: Automatically invoked during `cdk deploy` with default payload - **Manual Invocation**: Using AWS CLI with custom commands - Included example AWS CLI command for manual execution - Referenced CloudFormation stack outputs for function name and command template ### 2. Improved CDK Infrastructure Comments (`cdk/lib/constructs/webapp.ts`) - Added comments to Trigger construct explaining automatic invocation with default payload - Added comments to CfnOutput section with available commands and execution examples - Clarified the relationship between CDK Trigger and manual Lambda invocation ### 3. Extended README.md Deploy Section Added two new subsections under the Deploy section: #### WebApp Deployment - Explains that Next.js webapp is built and deployed during CDK deployment using `deploy-time-build` - Links to implementation file for details #### Database Migration - Explains that migrations are automatically executed during CDK deployment using CDK Trigger - Links to implementation files (webapp.ts and migration-runner.ts) - Provides AWS CLI command example for manual migration execution - Lists available commands: `deploy` (default), `force` (with --accept-data-loss) ## Benefits - Developers can now easily understand how to manually run database migrations - Clear documentation of the deployment process and migration workflow - Improved code maintainability with inline comments - Better onboarding experience for new developers ## Testing - Verified all links in README.md point to correct files - Confirmed code comments are accurate and helpful - No functional changes - documentation only <!-- DO NOT EDIT: System generated metadata --> <!-- WORKER_ID:1765116242720649 --> Co-authored-by: remote-swe-app[bot] <123456+remote-swe-app[bot]@users.noreply.github.com>
1 parent 5dfdbf9 commit dad0742

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ ServerlessWebappStarterKitStack.FrontendDomainName = https://web.exmaple.com
8484

8585
Opening the URL in `FrontendDomainName` output, you can now try the sample app on your browser.
8686

87+
### WebApp Deployment
88+
89+
The Next.js webapp is built and deployed during the CDK deployment process using [deploy-time-build](https://github.com/tmokmss/deploy-time-build). This approach ensures your application is containerized and deployed to AWS Lambda as part of the infrastructure deployment. See the [implementation](./cdk/lib/constructs/webapp.ts) for details.
90+
91+
### Database Migration
92+
93+
Database migrations are automatically executed during CDK deployment using a [CDK Trigger](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.triggers-readme.html). The migration runs with the default `deploy` command. See the [implementation](./cdk/lib/constructs/webapp.ts) and [migration runner](./webapp/src/jobs/migration-runner.ts) for details.
94+
95+
To manually run migrations with different commands:
96+
97+
```sh
98+
aws lambda invoke \
99+
--function-name <MigrationFunctionName from CDK output> \
100+
--payload '{"command":"deploy"}' \
101+
--cli-binary-format raw-in-base64-out \
102+
/dev/stdout
103+
```
104+
105+
Available commands: `deploy` (default), `force` (with --accept-data-loss).
106+
87107
## Add your own features
88108
To implement your own features, you may want to add frontend pages, API routes, or async jobs. The project uses Next.js App Router, which provides a unified approach for both frontend and backend development. You can follow the conventional Next.js patterns to add pages and API routes. For more details, please refer to the [`webapp/README.md`](./webapp/README.md) guide.
89109

cdk/lib/constructs/webapp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,18 @@ export class Webapp extends Construct {
157157
});
158158
migrationRunner.connections.allowToDefaultPort(database);
159159

160-
// run database migration during CDK deployment
160+
// Run database migration during CDK deployment
161+
// The Trigger construct automatically invokes the migration runner with default payload (command: 'deploy')
162+
// To manually run migrations with different commands (e.g., 'force'), use the AWS CLI command shown in the CDK output below
161163
const trigger = new Trigger(this, 'MigrationTrigger', {
162164
handler: migrationRunner,
163165
});
164166
// make sure migration is executed after the database cluster is available.
165167
trigger.node.addDependency(database.cluster);
166168

169+
// Output migration-related information for manual invocation
170+
// Available commands: "deploy" (default), "force" (with --accept-data-loss)
171+
// Example: aws lambda invoke --function-name <FUNCTION_NAME> --payload '{"command":"force"}' --cli-binary-format raw-in-base64-out /dev/stdout
167172
new CfnOutput(Stack.of(this), 'MigrationFunctionName', { value: migrationRunner.functionName });
168173
new CfnOutput(Stack.of(this), 'MigrationCommand', {
169174
value: `aws lambda invoke --function-name ${migrationRunner.functionName} --payload '{"command":"deploy"}' --cli-binary-format raw-in-base64-out /dev/stdout`,

webapp/src/jobs/migration-runner.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { execFile } from 'child_process';
33
import path from 'path';
44

55
export const handler: Handler = async (event, _) => {
6+
// This Lambda function is invoked in two contexts:
7+
// 1. CDK Trigger: Automatically invoked during `cdk deploy` with default payload (no command specified, defaults to 'deploy')
8+
// 2. Manual Invocation: Use AWS CLI to invoke with custom commands
9+
// Example: aws lambda invoke --function-name <FUNCTION_NAME> --payload '{"command":"force"}' --cli-binary-format raw-in-base64-out /dev/stdout
10+
// The function name and command template are available in the CloudFormation stack outputs after deployment
11+
//
612
// Available commands are:
713
// deploy: create new database if absent and apply all migrations to the existing database.
814
// reset: delete existing database, create new one, and apply all migrations. NOT for production environment.

0 commit comments

Comments
 (0)