

# 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://`、资源 `ElasticLoadBalancer` 的 DNS 名称和 `/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)。