

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

# AppSpec 'resources' 區段 （僅限 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**
+ [AWS Lambda 部署的 AppSpec 'resources' 區段](#reference-appspec-file-structure-resources-lambda)
+ [Amazon ECS 部署的 AppSpec 'resources' 區段](#reference-appspec-file-structure-resources-ecs)

## AWS Lambda 部署的 AppSpec 'resources' 區段
<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 函數流量的轉移版本。此值必須是有效的正整數。

## Amazon ECS 部署的 AppSpec 'resources' 區段
<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 Resource Name (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 Container Service 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*。