AWS CDK 使用现有的 DynamoDB 和 Streams [英] AWS CDK Working with Existing DynamoDB and Streams
问题描述
我正在将我的云解决方案迁移到 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 表流式传输",分区键为id",如您所说.
Assuming we have Dynamo table 'streaming' with partiion key 'id' as you stated.
在 cdk 代码下面添加与原始表相同的属性,如 RCU、WCU、键等.为简单起见,我只给出了名称和键,removingPolicy 是必须的
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 detect-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
这篇关于AWS CDK 使用现有的 DynamoDB 和 Streams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!