

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

# AppSpec 文件示例
<a name="reference-appspec-file-example"></a>

本主题提供了 AWS Lambda 和 EC2/本地部署的示例 AppSpec 文件。

**Topics**
+ [AppSpec Amazon ECS 部署的文件示例](#appspec-file-example-ecs)
+ [AppSpec AWS Lambda 部署的文件示例](#appspec-file-example-lambda)
+ [AppSpec EC2/本地部署的文件示例](#appspec-file-example-server)

## AppSpec Amazon ECS 部署的文件示例
<a name="appspec-file-example-ecs"></a>

 以下是使用 YAML 编写的用于部署 Amazon ECS 服务的 AppSpec 文件的示例。

```
version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: "arn:aws:ecs:us-east-1:111222333444:task-definition/my-task-definition-family-name:1"
        LoadBalancerInfo:
          ContainerName: "SampleApplicationName"
          ContainerPort: 80
# Optional properties
        PlatformVersion: "LATEST"
        NetworkConfiguration:
          AwsvpcConfiguration:
            Subnets: ["subnet-1234abcd","subnet-5678abcd"]
            SecurityGroups: ["sg-12345678"]
            AssignPublicIp: "ENABLED"
        CapacityProviderStrategy:
          - Base: 1
            CapacityProvider: "FARGATE_SPOT"
            Weight: 2
          - Base: 0
            CapacityProvider: "FARGATE"
            Weight: 1
Hooks:
  - BeforeInstall: "LambdaFunctionToValidateBeforeInstall"
  - AfterInstall: "LambdaFunctionToValidateAfterInstall"
  - AfterAllowTestTraffic: "LambdaFunctionToValidateAfterTestTrafficStarts"
  - BeforeAllowTraffic: "LambdaFunctionToValidateBeforeAllowingProductionTraffic"
  - AfterAllowTraffic: "LambdaFunctionToValidateAfterAllowingProductionTraffic"
```

 下面是用 JSON 编写的上述示例的版本。

```
{
    "version": 0.0,
    "Resources": [
        {
            "TargetService": {
                "Type": "AWS::ECS::Service",
                "Properties": {
                    "TaskDefinition": "arn:aws:ecs:us-east-1:111222333444:task-definition/my-task-definition-family-name:1",
                    "LoadBalancerInfo": {
                        "ContainerName": "SampleApplicationName",
                        "ContainerPort": 80
                    },
                    "PlatformVersion": "LATEST",
                    "NetworkConfiguration": {
                        "AwsvpcConfiguration": {
                            "Subnets": [
                                "subnet-1234abcd",
                                "subnet-5678abcd"
                            ],
                            "SecurityGroups": [
                                "sg-12345678"
                            ],
                            "AssignPublicIp": "ENABLED"
                        }
                    },
                    "CapacityProviderStrategy": [
                        {
                            "Base" : 1,
                            "CapacityProvider" : "FARGATE_SPOT",
                            "Weight" : 2
                        },
                        {
                            "Base" : 0,
                            "CapacityProvider" : "FARGATE",
                            "Weight" : 1
                        }
                    ]
                }               
            }
        }
    ],
    "Hooks": [
        {
            "BeforeInstall": "LambdaFunctionToValidateBeforeInstall"
        },
        {
            "AfterInstall": "LambdaFunctionToValidateAfterInstall"
        },
        {
            "AfterAllowTestTraffic": "LambdaFunctionToValidateAfterTestTrafficStarts"
        },
        {
            "BeforeAllowTraffic": "LambdaFunctionToValidateBeforeAllowingProductionTraffic"
        },
        {
            "AfterAllowTraffic": "LambdaFunctionToValidateAfterAllowingProductionTraffic"
        }
    ]
}
```

下面是部署期间的事件序列：

1.  在替换任务集上安装更新的 Amazon ECS 应用程序之前，名为 `LambdaFunctionToValidateBeforeInstall` 的 Lambda 函数运行。

1.  在替换任务集上安装更新的 Amazon ECS 应用程序之后，但在该应用程序接收任何流量之前，名为 `LambdaFunctionToValidateAfterInstall` 的 Lambda 函数运行。

1.  在替换任务集上的 Amazon ECS 应用程序开始接收来自测试侦听器的流量之后，名为 `LambdaFunctionToValidateAfterTestTrafficStarts` 的 Lambda 函数运行。此函数可能会运行验证测试以确定部署是否继续。如果未在部署组中指定测试侦听器，则会忽略此挂钩。

1.  在完成 `AfterAllowTestTraffic` 挂钩中的任何验证测试之后，并且在将生产流量提供给更新的 Amazon ECS 应用程序之前，名为 `LambdaFunctionToValidateBeforeAllowingProductionTraffic` 的 Lambda 函数运行。

1.  生产流量在更换任务集上提供给更新的 Amazon ECS 应用程序后，名为 `LambdaFunctionToValidateAfterAllowingProductionTraffic` 的 Lambda 函数运行。

 在任何挂钩期间运行的 Lambda 函数都可以执行验证测试或者收集流量指标。

## AppSpec AWS Lambda 部署的文件示例
<a name="appspec-file-example-lambda"></a>

 以下是使用 YAML 编写的用于部署 Lambda 函数版本的 AppSpec 文件的示例。

```
version: 0.0
Resources:
  - myLambdaFunction:
      Type: AWS::Lambda::Function
      Properties:
        Name: "myLambdaFunction"
        Alias: "myLambdaFunctionAlias"
        CurrentVersion: "1"
        TargetVersion: "2"
Hooks:
  - BeforeAllowTraffic: "LambdaFunctionToValidateBeforeTrafficShift"
  - AfterAllowTraffic: "LambdaFunctionToValidateAfterTrafficShift"
```

 下面是用 JSON 编写的上述示例的版本。

```
{
 	"version": 0.0,
 	"Resources": [{
 		"myLambdaFunction": {
 			"Type": "AWS::Lambda::Function",
 			"Properties": {
 				"Name": "myLambdaFunction",
 				"Alias": "myLambdaFunctionAlias",
 				"CurrentVersion": "1",
 				"TargetVersion": "2"
 			}
 		}
 	}],
 	"Hooks": [{
 			"BeforeAllowTraffic": "LambdaFunctionToValidateBeforeTrafficShift"
      },
      {
 			"AfterAllowTraffic": "LambdaFunctionToValidateAfterTrafficShift"
 		}
 	]
 }
```

下面是部署期间的事件序列：

1. 在将流量从名为 `myLambdaFunction` 的 Lambda 函数的版本 1 转移到版本 2 前，运行名为 `LambdaFunctionToValidateBeforeTrafficShift` 的 Lambda 函数，该函数将验证部署是否已准备好开始流量转移。

1. 如果 `LambdaFunctionToValidateBeforeTrafficShift` 返回了退出代码 0（成功），则开始将流量转移到 `myLambdaFunction` 的版本 2。此部署的部署配置确定流量转移的速率。

1. 在完成将流量从名为 `myLambdaFunction` 的 Lambda 函数的版本 1 转移到版本 2 后，运行名为 `LambdaFunctionToValidateAfterTrafficShift` 的 Lambda 函数，该函数将验证部署是否已成功完成。

## AppSpec EC2/本地部署的文件示例
<a name="appspec-file-example-server"></a>

以下是就地部署到亚马逊 Linux、Ubuntu 服务器或 RHEL 实例 AppSpec 的文件示例。

**注意**  
 部署到 Windows Server 实例不支持 `runas` 元素。如果您要部署到 Windows 服务器实例，请不要将其包含在 AppSpec 文件中。

```
version: 0.0
os: linux
files:
  - source: Config/config.txt
    destination: /webapps/Config
  - source: source
    destination: /webapps/myApp
hooks:
  BeforeInstall:
    - location: Scripts/UnzipResourceBundle.sh
    - location: Scripts/UnzipDataBundle.sh
  AfterInstall:
    - location: Scripts/RunResourceTests.sh
      timeout: 180
  ApplicationStart:
    - location: Scripts/RunFunctionalTests.sh
      timeout: 3600
  ValidateService:
    - location: Scripts/MonitorService.sh
      timeout: 3600
      runas: codedeployuser
```

对于 Windows Server 实例，将 `os: linux` 更改为 `os: windows`。另外，您还必须完全限定 `destination` 路径（例如 `c:\temp\webapps\Config` 和 `c:\temp\webapps\myApp`）。不包括 `runas` 元素。

下面是部署期间的事件序列：

1. 运行位于 `Scripts/UnzipResourceBundle.sh` 的脚本。

1. 如果前面的脚本返回了退出代码 0（成功），则运行位于 `Scripts/UnzipDataBundle.sh` 中的脚本。

1. 将文件从 `Config/config.txt` 路径复制到 `/webapps/Config/config.txt` 路径中。

1. 以递归方式将 `source` 目录中的所有文件复制到 `/webapps/myApp` 目录中。

1. 运行位于 `Scripts/RunResourceTests.sh` 中的脚本，超时时间为 180 秒（3 分钟）。

1. 运行位于 `Scripts/RunFunctionalTests.sh` 中的脚本，超时时间为 3600 秒（1 小时）。

1. 以 `Scripts/MonitorService.sh` 用户身份运行位于 `codedeploy` 中的脚本，超时时间为 3600 秒（1 小时）。