

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# DeploymentPreference
<a name="sam-property-function-deploymentpreference"></a>

指定启用逐步 Lambda 部署的配置。有关配置逐步 Lambda 部署的更多信息，请参阅 [使用以下方法逐步部署无服务器应用程序 AWS SAM](automating-updates-to-serverless-apps.md)。

**注意**  
要使用 `DeploymentPreference` 对象，必须在 [AWS::Serverless::Function](sam-resource-function.md) 中指定 `AutoPublishAlias`，否则将导致错误。

## 语法
<a name="sam-property-function-deploymentpreference-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-function-deploymentpreference-syntax.yaml"></a>

```
  [Alarms](#sam-function-deploymentpreference-alarms): List
  [Enabled](#sam-function-deploymentpreference-enabled): Boolean
  [Hooks](#sam-function-deploymentpreference-hooks): Hooks
  [PassthroughCondition](#sam-function-deploymentpreference-passthroughcondition): Boolean
  [Role](#sam-function-deploymentpreference-role): String
  [TriggerConfigurations](#sam-function-deploymentpreference-triggerconfigurations): List
  [Type](#sam-function-deploymentpreference-type): String
```

## Properties
<a name="sam-property-function-deploymentpreference-properties"></a>

 `Alarms`   <a name="sam-function-deploymentpreference-alarms"></a>
您希望由部署引发的任何错误触发的 CloudWatch 警报列表。  
此属性接受 `Fn::If` 内置函数。有关使用 `Fn::If` 的示例模板，请参阅本主题底部的“示例”部分。  
*类型*：列表  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Enabled`   <a name="sam-function-deploymentpreference-enabled"></a>
是否启用此部署首选项。  
*类型*：布尔值  
*必需*：否  
*默认值*：True  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Hooks`   <a name="sam-function-deploymentpreference-hooks"></a>
流量转移之前和之后运行的验证 Lambda 函数。  
*类型*：[钩子](sam-property-function-hooks.md)  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `PassthroughCondition`   <a name="sam-function-deploymentpreference-passthroughcondition"></a>
如果为 True，并且启用了此部署首选项，则函数的 Condition 将传递给生成的 CodeDeploy 资源。通常，应将其设置为 True。否则，即使函数的条件解析为 False，也会创建 CodeDeploy 资源。  
*类型*：布尔值  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `Role`   <a name="sam-function-deploymentpreference-role"></a>
用于流量转移的 IAM 角色 ARN。 CodeDeploy 如有提供，则不会创建 IAM 角色。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `TriggerConfigurations`   <a name="sam-function-deploymentpreference-triggerconfigurations"></a>
要与部署组关联的触发器配置列表。用于通知有关生命周期事件的 SNS 主题。  
*类型*：列表  
*必需*：否  
*CloudFormation 兼容性*：此属性直接传递给`AWS::CodeDeploy::DeploymentGroup`资源的`[TriggerConfigurations](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codedeploy-deploymentgroup.html#cfn-codedeploy-deploymentgroup-triggerconfigurations)`属性。

 `Type`   <a name="sam-function-deploymentpreference-type"></a>
目前有两类部署类型：线性和金丝雀。有关可用部署类型的更多信息，请参阅 [使用以下方法逐步部署无服务器应用程序 AWS SAM](automating-updates-to-serverless-apps.md)。  
*类型*：字符串  
*是否必需*：是  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-property-function-deploymentpreference--examples"></a>

### DeploymentPreference 带有交通前和交通后的挂钩。
<a name="sam-property-function-deploymentpreference--examples--deploymentpreference-with-pre--and-post-traffic-hooks."></a>

包含流量前后钩子的部署首选项示例。

#### YAML
<a name="sam-property-function-deploymentpreference--examples--deploymentpreference-with-pre--and-post-traffic-hooks.--yaml"></a>

```
DeploymentPreference:
  Enabled: true
  Type: Canary10Percent10Minutes 
  Alarms:
    - !Ref: AliasErrorMetricGreaterThanZeroAlarm
    - !Ref: LatestVersionErrorMetricGreaterThanZeroAlarm
  Hooks:
    PreTraffic:
      !Ref: PreTrafficLambdaFunction
    PostTraffic:
      !Ref: PostTrafficLambdaFunction
```

### DeploymentPreference 使用 Fn:: If 内部函数
<a name="sam-property-function-deploymentpreference--examples--deploymentpreference-with-fn::if-intrinsic-function"></a>

使用 `Fn::If` 配置警报的部署首选项示例。在本示例中，如果 `MyCondition` 为 `true`，则将配置 `Alarm1`；如果 `MyCondition` 为 `false`，则将配置 `Alarm2` 和 `Alarm5`。

#### YAML
<a name="sam-property-function-deploymentpreference--examples--deploymentpreference-with-fn::if-intrinsic-function--yaml"></a>

```
DeploymentPreference:
  Enabled: true
  Type: Canary10Percent10Minutes 
  Alarms:
    Fn::If:
      - MyCondition
      - - Alarm1
      - - Alarm2
        - Alarm5
```

# Hooks
<a name="sam-property-function-hooks"></a>

流量转移之前和之后运行的验证 Lambda 函数。

**注意**  
此属性中引用的 Lambda 函数会配置生成的 [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-alias.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-alias.html) 资源的 `CodeDeployLambdaAliasUpdate` 对象。有关更多信息，请参阅《*AWS CloudFormation 用户指南*》中的[CodeDeployLambdaAliasUpdate 策略](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html#cfn-attributes-updatepolicy-codedeploylambdaaliasupdate)。

## 语法
<a name="sam-property-function-hooks-syntax"></a>

要在 AWS Serverless Application Model (AWS SAM) 模板中声明此实体，请使用以下语法。

### YAML
<a name="sam-property-function-hooks-syntax.yaml"></a>

```
  [PostTraffic](#sam-function-hooks-posttraffic): String
  [PreTraffic](#sam-function-hooks-pretraffic): String
```

## Properties
<a name="sam-property-function-hooks-properties"></a>

 `PostTraffic`   <a name="sam-function-hooks-posttraffic"></a>
流量转移之后运行的 Lambda 函数。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

 `PreTraffic`   <a name="sam-function-hooks-pretraffic"></a>
流量转移之前运行的 Lambda 函数。  
*类型*：字符串  
*必需*：否  
*CloudFormation 兼容性*：此属性是独有的 AWS SAM ，没有 CloudFormation 等效属性。

## 示例
<a name="sam-property-function-hooks--examples"></a>

### 挂钩
<a name="sam-property-function-hooks--examples--hooks"></a>

示例钩子函数

#### YAML
<a name="sam-property-function-hooks--examples--hooks--yaml"></a>

```
Hooks:
  PreTraffic:
    Ref: PreTrafficLambdaFunction
  PostTraffic:
    Ref: PostTrafficLambdaFunction
```