

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

# 一般範本程式碼片段
<a name="quickref-general"></a>

以下範例說明非 AWS 服務專屬的不同 CloudFormation 範本功能。

**Topics**
+ [經 Base64 編碼的 UserData 屬性](#scenario-userdata-base64)
+ [經 Base64 編碼的 UserData 屬性，包含 AccessKey 和 SecretKey](#scenario-userdata-base64-with-keys)
+ [具有一個文字字串參數的 Parameters 區段](#scenario-one-string-parameter)
+ [字串參數使用一般表達式限制條件的 Parameters 區段](#scenario-constraint-string-parameter)
+ [數值參數使用 MinValue 和 MaxValue 限制條件的 Parameters 區段](#scenario-one-number-min-parameter)
+ [數值參數使用 AllowedValues 限制條件的 Parameters 區段](#scenario-one-number-parameter)
+ [具有一個文字 CommaDelimitedList 參數的 Parameters 區段](#scenario-one-list-parameter)
+ [參數值以虛擬參數為基礎的 Parameters 區段](#scenario-one-pseudo-parameter)
+ [具有三個映射的 Mapping 區段](#scenario-mapping-with-four-maps)
+ [根據常值字串的 Description](#scenario-description-from-literal-string)
+ [具有一個文字字串輸出的 Outputs 區段](#scenario-output-with-literal-string)
+ [具有一個資源參考和一個虛擬參考輸出的 Outputs 區段](#scenario-output-with-ref-and-pseudo-ref)
+ [具有輸出的 Outputs 區段，此輸出以函數、文字字串、參考和虛擬參數為基礎](#scenario-output-with-complex-spec)
+ [範本格式版本](#scenario-format-version)
+ [AWS Tags 屬性](#scenario-format-aws-tag)

## 經 Base64 編碼的 UserData 屬性
<a name="scenario-userdata-base64"></a>

此範例示範使用 `Fn::Base64` 和 `Fn::Join` 函數的 `UserData` 屬性。參考 `MyValue` 和 `MyName` 是必須在範本 `Parameters` 區段中定義的參數。文字字串 `Hello World` 只是本範例傳入，屬於 `UserData` 的另一個值。

### JSON
<a name="quickref-general-example-1.json"></a>

```
1. "UserData" : {
2.     "Fn::Base64" : {
3.         "Fn::Join" : [ ",", [
4.             { "Ref" : "MyValue" },
5.             { "Ref" : "MyName" },
6.             "Hello World" ] ]
7.     }
8. }
```

### YAML
<a name="quickref-general-example-1.yaml"></a>

```
1. UserData:
2.   Fn::Base64: !Sub |
3.      Ref: MyValue
4.      Ref: MyName
5.      Hello World
```

## 經 Base64 編碼的 UserData 屬性，包含 AccessKey 和 SecretKey
<a name="scenario-userdata-base64-with-keys"></a>

此範例示範使用 `Fn::Base64` 和 `Fn::Join` 函數的 `UserData` 屬性。它包含 `AccessKey` 和 `SecretKey` 資訊。參考 `AccessKey` 和 `SecretKey` 是必須在範本 Parameters (參數) 區段中定義的參數。

### JSON
<a name="quickref-general-example-2.json"></a>

```
1. "UserData" : {
2.     "Fn::Base64" : {
3.         "Fn::Join" : [ "", [
4.             "ACCESS_KEY=", { "Ref" : "AccessKey" },
5.             "SECRET_KEY=", { "Ref" : "SecretKey" } ]
6.         ]
7.     }
8. }
```

### YAML
<a name="quickref-general-example-2.yaml"></a>

```
1. UserData:
2.   Fn::Base64: !Sub |
3.      ACCESS_KEY=${AccessKey}
4.      SECRET_KEY=${SecretKey}
```

## 具有一個文字字串參數的 Parameters 區段
<a name="scenario-one-string-parameter"></a>

下列範例說明有效的 Parameters (參數) 區段宣告，在此宣告單一 `String` 類型參數。

### JSON
<a name="quickref-general-example-3.json"></a>

```
1. "Parameters" : {
2.     "UserName" : {
3.         "Type" : "String",
4.         "Default" : "nonadmin",
5.         "Description" : "Assume a vanilla user if no command-line spec provided"
6.     }
7. }
```

### YAML
<a name="quickref-general-example-3.yaml"></a>

```
1. Parameters:
2.   UserName:
3.     Type: String
4.     Default: nonadmin
5.     Description: Assume a vanilla user if no command-line spec provided
```

## 字串參數使用一般表達式限制條件的 Parameters 區段
<a name="scenario-constraint-string-parameter"></a>

下列範例說明有效的 Parameters (參數) 區段宣告，在此宣告單一 `String` 類型參數。`AdminUserAccount` 參數的預設值為 `admin`。此參數值長度下限為 1 個字元、長度上限為 16 個字元，而且包含英數字元，但必須以字母字元開頭。

### JSON
<a name="quickref-general-example-4.json"></a>

```
 1. "Parameters" : {
 2.     "AdminUserAccount": {
 3.       "Default": "admin",
 4.       "NoEcho": "true",
 5.       "Description" : "The admin account user name",
 6.       "Type": "String",
 7.       "MinLength": "1",
 8.       "MaxLength": "16",
 9.       "AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*"
10.     }
11. }
```

### YAML
<a name="quickref-general-example-4.yaml"></a>

```
1. Parameters:
2.   AdminUserAccount:
3.     Default: admin
4.     NoEcho: true
5.     Description: The admin account user name
6.     Type: String
7.     MinLength: 1
8.     MaxLength: 16
9.     AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
```

## 數值參數使用 MinValue 和 MaxValue 限制條件的 Parameters 區段
<a name="scenario-one-number-min-parameter"></a>

下列範例說明有效的 Parameters (參數) 區段宣告，在此宣告單一 `Number` 類型參數。`WebServerPort` 參數的預設值為 80，最小值 1 和最大值 65535。

### JSON
<a name="quickref-general-example-5.json"></a>

```
1. "Parameters" : {
2.     "WebServerPort": {
3.       "Default": "80",
4.       "Description" : "TCP/IP port for the web server",
5.       "Type": "Number",
6.       "MinValue": "1",
7.       "MaxValue": "65535"
8.     }
9. }
```

### YAML
<a name="quickref-general-example-5.yaml"></a>

```
1. Parameters:
2.   WebServerPort:
3.     Default: 80
4.     Description: TCP/IP port for the web server
5.     Type: Number
6.     MinValue: 1
7.     MaxValue: 65535
```

## 數值參數使用 AllowedValues 限制條件的 Parameters 區段
<a name="scenario-one-number-parameter"></a>

下列範例說明有效的 Parameters (參數) 區段宣告，在此宣告單一 `Number` 類型參數。`WebServerPort` 參數的預設值為 80，僅允許值 80 和 8888。

### JSON
<a name="quickref-general-example-6.json"></a>

```
1. "Parameters" : {
2.     "WebServerPortLimited": {
3.       "Default": "80",
4.       "Description" : "TCP/IP port for the web server",
5.       "Type": "Number",
6.       "AllowedValues" : ["80", "8888"]
7.     }
8. }
```

### YAML
<a name="quickref-general-example-6.yaml"></a>

```
1. Parameters:
2.   WebServerPortLimited:
3.     Default: 80
4.     Description: TCP/IP port for the web server
5.     Type: Number
6.     AllowedValues:
7.     - 80
8.     - 8888
```

## 具有一個文字 CommaDelimitedList 參數的 Parameters 區段
<a name="scenario-one-list-parameter"></a>

下列範例說明有效的 `Parameters` 區段宣告，在此宣告單一 `CommaDelimitedList` 類型參數。`NoEcho` 屬性被設定為 `TRUE`，這將在 **describe-stacks** 輸出中使用星號 (\$1\$1\$1\$1\$1) 遮罩其值，但儲存在下列指定位置的資訊除外。

**重要**  
使用 `NoEcho` 屬性不會遮罩任何儲存在下列資訊中的資訊：  
`Metadata` 範本區段。CloudFormation 不會轉換、修改或標記您在 `Metadata` 區段中包含的任何資訊。若要取得更多資訊，請參閱[中繼資料](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html)。
`Outputs` 範本區段。如需詳細資訊，請參閱[輸出](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html)。
資源定義的 `Metadata` 屬性。如需詳細資訊，請參閱 [`Metadata` 屬性](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-metadata.html)。
我們強烈建議您不要使用這些機制來包含敏感資訊，例如密碼或秘密。

**重要**  
我們建議您不要直接在 CloudFormation 範本中嵌入敏感資訊，而是在堆疊範本中使用動態參數來參考在 CloudFormation 外部存放和管理的敏感資訊，例如在 AWS Systems Manager 參數存放區或 中 AWS Secrets Manager。  
如需詳細資訊，請參閱[請勿在範本中內嵌認證](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/security-best-practices.html#creds)的最佳實務。

### JSON
<a name="quickref-general-example-7.json"></a>

```
1. "Parameters" : {
2.     "UserRoles" : {
3.         "Type" : "CommaDelimitedList",
4.         "Default" : "guest,newhire",
5.         "NoEcho" : "TRUE"
6.     }
7. }
```

### YAML
<a name="quickref-general-example-7.yaml"></a>

```
1. Parameters:
2.   UserRoles:
3.     Type: CommaDelimitedList
4.     Default: "guest,newhire"
5.     NoEcho: true
```

## 參數值以虛擬參數為基礎的 Parameters 區段
<a name="scenario-one-pseudo-parameter"></a>

以下範例顯示 EC2 使用者資料中使用虛擬參數 `AWS::StackName` 和 `AWS::Region` 的命令。如需這些虛擬參數的詳細資訊，請參閱[使用虛擬參數取得 AWS 值](pseudo-parameter-reference.md)。

### JSON
<a name="quickref-general-example-10.json"></a>

```
 1.           "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
 2.              "#!/bin/bash -xe\n",
 3.              "yum install -y aws-cfn-bootstrap\n",
 4. 
 5.              "/opt/aws/bin/cfn-init -v ",
 6.              "         --stack ", { "Ref" : "AWS::StackName" },
 7.              "         --resource LaunchConfig ",
 8.              "         --region ", { "Ref" : "AWS::Region" }, "\n",
 9. 
10.              "/opt/aws/bin/cfn-signal -e $? ",
11.              "         --stack ", { "Ref" : "AWS::StackName" },
12.              "         --resource WebServerGroup ",
13.              "         --region ", { "Ref" : "AWS::Region" }, "\n"
14.         ]]}}
15.       }
```

### YAML
<a name="quickref-general-example-10.yaml"></a>

```
1. UserData:
2.   Fn::Base64: !Sub |
3.      #!/bin/bash -xe
4.      yum update -y aws-cfn-bootstrap
5.      /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource LaunchConfig --region ${AWS::Region}
6.      /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerGroup --region ${AWS::Region}
```

## 具有三個映射的 Mapping 區段
<a name="scenario-mapping-with-four-maps"></a>

以下範例說明包含三個映射之有效的 `Mapping` 區段宣告。符合 `Stop`、`SlowDown` 或 `Go` 映射金鑰時，映射會提供指派給所對應 `RGBColor` 屬性的 RGB 值。

### JSON
<a name="quickref-general-example-11.json"></a>

```
 1. "Mappings" : {
 2.     "LightColor" : {
 3.         "Stop" : {
 4.             "Description" : "red",
 5.             "RGBColor" : "RED 255 GREEN 0 BLUE 0"
 6.         },
 7.         "SlowDown" : {
 8.             "Description" : "yellow",
 9.             "RGBColor" : "RED 255 GREEN 255 BLUE 0"
10.         },
11.         "Go" : {
12.             "Description" : "green",
13.             "RGBColor" : "RED 0 GREEN 128 BLUE 0"
14.         }
15.     }
16. }
```

### YAML
<a name="quickref-general-example-11.yaml"></a>

```
 1. Mappings:
 2.   LightColor:
 3.     Stop:
 4.       Description: red
 5.       RGBColor: "RED 255 GREEN 0 BLUE 0"
 6.     SlowDown:
 7.       Description: yellow
 8.       RGBColor: "RED 255 GREEN 255 BLUE 0"
 9.     Go:
10.       Description: green
11.       RGBColor: "RED 0 GREEN 128 BLUE 0"
```

## 根據常值字串的 Description
<a name="scenario-description-from-literal-string"></a>

以下範例說明有效的 `Description` 區段宣告，其值是以文字字串為基礎。此程式碼片段適用於範本、參數、資源、屬性或輸出。

### JSON
<a name="quickref-general-example-8.json"></a>

```
1. "Description" : "Replace this value"
```

### YAML
<a name="quickref-general-example-8.yaml"></a>

```
1. Description: "Replace this value"
```

## 具有一個文字字串輸出的 Outputs 區段
<a name="scenario-output-with-literal-string"></a>

此範例示範以文字字串為基礎的輸出指派。

### JSON
<a name="quickref-general-example-12.json"></a>

```
1. "Outputs" : {
2.     "MyPhone" : {
3.         "Value" : "Please call 555-5555",
4.         "Description" : "A random message for aws cloudformation describe-stacks"
5.     }
6. }
```

### YAML
<a name="quickref-general-example-12.yaml"></a>

```
1. Outputs:
2.   MyPhone:
3.     Value: Please call 555-5555
4.     Description: A random message for aws cloudformation describe-stacks
```

## 具有一個資源參考和一個虛擬參考輸出的 Outputs 區段
<a name="scenario-output-with-ref-and-pseudo-ref"></a>

此範例示範具有兩個輸出指派的 `Outputs` 區段。一個以資源為基礎，另一個以虛擬參考為基礎。

### JSON
<a name="quickref-general-example-13.json"></a>

```
1. "Outputs" : {
2.    "SNSTopic" : { "Value" : { "Ref" : "MyNotificationTopic" } },
3.    "StackName" : { "Value" : { "Ref" : "AWS::StackName" } }
4. }
```

### YAML
<a name="quickref-general-example-13.yaml"></a>

```
1. Outputs:
2.   SNSTopic:
3.     Value: !Ref MyNotificationTopic
4.   StackName:
5.     Value: !Ref AWS::StackName
```

## 具有輸出的 Outputs 區段，此輸出以函數、文字字串、參考和虛擬參數為基礎
<a name="scenario-output-with-complex-spec"></a>

此範例示範具有一個輸出指派的 Outputs (輸出) 區段。使用 Join 函數串連值，百分比符號為分隔符號。

### JSON
<a name="quickref-general-example-14.json"></a>

```
1. "Outputs" : {
2.     "MyOutput" : {
3.         "Value" : { "Fn::Join" :
4.             [ "%", [ "A-string", {"Ref" : "AWS::StackName" } ] ]
5.         }
6.     }
7. }
```

### YAML
<a name="quickref-general-example-14.yaml"></a>

```
1. Outputs:
2.   MyOutput:
3.     Value: !Join [ %, [ 'A-string', !Ref 'AWS::StackName' ]]
```

## 範本格式版本
<a name="scenario-format-version"></a>

下列程式碼片段說明有效的 `AWSTemplateFormatVersion` 區段宣告。

### JSON
<a name="quickref-general-example-9.json"></a>

```
1. "AWSTemplateFormatVersion" : "2010-09-09"
```

### YAML
<a name="quickref-general-example-9.yaml"></a>

```
1. AWSTemplateFormatVersion: '2010-09-09'
```

## AWS Tags 屬性
<a name="scenario-format-aws-tag"></a>

此範例顯示 屬性 AWS `Tags`。您可以在資源的 Properties (屬性) 區段內指定此屬性。資源建立後，會以您宣告的標籤標記。

### JSON
<a name="quickref-general-example-15.json"></a>

```
 1. "Tags" : [
 2.       {
 3.         "Key" : "keyname1",
 4.         "Value" : "value1"
 5.       },
 6.       {
 7.         "Key" : "keyname2",
 8.         "Value" : "value2"
 9.       }
10.     ]
```

### YAML
<a name="quickref-general-example-15.yaml"></a>

```
1. Tags: 
2.   - 
3.     Key: "keyname1"
4.     Value: "value1"
5.   - 
6.     Key: "keyname2"
7.     Value: "value2"
```