Skip to content

Commit 10555c9

Browse files
author
Dane Pilcher
committed
fix dynamodb lambda stream setup
1 parent ba07a30 commit 10555c9

File tree

1 file changed

+31
-5
lines changed
  • src/pages/[platform]/build-a-backend/functions/examples/dynamo-db-stream

1 file changed

+31
-5
lines changed

src/pages/[platform]/build-a-backend/functions/examples/dynamo-db-stream/index.mdx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ Lastly, create DynamoDB table as event source in the `amplify/backend.ts` file:
8383
```ts title="amplify/backend.ts"
8484
import { defineBackend } from "@aws-amplify/backend";
8585
import { StartingPosition } from "aws-cdk-lib/aws-lambda";
86-
import { DynamoEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
8786
import { auth } from "./auth/resource";
8887
import { data } from "./data/resource";
8988
import { myDynamoDBFunction } from "./functions/dynamoDB-function/resource";
@@ -94,9 +93,36 @@ const backend = defineBackend({
9493
myDynamoDBFunction,
9594
});
9695

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+
);
100126

101-
backend.myDynamoDBFunction.resources.lambda.addEventSource(eventSource);
127+
mapping.node.addDependency(policy);
102128
```

0 commit comments

Comments
 (0)