使用现有 DynamoDB 和流的 AWS CDK [英] AWS CDK Working with Existing DynamoDB and Streams

查看:28
本文介绍了使用现有 DynamoDB 和流的 AWS CDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的云解决方案迁移到 cdk.我可以看到如何通过 TableProps 在构造函数中向新的 DynamoDB 添加流:

I'm migrating my cloud solution to cdk. I can see how to add a stream to a new DynamoDB in the constructor through the TableProps:

const newTable = new dynamodb.Table(this, 'new Table', {
  tableName: 'streaming',
  partitionKey: { name : 'id', type: dynamodb.AttributeType.NUMBER },
  stream: StreamViewType.NEW_AND_OLD_IMAGES,
})

但是没有明显的方法可以在现有的 DynamoDB 上启用流.我似乎无法访问现有项目的 TableProps.

but there is no apparent way to enable a stream on an existing DynamoDB. I can't seem to access the TableProps on an existing item.

const sandpitTable = dynamodb.Table.fromTableArn(this, 'sandpitTable', 'arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxx:table/Sandpit');
sandpitTable.grantStreamRead(streamLambda);
// sandpitTable.  ??? what to do?

如何实现?以及该解决方案如何考虑灾难恢复并防止使用控制台时无法意外删除Dynamo DB.

How can this be achieved? And how does the solution take into account disaster recovery and prevent accidental deletion of the Dynamo DB that is not possible when using the console.

推荐答案

启用流只是 CloudFormation 中资源AWS::DynamoDB::Table"的另一个属性,我不相信我们可以对资源进行更改除非我们导入资源,否则是在另一个 cloudformation/cdk 堆栈的堆栈中(或手动)创建的.这里是文档.我可以试着总结一下.

Enabling streams is just another attribute of resource 'AWS::DynamoDB::Table' in CloudFormation and I don't believe we can make changes to a resource that is created in a stack (or manually) from another cloudformation/cdk stack unless we import the resource. Here is documentation. I can try and summarize.

  • 假设我们有一个现有的 cdk 项目,它在没有元数据资源的情况下部署 cdk --no-version-reporting deploy

假设我们有 Dynamo 表 'streaming',分区键为 'id',如您所说.

Assuming we have Dynamo table 'streaming' with partiion key 'id' as you stated.

在下面添加与原始表相同属性的 cdk 代码,如 RCU、WCU、键等.为简单起见,我只给出了名称和键,并且必须使用删除策略

Adding below cdk code with same attributes of original table like RCU, WCU, keys, etc. For simplicity I just gave name and key and removalPolicy is must

const myTable = new dynamodb.Table(this, "dynamo-table", {
  tableName: "streaming",
   partitionKey: { name: "id", type: dynamodb.AttributeType.NUMBER },
   removalPolicy: cdk.RemovalPolicy.RETAIN,
});

  • 我们现在可以默认将 CloudFormation 合成并生成到 cdk.out 文件夹中 cdk --no-version-reporting synth

    从 .json 文件中获取逻辑 ID,在我的情况下它是 dynamotableF6720B98

    Grab the logical Id from .json file in my case it is dynamotableF6720B98

    使用正确的表名和逻辑 ID 创建 ChangeSet 集aws cloudformation create-change-set --stack-name HelloCdkStack --change-set-name ImportChangeSet --change-set-type IMPORT --resources-to-import "[{"ResourceType";:"AWS::DynamoDB::Table","LogicalResourceId":"dynamotableF6720B98","ResourceIdentifier":{"TableName":";流媒体}}]"--template-body file://cdk.out/HelloCdkStack.template.json

    Create ChangeSet set with right table name and logical id aws cloudformation create-change-set --stack-name HelloCdkStack --change-set-name ImportChangeSet --change-set-type IMPORT --resources-to-import "[{"ResourceType":"AWS::DynamoDB::Table","LogicalResourceId":"dynamotableF6720B98","ResourceIdentifier":{"TableName":"streaming"}}]" --template-body file://cdk.out/HelloCdkStack.template.json

    执行变更集

    aws cloudformation execute-change-set --change-set-name ImportChangeSet --stack-name HelloCdkStack

    最好检查漂移并进行必要的修改aws cloudformation detection-stack-drift --stack-name HelloCdkStack

    Best to check the drift and make necessary chagnes to aws cloudformation detect-stack-drift --stack-name HelloCdkStack

    对于防止意外删除的另一个问题,我们可以简单地添加删除策略,以避免在删除堆栈/资源时删除发电机表.

    To your other question of preventing accidental deletion, we can simply add deletion policy to avoid dynamo table getting deleted when stack/resource is deleted.

    removalPolicy: RemovalPolicy.RETAIN
    

    这篇关于使用现有 DynamoDB 和流的 AWS CDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆