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

问题描述

我正在将云解决方案迁移到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,密钥等.为简单起见,我只给出了名称和密钥,removePolicy是必需的

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创建更改集-堆栈名称HelloCdkStack-更改集名称ImportChangeSet-更改集类型IMPORT-要导入的资源:\"AWS :: DynamoDB :: Table \",\"LogicalResourceId \":\\"dynamotableF6720B98 \",\\"ResourceIdentifier \":{\"TableName \" :: \"; streaming \}}]"--template-body file://cdk.out/HelloCdkStack.template.json

    Create ChangeSet set with right table name and logical idaws 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执行更改集-更改集名ImportChangeSet-堆栈名HelloCdkStack

    最好检查漂移并进行必要的调整 aws cloudformation detect-stack-drift --stack-name HelloCdkStack

    Best to check the drift and make necessary chagnes toaws 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和流一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-17 20:45