

这是新的《CloudFormation 模板参考指南》**。请更新您的书签和链接。有关开始使用 CloudFormation 的帮助，请参阅《AWS CloudFormation 用户指南》[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

# CloudFormation 资源自定义命名
<a name="aws-properties-name"></a>

您可以在 CloudFormation 模板中为支持的资源类型分配自定义名称，以使其更有意义且易于识别。默认情况下，CloudFormation 会生成唯一物理 ID 来命名资源。举例来说，CloudFormation 可能会使用物理 ID `MyStack-MyBucket-abcdefghijk1` 来命名 Amazon S3 桶。借助自定义名称，您可以指定一个更易于读取和识别的名称，如 `production-app-logs` 或 `business-metrics`。

并非所有资源都支持自定义名称。每个 AWS 服务独立决定哪些资源类型支持自定义名称。

资源名称在您的所有活动堆栈中必须是唯一的。如果您重复使用模板来创建多个堆栈，则必须从模板来更改或删除自定义名称。如果您没有指定名称，则 CloudFormation 将生成一个唯一物理 ID 来为资源命名。名称必须以字母开头，只能包含 ASCII 字母、数字和连字符，并且不能以连字符结尾或包含两个连续的连字符。

此外，请勿管理 CloudFormation 外部的堆栈资源。举例来说，如果重命名一个资源，该资源属于未使用 CloudFormation 的堆栈，每次当您尝试更新或删除该堆栈时，都可能发生错误。

**重要**  
您无法执行导致自定义命名的资源被更换的更新。如果必须替换资源，请指定新名称。

## 示例
<a name="aws-properties-name-example"></a>

如果您想要使用自定义名称，请在 CloudFormation 模板中为该资源指定名称属性。支持自定义名称的每个资源都具有可以指定的自身属性。例如，要命名一个 DynamoDB 表，您可以使用 `TableName` 属性，如以下示例所示：

### JSON
<a name="aws-properties-name-example.json"></a>

```
"myDynamoDBTable" : {
   "Type" : "AWS::DynamoDB::Table",
   "Properties" : {
      "KeySchema" : {
         "HashKeyElement": {
            "AttributeName" : "AttributeName1",
            "AttributeType" : "S"
         },
         "RangeKeyElement" : {
            "AttributeName" : "AttributeName2",
            "AttributeType" : "N"
         }
      },
      "ProvisionedThroughput" : {
         "ReadCapacityUnits" : "5",
         "WriteCapacityUnits" : "10"
      },
      "TableName" : "SampleTable"
   }
}
```

### YAML
<a name="aws-properties-name-example.yaml"></a>

```
myDynamoDBTable: 
  Type: AWS::DynamoDB::Table
  Properties: 
    KeySchema: 
      HashKeyElement: 
        AttributeName: "AttributeName1"
        AttributeType: "S"
      RangeKeyElement: 
        AttributeName: "AttributeName2"
        AttributeType: "N"
    ProvisionedThroughput: 
      ReadCapacityUnits: "5"
      WriteCapacityUnits: "10"
    TableName: "SampleTable"
```