@@ -71,7 +71,7 @@ export const handler: DynamoDBStreamHandler = async (event) => {
71
71
}
72
72
}
73
73
logger .info (` Successfully processed ${event .Records .length } records. ` );
74
-
74
+
75
75
return {
76
76
batchItemFailures: [],
77
77
};
@@ -82,21 +82,49 @@ Lastly, create DynamoDB table as event source in the `amplify/backend.ts` file:
82
82
83
83
``` ts title="amplify/backend.ts"
84
84
import { 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" ;
87
88
import { auth } from " ./auth/resource" ;
88
89
import { data } from " ./data/resource" ;
89
- import { myDynamoDBFunction } from " ./functions/kinesis -function/resource" ;
90
+ import { myDynamoDBFunction } from " ./functions/dynamoDB -function/resource" ;
90
91
91
92
const backend = defineBackend ({
92
93
auth ,
93
94
data ,
94
95
myDynamoDBFunction ,
95
96
});
96
97
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
+ );
100
128
101
- backend . myDynamoDBFunction . resources . lambda . addEventSource ( eventSource );
129
+ mapping . node . addDependency ( policy );
102
130
```
0 commit comments