Skip to content

feat(gen2-migration): support storage DynamoDB trigger for gen2-migration#14766

Open
sai-ray wants to merge 12 commits intogen2-migrationfrom
sai/storage-ddb-trigger-for-gen2migration
Open

feat(gen2-migration): support storage DynamoDB trigger for gen2-migration#14766
sai-ray wants to merge 12 commits intogen2-migrationfrom
sai/storage-ddb-trigger-for-gen2migration

Conversation

@sai-ray
Copy link
Copy Markdown
Contributor

@sai-ray sai-ray commented Apr 9, 2026

Description of changes

Adds codegen support for storage DynamoDB Lambda triggers in gen2-migration generate. When a Gen1 function's CloudFormation template contains an EventSourceMapping with Ref: storage<tableName>StreamArn, the generated backend.ts now includes addEventSource, grantStreamRead, and grantTableListStreams wiring between the table and the trigger function.

This addresses the 🔴 Do you want to add a Lambda Trigger for your Table feature in the migration guide (under Storage → DynamoDB).

Approach:

FunctionGenerator parses its own CloudFormation template for EventSourceMapping resources that reference storage table streams via Ref: storage<tableName>StreamArn. This is consistent with how model triggers are detected (detectDynamoTriggerModels parses for Fn::ImportValue/Fn::Sub patterns). No cross-generator wiring needed — detection and generation happen entirely within FunctionGenerator.

Files changed:

  • function.generator.tsdetectStorageDynamoTriggers() and contributeStorageDynamoTrigger()
  • post-refactor.ts — fixed regex to match per-table stack names, added bookmarks and S3 bucket fixups

Test app:

Added activityTrigger79c26ead to the discussions app — a Lambda triggered by the activity table stream that increments a global counter. Updated all four snapshot directories.

Issue #, if available

Description of how you validated changes

  1. Snapshot tests: all 25 pass (12 generate + 13 refactor), no regressions on other apps.
  2. Integration test: npm run test:gen1 — creates a topic, verifies the counter increments via the full trigger chain (model stream → recorduseractivity → activity table → activityTrigger → counter).
  3. Manual E2E: deployed Gen1 with trigger, ran generate, manually added expected wiring to backend.ts, deployed Gen2, verified trigger fires. Ran refactor successfully.

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (migration guide 🔴→🟢 pending)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies
  • Pull request labels are added

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

return await fetchActivityStats();
}

const { userId } = event.arguments || {};
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove fallback

@sai-ray sai-ray changed the title Sai/storage ddb trigger for gen2migration feat(gen2-migration): support storage ddb trigger for gen2migration Apr 9, 2026
@sai-ray sai-ray changed the title feat(gen2-migration): support storage ddb trigger for gen2migration feat(gen2-migration): support storage DynamoDB trigger for gen2-migration Apr 9, 2026
@sai-ray sai-ray marked this pull request as ready for review April 9, 2026 19:35
@sai-ray sai-ray requested a review from a team as a code owner April 9, 2026 19:35
sai-ray added 11 commits April 10, 2026 11:19
Add activityTrigger function to the discussions test app. The
trigger fires on activity table stream inserts and increments
a global counter at STATS#global. Updated fetchuseractivity to
serve the counter via a new getActivityStats GraphQL query.
Frontend shows the count on the activity page.

Changes:
- backend/activityTrigger.cjs: new trigger function source
- backend/activityTrigger.package.json: trigger dependencies
- backend/schema.graphql: added getActivityStats query type
- backend/fetchuseractivity.cjs: added getStats routing
- backend/configure.sh: copies trigger files on configure
- src/main.js: loadActivityCounter on activity page
- index.html: activityCounter element in activity page
- src/graphql/queries.js: regenerated with getActivityStats
- README.md: added trigger setup and update function steps
---
Prompt: Add a DynamoDB storage Lambda trigger to the
discussions test app with an activity counter that surfaces
on the frontend, update golden backend files, configure
scripts, and README.
…gger

Add test that verifies the activityTrigger Lambda increments
the global counter after a topic is created. Polls
getActivityStats until activityCount > 0.
---
Prompt: Sync updated discussions test app files from working
copy to amplify-migration-apps.
Update pre-generate and post-generate snapshots to include the
activityTrigger79c26ead function and its EventSourceMapping
wiring on the activity table. Post-generate backend.ts includes
the expected addEventSource/grantStreamRead/grantTableListStreams
calls. Updated post-generate.ts to convert the trigger function
to ESM.
---
Prompt: Update snapshots with storage DynamoDB trigger after
validating the Gen2 deployment works end-to-end.
Add codegen support for storage DynamoDB Lambda triggers in
gen2-migration generate. When a storage table has
triggerFunctions in cli-inputs.json, the generated backend.ts
now includes addEventSource, grantStreamRead, and
grantTableListStreams wiring.

DynamoDBGenerator exposes resourceName, tableVariableName,
and triggerFunctions getters. FunctionGenerator checks if it
appears in any DynamoDBGenerator's triggerFunctions list and
emits the wiring AST. generate.ts collects DynamoDBGenerator
instances and wires them to FunctionGenerator.

Updated discussions test app with activityTrigger79c26ead on
the activity table. Captured all four snapshots (pre-generate,
post-generate, pre-refactor, post-refactor) and verified the
full migration workflow end-to-end.
---
Prompt: Implement storage DynamoDB Lambda trigger codegen
for the Gen1-to-Gen2 migration tool, following the existing
model trigger pattern.
Add codegen support for storage DynamoDB Lambda triggers in
gen2-migration generate. When a storage table has
triggerFunctions in cli-inputs.json, the generated backend.ts
now includes addEventSource, grantStreamRead, and
grantTableListStreams wiring.

DynamoDBGenerator exposes resourceName, tableVariableName,
and triggerFunctions getters. FunctionGenerator checks if it
appears in any DynamoDBGenerator's triggerFunctions list and
emits the wiring AST. generate.ts collects DynamoDBGenerator
instances and wires them to FunctionGenerator.

Updated discussions test app with activityTrigger79c26ead on
the activity table. Captured all four snapshots (pre-generate,
post-generate, pre-refactor, post-refactor) and verified the
full migration workflow end-to-end.
---
Prompt: Implement storage DynamoDB Lambda trigger codegen
for the Gen1-to-Gen2 migration tool, following the existing
model trigger pattern.
Fix package.json name in pre-generate and post-generate
snapshots to use -snapshot suffix (matching convention used
by all other apps). Update post-generate snapshot via
--updateSnapshot to match actual codegen output.

All 12 generate snapshot tests pass.
---
Prompt: Fix snapshot test failures for discussions app.
Replace real AWS account ID with placeholder in pre-refactor
snapshots. Update post-refactor snapshot. All generate and
refactor snapshot tests pass.
---
Prompt: Fix refactor snapshot test failures caused by
unsanitized account IDs.
Restore standard boilerplate in pre-generate and post-generate
.gitignore files. Remove test-only deps from pre-generate
package.json so codegen output matches expected snapshot.
All 25 generate + refactor tests pass.
---
Prompt: Fix CI lockfile failure and gitignore differences
in discussion snapshots.
Run yarn install to add discussions snapshot dependencies
to the lockfile. Fixes CI immutable lockfile check.
---
Prompt: Fix CI lockfile failure by running yarn install.
Fix regex to match per-table stack names (storageActivityStack,
storageBookmarksStack) instead of the old shared storageStack.
Add bookmarks table name and S3 bucket name uncommenting.
---
Prompt: Fix post-refactor.ts to match the actual codegen
output and handle all post-refactor edits.
Replace real Amplify App ID (d1c9f6du1r9hog) with sanitized
name (discussions) in all pre-refactor and post-refactor
snapshot file contents and filenames.
---
Prompt: Sanitize the app ID in refactor snapshot files.
@sai-ray sai-ray force-pushed the sai/storage-ddb-trigger-for-gen2migration branch from 22e396e to abe54ff Compare April 10, 2026 15:22
Replace cli-inputs.json detection with CloudFormation template
parsing, consistent with how model triggers are detected.
FunctionGenerator now parses its own CFN template for
EventSourceMapping resources with Ref: storageXStreamArn.

Removed cross-generator wiring: DynamoDBGenerator getters,
addDynamoDBGenerator(), and generate.ts wiring loop. Only
function.generator.ts has codegen changes; dynamodb.generator.ts
and generate.ts are reverted to their original state.

All 25 snapshot tests pass.
---
Prompt: Switch storage DynamoDB trigger detection from
cli-inputs.json to CFN template parsing for consistency
with model triggers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant