Skip to content

Commit bca62c6

Browse files
author
Dane Pilcher
authored
fix dynamodb lambda stream setup (#7917)
1 parent 2053da0 commit bca62c6

File tree

1 file changed

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

1 file changed

+36
-8
lines changed

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
8484
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";
8788
import { auth } from "./auth/resource";
8889
import { data } from "./data/resource";
89-
import { myDynamoDBFunction } from "./functions/kinesis-function/resource";
90+
import { myDynamoDBFunction } from "./functions/dynamoDB-function/resource";
9091

9192
const 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

Comments
 (0)