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

如何在CloudFormation模板中为多个DynamoDB表复用相同标签

CloudFormation 多资源标签复用实现方案

你当前写法无法生效的核心原因:

  • Ref 函数仅能返回资源的默认标识符,DynamoDB表调用Ref会返回表名,无法直接读取资源的Tags属性
  • 即使使用Fn::GetAtt读取资源属性,AWS::DynamoDB::Table类型的支持返回值中也不包含Tags字段,因此无法通过引用已有资源的标签实现复用。

正确实现方案

你可以选择以下任意一种方案实现标签复用:

方案1:模板内公共标签映射(适用JSON/YAML模板)

将通用标签定义在Mappings段,所有资源通过Fn::FindInMap引用即可,修改标签时仅需修改映射段一处即可全局生效,JSON模板示例如下:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Mappings": {
    "CommonTagConfig": {
      "GlobalTags": {
        "Application": "BFMS",
        "Name": "EventSourcingDataStore"
      }
    }
  },
  "Resources": {
    "Status": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "AttributeDefinitions": [
          {
            "AttributeName": "SId",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "SId",
            "KeyType": "HASH"
          }
        ],
        "ProvisionedThroughput": {
          "ReadCapacityUnits": "1",
          "WriteCapacityUnits": "1"
        },
        "TableName": "Statuscf",
        "Tags": [
          {
            "Key": "Application",
            "Value": {"Fn::FindInMap": ["CommonTagConfig", "GlobalTags", "Application"]}
          },
          {
            "Key": "Name",
            "Value": {"Fn::FindInMap": ["CommonTagConfig", "GlobalTags", "Name"]}
          }
        ]
      }
    },
    "BMSHSData": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "AttributeDefinitions": [
          {
            "AttributeName": "你的主键字段名",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "你的主键字段名",
            "KeyType": "HASH"
          }
        ],
        "ProvisionedThroughput": {
          "ReadCapacityUnits": "1",
          "WriteCapacityUnits": "1"
        },
        "TableName": "Billing.FmsDatacf",
        "Tags": [
          {
            "Key": "Application",
            "Value": {"Fn::FindInMap": ["CommonTagConfig", "GlobalTags", "Application"]}
          },
          {
            "Key": "Name",
            "Value": {"Fn::FindInMap": ["CommonTagConfig", "GlobalTags", "Name"]}
          }
        ]
      }
    }
  }
}

方案2:YAML模板锚点复用(仅适用YAML格式模板)

如果使用YAML编写模板,可以直接用YAML原生的锚点语法复用标签段,写法更简洁:

AWSTemplateFormatVersion: 2010-09-09
Resources:
  Status:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: SId
          AttributeType: S
      KeySchema:
        - AttributeName: SId
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
      TableName: Statuscf
      Tags: &commonTags # 定义锚点
        - Key: Application
          Value: BFMS
        - Key: Name
          Value: EventSourcingDataStore
  BMSHSData:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: 你的主键字段名
          AttributeType: S
      KeySchema:
        - AttributeName: 你的主键字段名
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
      TableName: Billing.FmsDatacf
      Tags: *commonTags # 引用锚点直接复用标签

方案3:Stack全局标签

如果你的所有资源都需要挂载这组标签,可以在创建CloudFormation Stack的时候直接指定全局标签,CloudFormation会自动将全局标签挂载到所有支持标签的资源上,不需要在模板内单独配置每个资源的Tags字段。


内容的提问来源于stack exchange,提问作者90-211456

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 10:18:03