

這是新的 *CloudFormation 範本參考指南*。請更新您的書籤和連結。如需 CloudFormation 入門的說明，請參閱 [AWS CloudFormation 使用者指南](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# CloudFormation 資源自訂命名
<a name="aws-properties-name"></a>

可以將自訂名稱指派給 CloudFormation 範本中支援的資源類型，讓其更有意義且更容易識別。預設情況下，CloudFormation 會產生一個唯一的實體 ID，以命名資源。例如，CloudFormation 可能會使用以下實體 ID 命名 Amazon S3 儲存貯體：`MyStack-MyBucket-abcdefghijk1`。使用自訂名稱，您可以指定可更輕鬆閱讀及識別的名稱，例如 `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"
```