내용으로 건너뛰기

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개 답변
1
수락된 답변

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.

답변함 일 년 전
전문가
검토됨 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.