@@ -83,7 +83,6 @@ Lastly, create DynamoDB table as event source in the `amplify/backend.ts` file:
83
83
``` ts title="amplify/backend.ts"
84
84
import { defineBackend } from " @aws-amplify/backend" ;
85
85
import { StartingPosition } from " aws-cdk-lib/aws-lambda" ;
86
- import { DynamoEventSource } from " aws-cdk-lib/aws-lambda-event-sources" ;
87
86
import { auth } from " ./auth/resource" ;
88
87
import { data } from " ./data/resource" ;
89
88
import { myDynamoDBFunction } from " ./functions/dynamoDB-function/resource" ;
@@ -94,9 +93,36 @@ const backend = defineBackend({
94
93
myDynamoDBFunction ,
95
94
});
96
95
97
- const eventSource = new DynamoEventSource (backend .data .resources .tables [" Todo" ], {
98
- startingPosition: StartingPosition .LATEST ,
99
- });
96
+ const todoTable = backend .data .resources .tables [" Todo" ];
97
+ const policy = new Policy (
98
+ Stack .of (todoTable ),
99
+ " MyDynamoDBFunctionStreamingPolicy" ,
100
+ {
101
+ statements: [
102
+ new PolicyStatement ({
103
+ effect: Effect .ALLOW ,
104
+ actions: [
105
+ " dynamodb:DescribeStream" ,
106
+ " dynamodb:GetRecords" ,
107
+ " dynamodb:GetShardIterator" ,
108
+ " dynamodb:ListStreams" ,
109
+ ],
110
+ resources: [" *" ],
111
+ }),
112
+ ],
113
+ }
114
+ );
115
+ backend .myDynamoDBFunction .resources .lambda .role ?.attachInlinePolicy (policy );
116
+
117
+ const mapping = new EventSourceMapping (
118
+ Stack .of (todoTable ),
119
+ " MyDynamoDBFunctionTodoEventStreamMapping" ,
120
+ {
121
+ target: backend .myDynamoDBFunction .resources .lambda ,
122
+ eventSourceArn: todoTable .tableStreamArn ,
123
+ startingPosition: StartingPosition .LATEST ,
124
+ }
125
+ );
100
126
101
- backend . myDynamoDBFunction . resources . lambda . addEventSource ( eventSource );
127
+ mapping . node . addDependency ( policy );
102
128
```
0 commit comments