@@ -71,7 +71,7 @@ export const handler: DynamoDBStreamHandler = async (event) => {
7171 }
7272 }
7373 logger .info (` Successfully processed ${event .Records .length } records. ` );
74-
74+
7575 return {
7676 batchItemFailures: [],
7777 };
@@ -82,21 +82,49 @@ Lastly, create DynamoDB table as event source in the `amplify/backend.ts` file:
8282
8383``` ts title="amplify/backend.ts"
8484import { defineBackend } from " @aws-amplify/backend" ;
85- import { StartingPosition } from " aws-cdk-lib/aws-lambda" ;
86- import { DynamoEventSource } from " aws-cdk-lib/aws-lambda-event-sources" ;
85+ import { Stack } from " aws-cdk-lib" ;
86+ import { Policy , PolicyStatement , Effect } from " aws-cdk-lib/aws-iam" ;
87+ import { StartingPosition , EventSourceMapping } from " aws-cdk-lib/aws-lambda" ;
8788import { auth } from " ./auth/resource" ;
8889import { data } from " ./data/resource" ;
89- import { myDynamoDBFunction } from " ./functions/kinesis -function/resource" ;
90+ import { myDynamoDBFunction } from " ./functions/dynamoDB -function/resource" ;
9091
9192const backend = defineBackend ({
9293 auth ,
9394 data ,
9495 myDynamoDBFunction ,
9596});
9697
97- const eventSource = new DynamoEventSource (backend .data .resources .tables [" Todo" ], {
98- startingPosition: StartingPosition .LATEST ,
99- });
98+ const todoTable = backend .data .resources .tables [" Todo" ];
99+ const policy = new Policy (
100+ Stack .of (todoTable ),
101+ " MyDynamoDBFunctionStreamingPolicy" ,
102+ {
103+ statements: [
104+ new PolicyStatement ({
105+ effect: Effect .ALLOW ,
106+ actions: [
107+ " dynamodb:DescribeStream" ,
108+ " dynamodb:GetRecords" ,
109+ " dynamodb:GetShardIterator" ,
110+ " dynamodb:ListStreams" ,
111+ ],
112+ resources: [" *" ],
113+ }),
114+ ],
115+ }
116+ );
117+ backend .myDynamoDBFunction .resources .lambda .role ?.attachInlinePolicy (policy );
118+
119+ const mapping = new EventSourceMapping (
120+ Stack .of (todoTable ),
121+ " MyDynamoDBFunctionTodoEventStreamMapping" ,
122+ {
123+ target: backend .myDynamoDBFunction .resources .lambda ,
124+ eventSourceArn: todoTable .tableStreamArn ,
125+ startingPosition: StartingPosition .LATEST ,
126+ }
127+ );
100128
101- backend . myDynamoDBFunction . resources . lambda . addEventSource ( eventSource );
129+ mapping . node . addDependency ( policy );
102130```
0 commit comments