AWS CDK 使用现有的 DynamoDB 和流
AWS CDK Working with Existing DynamoDB and Streams
我正在将我的云解决方案迁移到 cdk。我可以看到如何通过 TableProps 在构造函数中将流添加到新的 DynamoDB:
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。
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,使用控制台时是不可能的。
启用流只是 CloudFormation 中资源 'AWS::DynamoDB::Table' 的另一个属性,我认为我们无法更改在堆栈中(或手动)从另一个 cloudformation/cdk 创建的资源堆栈,除非我们导入资源。
Here 是文档。我可以试着总结一下。
假设我们有一个没有元数据资源部署的现有 cdk 项目 cdk --no-version-reporting deploy
假设我们有 Dynamo table 'streaming' 和你所说的分区键 'id'。
在 cdk 代码下方添加与原始 table 相同的属性,如 RCU、WCU、密钥等。为简单起见,我只提供了名称和密钥,并且 removalPolicy 是必须的
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
创建具有正确 table 名称和逻辑 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\":\"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
对于防止意外删除的另一个问题,我们可以简单地添加删除策略以避免在删除 stack/resource 时删除 dynamo table。
removalPolicy: RemovalPolicy.RETAIN
我正在将我的云解决方案迁移到 cdk。我可以看到如何通过 TableProps 在构造函数中将流添加到新的 DynamoDB:
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。
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,使用控制台时是不可能的。
启用流只是 CloudFormation 中资源 'AWS::DynamoDB::Table' 的另一个属性,我认为我们无法更改在堆栈中(或手动)从另一个 cloudformation/cdk 创建的资源堆栈,除非我们导入资源。 Here 是文档。我可以试着总结一下。
假设我们有一个没有元数据资源部署的现有 cdk 项目
cdk --no-version-reporting deploy
假设我们有 Dynamo table 'streaming' 和你所说的分区键 'id'。
在 cdk 代码下方添加与原始 table 相同的属性,如 RCU、WCU、密钥等。为简单起见,我只提供了名称和密钥,并且 removalPolicy 是必须的
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
创建具有正确 table 名称和逻辑 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\":\"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
对于防止意外删除的另一个问题,我们可以简单地添加删除策略以避免在删除 stack/resource 时删除 dynamo table。
removalPolicy: RemovalPolicy.RETAIN