Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dynamodb lambda stream setup #7917

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const handler: DynamoDBStreamHandler = async (event) => {
}
}
logger.info(`Successfully processed ${event.Records.length} records.`);

return {
batchItemFailures: [],
};
Expand All @@ -83,20 +83,46 @@ 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/kinesis-function/resource";
import { myDynamoDBFunction } from "./functions/dynamoDB-function/resource";

const backend = defineBackend({
auth,
data,
myDynamoDBFunction,
});

const eventSource = new DynamoEventSource(backend.data.resources.tables["Todo"], {
startingPosition: StartingPosition.LATEST,
});
const todoTable = backend.data.resources.tables["Todo"];
const policy = new Policy(
josefaidt marked this conversation as resolved.
Show resolved Hide resolved
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);
```