Skip to content

Commit

Permalink
fix dynamodb lambda stream setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch committed Aug 28, 2024
1 parent ba07a30 commit 10555c9
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ 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 { auth } from "./auth/resource";
import { data } from "./data/resource";
import { myDynamoDBFunction } from "./functions/dynamoDB-function/resource";
Expand All @@ -94,9 +93,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);
```

0 comments on commit 10555c9

Please sign in to comment.