You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过CloudFormation YAML启用已有DynamoDB表的Stream且不重建表?

不重建DynamoDB表启用流的CloudFormation解决方案

可以在不重建表、不丢失数据的前提下,通过CloudFormation启用DynamoDB流。你遇到的Resource of type 'AWS::DynamoDB::Table' with identifier 'Users' already exists错误,是因为现有表未被当前CloudFormation栈管理,直接在模板中添加该表资源会触发创建操作,但表名已存在导致冲突。

解决步骤:

  1. 将现有DynamoDB表导入CloudFormation栈
    只有被栈管理的资源才能通过更新栈来修改属性。操作如下:

    • 登录CloudFormation控制台,打开目标栈,点击导入资源。
    • 选择资源类型为AWS::DynamoDB::Table,输入表名Users完成导入。
    • 也可通过AWS CLI执行导入:
      aws cloudformation import-resources --stack-name 你的栈名 --resources file://import-config.json
      
      其中import-config.json内容为:
      [
        {
          "ResourceType": "AWS::DynamoDB::Table",
          "LogicalResourceId": "Users",
          "ResourceIdentifier": {
            "TableName": "Users"
          }
        }
      ]
      
  2. 更新栈以启用流
    确保模板中表的所有现有属性(KeySchema、AttributeDefinitions、BillingMode等)与线上表完全一致,添加StreamSpecification后执行栈更新。修正后的模板如下:

    AWSTemplateFormatVersion: 2010-09-09
    Transform: AWS::Serverless-2016-10-31
    Description: Stream
    
    Resources:
      Users:
        Type: AWS::DynamoDB::Table
        Properties:
          StreamSpecification:
            StreamViewType: NEW_IMAGE
          KeySchema:
            - AttributeName: name
              KeyType: HASH
            - AttributeName: city
              KeyType: RANGE
          AttributeDefinitions:
            - AttributeName: name
              AttributeType: S
            - AttributeName: city
              AttributeType: S
          BillingMode: PAY_PER_REQUEST
          TableName: Users
        UpdateReplacePolicy: Retain
        DeletionPolicy: Retain
    

关键注意事项:

  • 模板中表的属性必须与线上现有表完全匹配,否则栈更新会失败。
  • UpdateReplacePolicy和DeletionPolicy设为Retain可确保更新过程中不会意外删除或替换表,保障数据安全。

内容的提问来源于stack exchange,提问作者Caique Freitas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 03:23:19