Question:
I currently have a dynamodb table that’s been in use for a couple of years, originally created in the console. It contains lots of valuable data. It uses a stream to periodically send a snapshot using a lambda trigger of the table to s3 for analytics. The table itself is heavily used by end users to access their data.
I want to migrate my solution into CDK. The options I want to explore:
- When you use the Table.fromTableArn construct, you don’t get access to the table stream arn so it’s impossible to attach a lambda trigger. Is there a way around this?
- Is there a way to clone the dynamoDB table contents in my CDK stack so that my copy will start off in exactly the same state as the original? Then I can add and manage the stream etc in CDK no problem.
- It’s worth checking my assumption that these are the only 2 options.
Answer:
- Subscribing Lambda to an existing Dynamo Table:
We don’t need to have table created within same stack. We can’t use addEventSource on lambda but we can use addEventSourceMapping
and add necessary policies to Lambda, which is what addEventSource does behind the scenes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
const streamsArn = "arn:aws:dynamodb:us-east-1:110011001100:table/test/stream/2021-03-18T06:25:21.904"; const myLambda = new lambda.Function(this, "my-lambda", { code: new lambda.InlineCode(` exports.handler = (event, context, callback) => { console.log('event',event) callback(null,'10') } `), handler: "index.handler", runtime: lambda.Runtime.NODEJS_10_X, }); const eventSoruce = myLambda.addEventSourceMapping("test", { eventSourceArn: streamsArn, batchSize: 5, startingPosition: StartingPosition.TRIM_HORIZON, bisectBatchOnError: true, retryAttempts: 10, }); const roleUpdates = myLambda.addToRolePolicy( new iam.PolicyStatement({ actions: [ "dynamodb:DescribeStream", "dynamodb:GetRecords", "dynamodb:GetShardIterator", "dynamodb:ListStreams", ], resources: [streamsArn], }) ); |
- Importing existing DynamoDb into CDK:
We re-write dynamo db with same attributes in cdk, synth to generate Cloudformation and use resource import to import an existing resources into a stack. Here is an SO answer