

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

# AppSpec “资源” 部分（仅限 Amazon ECS 和 AWS Lambda 部署）
<a name="reference-appspec-file-structure-resources"></a>

 AppSpec 文件`'resources'`部分的内容因部署的计算平台而异。Amazon ECS 部署的 `'resources'` 部分包含 Amazon ECS 任务定义、用于将流量路由到更新的 Amazon ECS 任务集的容器和端口以及其他可选信息。 AWS Lambda 部署`'resources'`部分包含 Lambda 函数的名称、别名、当前版本和目标版本。

**Topics**
+ [AppSpec AWS Lambda 部署的 “资源” 部分](#reference-appspec-file-structure-resources-lambda)
+ [AppSpec Amazon ECS 部署的 “资源” 部分](#reference-appspec-file-structure-resources-ecs)

## AppSpec AWS Lambda 部署的 “资源” 部分
<a name="reference-appspec-file-structure-resources-lambda"></a>

`'resources'` 部分指定要部署的 Lambda 函数，并具有以下结构：

YAML：

```
resources:
  - name-of-function-to-deploy:
      type: "AWS::Lambda::Function"
      properties:
        name: name-of-lambda-function-to-deploy
        alias: alias-of-lambda-function-to-deploy
        currentversion: version-of-the-lambda-function-traffic-currently-points-to
        targetversion: version-of-the-lambda-function-to-shift-traffic-to
```

JSON:

```
"resources": [
    {
        "name-of-function-to-deploy" {
            "type": "AWS::Lambda::Function",
            "properties": {
                "name": "name-of-lambda-function-to-deploy",
                "alias": "alias-of-lambda-function-to-deploy",
                "currentversion": "version-of-the-lambda-function-traffic-currently-points-to",
                "targetversion": "version-of-the-lambda-function-to-shift-traffic-to"
            }
        }
    }
]
```

每个属性均使用字符串指定。
+ `name` – 必需。这是要部署的 Lambda 函数的名称。
+ `alias` – 必需。这是 Lambda 函数的别名。
+ `currentversion` – 必需。这是流量当前定向到的 Lambda 函数版本。此值必须为有效的正整数。
+ `targetversion` – 必需。这是流量要转移到的 Lambda 函数版本。此值必须为有效的正整数。

## AppSpec Amazon ECS 部署的 “资源” 部分
<a name="reference-appspec-file-structure-resources-ecs"></a>

 `'resources'` 部分指定要部署的 Amazon ECS 服务，并具有以下结构：

YAML：

```
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: "task-definition-arn"
        LoadBalancerInfo: 
          ContainerName: "ecs-container-name" 
          ContainerPort: "ecs-application-port"
# Optional properties
        PlatformVersion: "ecs-service-platform-version"
        NetworkConfiguration:
          AwsvpcConfiguration:
            Subnets: ["ecs-subnet-1","ecs-subnet-n"] 
            SecurityGroups: ["ecs-security-group-1","ecs-security-group-n"] 
            AssignPublicIp: "ENABLED | DISABLED"
        CapacityProviderStrategy:
          - Base: integer
            CapacityProvider: "capacityProviderA"
            Weight: integer
          - Base: integer
            CapacityProvider: "capacityProviderB"
            Weight: integer
```

JSON:

```
"Resources": [
    {
        "TargetService": {
            "Type": "AWS::ECS::Service",
            "Properties": {
                "TaskDefinition": "task-definition-arn",
                "LoadBalancerInfo": {
                    "ContainerName": "ecs-container-name",
                    "ContainerPort": "ecs-application-port"
                },
                "PlatformVersion": "ecs-service-platform-version",
                "NetworkConfiguration": {
                    "AwsvpcConfiguration": {
                        "Subnets": [
                            "ecs-subnet-1",
                            "ecs-subnet-n"
                        ],
                        "SecurityGroups": [
                            "ecs-security-group-1",
                            "ecs-security-group-n"
                        ],
                        "AssignPublicIp": "ENABLED | DISABLED"
                    }
                },
                "CapacityProviderStrategy": [
                    {
                        "Base": integer,
                        "CapacityProvider": "capacityProviderA",
                        "Weight": integer
                    },
                    {
                        "Base": integer,
                        "CapacityProvider": "capacityProviderB",
                        "Weight": integer
                    }
                ]
            }
        }
    }
]
```

每个属性都使用字符串指定，但 `ContainerPort` 除外，它是一个数字。
+ `TaskDefinition` – 必需。这是要部署的 Amazon ECS 服务的任务定义。它是使用任务定义的 ARN 指定的。ARN 格式为 `arn:aws:ecs:aws-region:account-id:task-definition/task-definition-family:task-definition-revision`。有关更多信息，请参阅 [Amazon 资源名称 (ARNs) 和 AWS 服务命名空间](https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html)。
**注意**  
ARN 的 `:task-definition-revision` 部分是可选的。如果省略该部分，则 Amazon ECS 将使用任务定义的最新 ACTIVE 修订。
+ `ContainerName` – 必需。这是包含 Amazon ECS 应用程序的 Amazon ECS 容器名称。它必须是在 Amazon ECS 任务定义中指定的容器。
+ `ContainerPort` – 必需。这是流量将被路由到的容器上的端口。
+ `PlatformVersion`: 可选。已部署的 Amazon ECS 服务中 Fargate 任务的平台版本。有关平台版本的更多信息，请参阅 [AWS Fargate 平台版本](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html)。如果没有指定任何版本，将默认使用 `LATEST`。
+  `NetworkConfiguration`: 可选。在 `AwsvpcConfiguration` 下，您可以指定以下设置。有关更多信息，请参阅 *Amazon ECS 容器服务 API 参考[AwsVpcConfiguration](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_AwsVpcConfiguration.html)*中的。
  + `Subnets`: 可选。Amazon ECS 服务中一个或多个子网的逗号分隔列表。
  + `SecurityGroups`: 可选。Amazon Elastic Container Service 中一个或多个安全组的逗号分隔列表。
  + `AssignPublicIp`: 可选。一个字符串，它指定 Amazon ECS 服务的弹性网络接口是否接收公有 IP 地址。有效值为 `ENABLED` 和 `DISABLED`。
**注意**  
 必须指定 `NetworkConfiguration` 下的所有设置或不指定任何设置。例如，如果您要指定 `Subnets`，那么还必须指定 `SecurityGroups` 和 `AssignPublicIp`。如果未指定，则 CodeDeploy 使用当前的网络 Amazon ECS 设置。
+ `CapacityProviderStrategy`: 可选。您要用于部署的 Amazon ECS 容量提供程序的列表。有关更多信息，请参阅《Amazon Elastic Container Service 开发人员指南》**中的 [Amazon ECS 容量提供程序](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cluster-capacity-providers.html)。对于每个容量提供程序，您可以指定以下设置。有关这些设置的详细信息，请参阅《*AWS CloudFormation 用户指南》[AWS::ECS::ServiceCapacityProviderStrategyItem](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-service-capacityproviderstrategyitem.html)*中的
  + `Base`: 可选。基准值指明在指定的容量提供程序上至少运行多少个任务。在一个容量提供程序策略中，只能有一个容量提供程序策略定义了基准。如果未指定值，则使用默认值 0。
  + `CapacityProvider`: 可选。容量提供程序的简称。示例：*capacityProviderA*
  + `Weight`: 可选。

    *权重* 值指明应使用指定容量提供程序的已启动任务总数的相对百分比。在满足了 `base` 值（如果定义）之后，将考虑 `weight` 值。

    如果未指定 `weight` 值，则使用默认值 `0`。如果在容量提供程序策略中指定了多个容量提供程序，则至少有一个容量提供程序的权重值必须大于零，且任何权重为 `0` 的容量提供程序都不会被用来放置任务。如果您在策略中指定的多个容量提供程序的权重全部为 `0`，则使用该容量提供程序策略的任何 `RunTask` 或 `CreateService` 操作都将失败。

     下面是使用权重的示例情景：定义的策略包含两个容量提供程序，并且两个容量提供程序的权重均为 `1`，那么当满足 `base` 时，这些任务将在两个容量提供程序之间均匀分配。按照相同的逻辑，如果您为 *capacityProviderA* 指定权重 `1`，并为 *capacityProviderB* 指定权重 `4`，那么运行的每一个任务均使用 *capacityProviderA*，四个任务将使用 *capacityProviderB*。