monkey
monkey

Reputation: 1597

Add existing dynamodb table with stream to CDK

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:

  1. 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?
  2. 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.
  3. It's worth checking my assumption that these are the only 2 options.

Upvotes: 7

Views: 5472

Answers (1)

Balu Vyamajala
Balu Vyamajala

Reputation: 10333

  1. 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.

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],
  })
);
  1. 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

Upvotes: 9

Related Questions