

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

# CloudFormation 範本 Outputs 語法
<a name="outputs-section-structure"></a>

選用 `Outputs` 區段會宣告堆疊的輸出值。這些輸出值可透過多種方式使用：
+ **擷取資源的重要詳細資訊** – 輸出是擷取資源重要資訊的便捷方式。例如，您可以輸出堆疊的 S3 儲存貯體名稱，以更輕鬆地找到儲存貯體。您可以在 CloudFormation 主控台的**輸出**索引標籤中檢視輸出值，或使用 [describe-stacks](service_code_examples.md#describe-stacks-sdk) CLI 命令檢視輸出值。
+ **跨堆疊參考** – 您可以將輸出值匯入其他堆疊，以[在堆疊之間建立參考](using-cfn-stack-exports.md)。這在您需要跨多個堆疊共用資源或組態時相當實用。

**重要**  
CloudFormation 不會對您在 `Outputs` 區段中包含的任何資訊進行修改或混淆。我們強烈建議您不要使用此區段來輸出敏感資訊，例如密碼或秘密。  
堆疊操作完成後，可使用輸出值。堆疊狀態處於任何 `IN_PROGRESS` [狀態](view-stack-events.md#cfn-console-view-stack-data-resources-status-codes)時，堆疊輸出值將不可用。我們不建議在服務執行階段和堆疊輸出值之間建立相依性，因為輸出值可能無法在任何時候都可用。

## 語法
<a name="outputs-section-syntax"></a>

`Outputs` 區段由索引鍵名稱 `Outputs` 組成。您最多可以在範本中宣告 200 個輸出。

下列範例示範 `Outputs` 區段的結構。

### JSON
<a name="outputs-section-structure-syntax.json"></a>

使用括號括住所有輸出宣告。使用逗號分隔多個輸出。

```
"Outputs" : {
  "OutputLogicalID" : {
    "Description" : "Information about the value",
    "Value" : "Value to return",
    "Export" : {
      "Name" : "Name of resource to export"
    }
  }
}
```

### YAML
<a name="outputs-section-structure-syntax.yaml"></a>

```
Outputs:
  OutputLogicalID:
    Description: Information about the value
    Value: Value to return
    Export:
      Name: Name of resource to export
```

### 輸出欄位
<a name="outputs-section-structure-output-fields"></a>

`Outputs` 區段可以包含下列欄位。

**邏輯 ID (也稱為*邏輯名稱*)**  
目前輸出的識別碼。邏輯 ID 必須是英數字元 (`a–z`、`A–Z`、`0–9`)，而且在範本內必須是唯一的。

**`Description` (選用)**  
`String` 類型，說明輸出值。描述宣告值的長度必須是介於 0 與 1024 位元組之間的常值字串。您不能使用參數或函數來指定描述。

**`Value` (必要)**  
[describe-stacks](service_code_examples.md#describe-stacks-sdk) 命令所傳回的屬性值。輸出值可以包含常值、參數參考、虛擬參數、映射值或內建函數。

**`Export` (選用)**  
要針對跨堆疊參考匯出的資源輸出名稱。  
您可以使用內部函數來自訂匯出的 `Name` 值。  
如需詳細資訊，請參閱[從部署的 CloudFormation 堆疊取得匯出的輸出](using-cfn-stack-exports.md)。

若要建立條件與輸出的關聯，請在範本的 [Conditions](conditions-section-structure.md) 區段中定義條件。

## 範例
<a name="outputs-section-structure-examples"></a>

下列範例說明堆疊輸出的運作方式。

**Topics**
+ [堆疊輸出](#outputs-section-structure-examples-stack-output)
+ [使用 `Fn::Sub` 自訂匯出名稱](#outputs-section-structure-examples-cross-stack)
+ [使用 `Fn::Join` 自訂匯出名稱](#outputs-section-structure-examples-join-export-name)
+ [傳回使用 `Fn::Join` 建構的 URL](#outputs-section-structure-examples-join-export-url)

### 堆疊輸出
<a name="outputs-section-structure-examples-stack-output"></a>

在下列範例中，只有在 `BackupLoadBalancerDNSName` 條件為 true 時，名為 `BackupLoadBalancer` 的輸出才會傳回邏輯 ID 為 `CreateProdResources` 之資源的 DNS 名稱 名為 `InstanceID` 的輸出會傳回邏輯 ID 為 `EC2Instance` 之 EC2 執行個體的 ID。

#### JSON
<a name="outputs-section-structure-example.json"></a>

```
"Outputs" : {
  "BackupLoadBalancerDNSName" : {
    "Description": "The DNSName of the backup load balancer",  
    "Value" : { "Fn::GetAtt" : [ "BackupLoadBalancer", "DNSName" ]},
    "Condition" : "CreateProdResources"
  },
  "InstanceID" : {
    "Description": "The Instance ID",  
    "Value" : { "Ref" : "EC2Instance" }
  }
}
```

#### YAML
<a name="outputs-section-structure-example.yaml"></a>

```
Outputs:
  BackupLoadBalancerDNSName:
    Description: The DNSName of the backup load balancer
    Value: !GetAtt BackupLoadBalancer.DNSName
    Condition: CreateProdResources
  InstanceID:
    Description: The Instance ID
    Value: !Ref EC2Instance
```

### 使用 `Fn::Sub` 自訂匯出名稱
<a name="outputs-section-structure-examples-cross-stack"></a>

在下列範例中，名為 `StackVPC` 的輸出會傳回 VPC 的 ID，然後匯出跨堆疊參考的值，亦即將名稱 `VPCID` 附加至堆疊名稱。

#### JSON
<a name="outputs-section-structure-cross-stack-example.json"></a>

```
"Outputs" : {
  "StackVPC" : {
    "Description" : "The ID of the VPC",
    "Value" : { "Ref" : "MyVPC" },
    "Export" : {
      "Name" : {"Fn::Sub": "${AWS::StackName}-VPCID" }
    }
  }
}
```

#### YAML
<a name="outputs-section-structure-cross-stack-example.yaml"></a>

```
Outputs:
  StackVPC:
    Description: The ID of the VPC
    Value: !Ref MyVPC
    Export:
      Name: !Sub "${AWS::StackName}-VPCID"
```

如需 `Fn::Sub` 函數的詳細資訊，請參閱「[https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-sub.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-sub.html)」。

### 使用 `Fn::Join` 自訂匯出名稱
<a name="outputs-section-structure-examples-join-export-name"></a>

您還可以使用 `Fn::Join` 函數根據參數、資源屬性和其他字串來建構值。

下列範例使用 `Fn::Join` 函數來自訂匯出名稱，而非 `Fn::Sub` 函數。範例 `Fn::Join` 函數使用冒號做為分隔符號，將堆疊名稱與名稱 `VPCID` 串連。

#### JSON
<a name="outputs-section-structure-join-export-name-example.json"></a>

```
"Outputs" : {
  "StackVPC" : {
    "Description" : "The ID of the VPC",
    "Value" : { "Ref" : "MyVPC" },
    "Export" : {
      "Name" : { "Fn::Join" : [ ":", [ { "Ref" : "AWS::StackName" }, "VPCID" ] ] }
    }
  }
}
```

#### YAML
<a name="outputs-section-structure-join-export-name-example.yaml"></a>

```
Outputs:
  StackVPC:
    Description: The ID of the VPC
    Value: !Ref MyVPC
    Export:
      Name: !Join [ ":", [ !Ref "AWS::StackName", VPCID ] ]
```

如需 `Fn::Join` 函數的詳細資訊，請參閱「[https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-join.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-join.html)」。

### 傳回使用 `Fn::Join` 建構的 URL
<a name="outputs-section-structure-examples-join-export-url"></a>

在建立 WordPress 網站的範本的下列範例中，`InstallURL` 是 `Fn::Join` 函數呼叫傳回的字串，它會串連 `http://`、資源的 DNS 名稱 `ElasticLoadBalancer`，以及 `/wp-admin/install.php`。輸出值可能類似以下內容：

```
http://mywptests-elasticl-1gb51l6sl8y5v-206169572.aws-region.elb.amazonaws.com/wp-admin/install.php
```

#### JSON
<a name="outputs-section-structure-examples-join-export-url.json"></a>

```
{
    "Outputs": {
        "InstallURL": {
            "Value": {
                "Fn::Join": [
                    "",
                    [
                        "http://",
                        {
                            "Fn::GetAtt": [
                                "ElasticLoadBalancer",
                                "DNSName"
                            ]
                        },
                        "/wp-admin/install.php"
                    ]
                ]
            },
            "Description": "Installation URL of the WordPress website"
        }
    }
}
```

#### YAML
<a name="outputs-section-structure-examples-join-export-url.yaml"></a>

```
Outputs:
  InstallURL:
    Value: !Join 
      - ''
      - - 'http://'
        - !GetAtt 
          - ElasticLoadBalancer
          - DNSName
        - /wp-admin/install.php
    Description: Installation URL of the WordPress website
```

如需 `Fn::Join` 函數的詳細資訊，請參閱「[https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-join.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference-join.html)」。