Hi, I am using an existing DynamoDB, how to enable...
# help
s
Hi, I am using an existing DynamoDB, how to enable stream on it? my code:
const table = new Table(stack, "Table", {
cdk: {
table: dynamodb.Table.fromTableArn(
stack,
"ec.subscriptions",
"arn:aws:dynamodb:ap-south-1:146331168322:table/ec.subscriptions"
),
stream: true,
consumers: {
consumer1: "functions/lambda.handler",
},
},
});
I have not set any stream or trigger in my DynamoDB. I want to enable stream and trigger from SST.
a
According to the documentation, stream and consumers properties should be directly on the root Table construct, not nested under cdk: https://docs.serverless-stack.com/constructs/Table#dynamodb-streams
s
@Adrian Schweizer Thank you, For an existing DynamoDB Table I did refactored my code like this and it worked for me.
table: dynamodb.Table.fromTableAttributes(stack, "ec.subscriptions", {
tableArn:
"arn:aws:dynamodb:ap-south-1:146331168322:table/ec.subscriptions",
tableStreamArn:
"arn:aws:dynamodb:ap-south-1:146331168322:table/ec.subscriptions/stream/2022-06-21T13:37:35.767",
}),
and added consumers like this:
table.addConsumers(stack, {
myConsumer: {
function: {
handler: "functions/hello.handler",
},
},
});
Know it deployed without any error. I will test my consumers and give my feedback.
f
Thanks @Adrian Schweizer!
s
@Adrian Schweizer My consumers worked well. Now I am trying out filters. Have any idea where to put the filters?
a
You could try something like this (completely untested, just a hypothetical way it could maybe work): 1. Create a DynamoEventSource object by doing
const myEventSource = new DynamoEventSource(bla);
(replace bla with proper parameters) 2. Get the lambda function you want to be called by DynamoDB events from your SST API definition by doing
const myLambda = api.getFunction("GET /myRoute");
(use the correct route definition as parameter) 3. Attach the DynamoEventSource to your lambda by doing
myLambda.addEventSource(mySource);
4. Modify your DynamoEventSource to add the FilterCriteria (seems it's not possible directly using CDK yet):
Copy code
const cfnSourceMapping = myEventSource.node.defaultChild as CfnEventSourceMapping

cfnSourceMapping.addPropertyOverride('FilterCriteria', {
    Filters: [
        {
            Pattern:
                JSON.stringify({
                    // Only capture DELETE events whose status IS deletion_requested"
                    dynamodb: {
                        OldImage: {
                            status: {S: ['banned']},
                        },
                    },
                    eventName: ['REMOVE'],
                }),
        },
    ],
})
If this doesn't work, you'll have to do it manually over console or cli until CDK supports it
Sorry, I guess you want to use the filter for your consumer, so instead of getting the function from the API, you would get it from the table of course: https://docs.serverless-stack.com/constructs/Table#getfunction
Also, you probably don't have to create a new DynamoEventSource, because setting up a consumer should already create one for you, so it's just a matter of retrieving it and then do step 4 from my previous list directly on it
I don't see a way to get the event source out of the table's consumers, so maybe you still have to set it up yourself and then pass it when you configure your consumers like described here: https://docs.serverless-stack.com/constructs/Table#consumers
So the way to try it would be: 1. Manually create the DynamoEventSource myEventSource 2. Add the filter criteria like discussed above to the event source 3. Pass the event source in TableConsumerProps when you config the consumers like so:
Copy code
const table = new Table(stack, "Table", {
  consumers: {
    consumer1: {
      function: "src/consumer1.main",
      cdk: {
        eventSource: myEventSource,
      },
    },
    consumer2: "src/consumer2.main",
  },
});