diff --git a/src/pages/[platform]/build-a-backend/functions/examples/dynamo-db-stream/index.mdx b/src/pages/[platform]/build-a-backend/functions/examples/dynamo-db-stream/index.mdx index 9674d6827b4..97ece1d1e6f 100644 --- a/src/pages/[platform]/build-a-backend/functions/examples/dynamo-db-stream/index.mdx +++ b/src/pages/[platform]/build-a-backend/functions/examples/dynamo-db-stream/index.mdx @@ -71,7 +71,7 @@ export const handler: DynamoDBStreamHandler = async (event) => { } } logger.info(`Successfully processed ${event.Records.length} records.`); - + return { batchItemFailures: [], }; @@ -82,11 +82,12 @@ Lastly, create DynamoDB table as event source in the `amplify/backend.ts` file: ```ts title="amplify/backend.ts" import { defineBackend } from "@aws-amplify/backend"; -import { StartingPosition } from "aws-cdk-lib/aws-lambda"; -import { DynamoEventSource } from "aws-cdk-lib/aws-lambda-event-sources"; +import { Stack } from "aws-cdk-lib"; +import { Policy, PolicyStatement, Effect } from "aws-cdk-lib/aws-iam"; +import { StartingPosition, EventSourceMapping } from "aws-cdk-lib/aws-lambda"; import { auth } from "./auth/resource"; import { data } from "./data/resource"; -import { myDynamoDBFunction } from "./functions/kinesis-function/resource"; +import { myDynamoDBFunction } from "./functions/dynamoDB-function/resource"; const backend = defineBackend({ auth, @@ -94,9 +95,36 @@ const backend = defineBackend({ myDynamoDBFunction, }); -const eventSource = new DynamoEventSource(backend.data.resources.tables["Todo"], { - startingPosition: StartingPosition.LATEST, -}); +const todoTable = backend.data.resources.tables["Todo"]; +const policy = new Policy( + Stack.of(todoTable), + "MyDynamoDBFunctionStreamingPolicy", + { + statements: [ + new PolicyStatement({ + effect: Effect.ALLOW, + actions: [ + "dynamodb:DescribeStream", + "dynamodb:GetRecords", + "dynamodb:GetShardIterator", + "dynamodb:ListStreams", + ], + resources: ["*"], + }), + ], + } +); +backend.myDynamoDBFunction.resources.lambda.role?.attachInlinePolicy(policy); + +const mapping = new EventSourceMapping( + Stack.of(todoTable), + "MyDynamoDBFunctionTodoEventStreamMapping", + { + target: backend.myDynamoDBFunction.resources.lambda, + eventSourceArn: todoTable.tableStreamArn, + startingPosition: StartingPosition.LATEST, + } +); -backend.myDynamoDBFunction.resources.lambda.addEventSource(eventSource); +mapping.node.addDependency(policy); ```