

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

# 函數
<a name="ebextensions-functions"></a>

針對來自其他資源或來自 Elastic Beanstalk 組態選項設定之資訊的資源屬性，您可使用組態檔案中的函數產生其值。Elastic Beanstalk 支援 CloudFormation 函數 (`Ref`、`Fn::GetAtt`、`Fn::Join`) 和一個 Elastic Beanstalk 特定函數 `Fn::GetOptionSetting`。

**Topics**
+ [Ref](#ebextensions-functions-ref)
+ [Fn::GetAtt](#ebextensions-functions-getatt)
+ [Fn::Join](#ebextensions-functions-join)
+ [Fn::GetOptionSetting](#ebextensions-functions-getoptionsetting)

## Ref
<a name="ebextensions-functions-ref"></a>

使用 `Ref` 擷取 AWS 資源的預設字串表示法。`Ref` 回傳的值取決於資源類型，有時則視其他因素而異。例如，安全群組 ([AWS::EC2::SecurityGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html)) 會傳回安全群組的名稱或 ID，取決於安全群組是在預設的 [Amazon Virtual Private Cloud](https://docs.aws.amazon.com/vpc/latest/userguide/) (Amazon VPC)、EC2 classic 或在自訂 VPC 中。

```
{ "Ref" : "resource name" }
```

**注意**  
如需各個資源類型的詳細資訊 (包括 `Ref` 的傳回值)，請參閱《*CloudFormation 使用者指南*》中的 [AWS 資源類型參考](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html)。

在範例 [Auto Scaling 生命週期關聯](environment-resources.md)中：

```
Resources:
  lifecyclehook:
    Type: AWS::AutoScaling::LifecycleHook
    Properties:
      AutoScalingGroupName: { "Ref" : "AWSEBAutoScalingGroup" }
```

您也可以使用 `Ref` 來擷取相同檔案或不同組態檔案中其他位置定義的 CloudFormation 參數值。

## Fn::GetAtt
<a name="ebextensions-functions-getatt"></a>

使用 `Fn::GetAtt`擷取 AWS 資源上屬性的值。

```
{ "Fn::GetAtt" : [ "resource name", "attribute name"] }
```

在範例 [Auto Scaling 生命週期關聯](environment-resources.md)中：

```
Resources:
  lifecyclehook:
    Type: AWS::AutoScaling::LifecycleHook
    Properties:
      RoleARN: { "Fn::GetAtt" : [ "hookrole", "Arn"] }
```

如需詳細資訊，請參閱 [Fn::GetAtt](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html)。

## Fn::Join
<a name="ebextensions-functions-join"></a>

使用 `Fn::Join` 來結合帶有分隔符號的字串。字串可為硬式編碼，或使用 `Fn::GetAtt` 或 `Ref` 的輸出。

```
{ "Fn::Join" : [ "delimiter", [ "string1", "string2" ] ] }
```

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

## Fn::GetOptionSetting
<a name="ebextensions-functions-getoptionsetting"></a>

使用 `Fn::GetOptionSetting` 來擷取環境套用之[組態選項](command-options.md)設定的值。

```
"Fn::GetOptionSetting":
  Namespace: "namespace"
  OptionName: "option name"
  DefaultValue: "default value"
```

在[存放私密金鑰](https-storingprivatekeys.md)範例中：

```
Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Auth:
          type: "s3"
          buckets: ["elasticbeanstalk-us-west-2-123456789012"]
          roleName: 
            "Fn::GetOptionSetting": 
              Namespace: "aws:autoscaling:launchconfiguration"
              OptionName: "IamInstanceProfile"
              DefaultValue: "aws-elasticbeanstalk-ec2-role"
```