

# 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` で構成されます。1 つのテンプレートで宣言できる出力は最大 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` という名前の出力によって、論理 ID が `BackupLoadBalancer` であるリソースの DNS 名が `CreateProdResources` 条件が true の場合に返されます。`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::Sub` 関数の代わりに `Fn::Join` 関数を使用してエクスポート名をカスタマイズします。この `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` は、`http://` 、リソース `ElasticLoadBalancer` の DNS 名、`/wp-admin/install.php` を連結する `Fn::Join` 関数呼び出しによって返される文字列です。出力値は次の例のようになります。

```
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)」を参照してください。