Importing existing DDBs in AWS account to be managed by AWS CDK

0

Hi,

I have created an empty CDK pipeline and package. In the CDK package, I created a deployment stack for Kinesis stream which created 3 new kinesis streams. There are 3 DDBs existing in the AWS account to which each one of these kinesis streams has to be made consumers of. I can integrate the DDBs with the kinesis stream using the UI but I want to import the DDBs into a CDK stack and add kinesisStream from TableOptionsV2.

To do the above, I followed the AWS guide for cdk import: https://docs.aws.amazon.com/cdk/v2/guide/cli.html#cli-import

  1. Created an empty stack DDBStack.ts and deployed it to CloudFormation using brazil-build cdk deploy DDStack-beta
export class DDBStack extends DeploymentStack {
  constructor(app: Construct, id: string, props: DDBStackProps) {
    super(app, id, props);
  }
}
  1. Created a CDK construct in that stack for 1st DDB with the same existing model as present in UI and did cdk-import (had to provide the name of the DDB to be imported). This created the DDB resource and imported the existing DDB into it.
interface DDBStackProps extends DeploymentStackProps {
  kinesisStreamForMyDDB: IStream;
}

export class DDBStack extends DeploymentStack {
  MyDDB: TableV2;

  constructor(app: Construct, id: string, props: DDBStackProps) {
    super(app, id, props);
    const { kinesisStreamForMyDDB } = props;

    this.MyDDB = new TableV2(this, 'MyDDB', {
      partitionKey: { name: 'PK', type: AttributeType.STRING },
      sortKey: { name: 'SK', type: AttributeType.STRING },
      pointInTimeRecovery: true,
    });
  }
}

  1. Added kinesisStream from TableOptionsV2 to the DDB construct props and deployed the stack again. Now, the kinesis stream was added as a consumer for that DDB
this.MyDDB = new TableV2(this, 'MyDDB', {
  partitionKey: { name: 'PK', type: AttributeType.STRING },
      sortKey: { name: 'SK', type: AttributeType.STRING },
      pointInTimeRecovery: true,
      kinesisStream: kinesisStreamForMyDDB,
});
  1. Repeated steps 2-3 for the rest of the 2 DDBs.

Now, when I merge my code and it gets propagated thru the CDK pipeline to Prod, the DDBStack-prod stack will be deployed in the prod AWS account with the 3 DDBs and attached kinesis streams. But, the stack will not import the existing DDBs present in AWS account because cdk import was never run before the stack was deployed. How do I mimic the above workflow for Prod?

1 Respuesta
1
Respuesta aceptada

Have figured it out. Simply raising 2 consecutive CRs solves the problem. The first CR will deploy the empty stack from step-1. Then, DDB constructs are added to the empty stack and run cdk import DDStack which will import the existing DDBs into the stack. Next, we add kinesisStream prop to the constructs and raise another CR. Post CR-merge, kinesis stream will be attached to the existing DDBs.

respondido hace 3 meses
profile picture
EXPERTO
revisado hace 3 meses

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas