

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

# 使用 EcsProperties 建立任務定義
<a name="multi-container-jobs"></a>

透過使用 AWS Batch 的任務定義[https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html)，您可以在個別容器中建立硬體、感應器、3D 環境和其他模擬的模型。您可以使用此功能以邏輯方式組織工作負載元件，並將其與主要應用程式分開。此功能可在 Amazon Elastic Container Service (Amazon ECS)、Amazon Elastic Kubernetes Service (Amazon EKS) 和 AWS Batch 上使用 AWS Fargate。

## `ContainerProperties` 與`EcsProperties`任務定義
<a name="containerpropertions-vs-ecsproperties"></a>

您可以選擇使用 [https://docs.aws.amazon.com/batch/latest/APIReference/API_ContainerProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_ContainerProperties.html)或 [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html)任務定義做為您的使用案例指示。在高階，使用 執行 AWS Batch 中的任務`EcsProperties`類似於使用 執行中的任務`ContainerProperties`。

使用 的舊版任務定義結構`ContainerProperties`仍受支援。如果您目前有使用此結構的工作流程，您可以繼續執行它們。

主要差別在於有新物件新增至任務定義，以容納 `EcsProperties`型定義。

例如，在 Amazon ECS 和 Fargate `ContainerProperties`上使用 的任務定義具有下列結構：

```
{
   "containerProperties": {
     ...
     "image": "my_ecr_image1",
     ...
  },
...
}
```

在 Amazon ECS 和 Fargate `EcsProperties`上使用的任務定義具有下列結構：

```
{
  "ecsProperties": {
    "taskProperties": [{
      "containers": [
        { 
          ...
          "image": "my_ecr_image1",
          ...
        },
        { 
          ...
          "image": "my_ecr_image2",
          ...
        },
```

## AWS Batch APIs的一般變更
<a name="multi-container-general"></a>

以下進一步概述使用 `ContainerProperties`和 `EcsProperties` API 資料類型時的一些主要差異：
+ 在 中使用的許多參數都`ContainerProperties`會顯示在 中`TaskContainerProperties`。一些範例包括 、`command``image`、`secrets`、 `privileged`和 `users`。它們都可以在 [TaskContainerProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html) 中找到。
+ 有些`TaskContainerProperties`參數在舊版結構中沒有功能同等項目。一些範例包括 、`dependsOn``essential`、`ipcMode`、 `name`和 `pidMode`。如需詳細資訊，請參閱 [EcsTaskDetails](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskDetails.html) 和 [TaskContainerProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html)。

  此外，某些`ContainerProperties`參數在 `EcsProperties` 結構中沒有對等參數或應用程式。在 中[https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties)， `container` 已取代為 ，`containers`因此新物件最多可接受 10 個元素。如需詳細資訊，請參閱 [RegisterJobDefinition：containerProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-containerProperties) 和 [EcsTaskProperties：containers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html)。
+ `taskRoleArn` 功能上等同於 `jobRoleArn`。如需詳細資訊，請參閱 [EcsTaskProperties：taskRoleArn](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html) 和 [ContainerProperties：jobRoleArn](https://docs.aws.amazon.com/batch/latest/APIReference/API_ContainerProperties.html)。
+ 您可以在 `EcsProperties`結構中包含一 (1) 到十 (10) 個容器。如需詳細資訊，請參閱 [EcsTaskProperties：containers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html)。
+ `taskProperties` 和 instanceTypes 物件是陣列，但目前只接受一個元素。例如，[EcsProperties：taskProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html) 和 [NodeRangeProperty：instanceTypes](https://docs.aws.amazon.com/batch/latest/APIReference/API_NodeRangeProperty.html)。

## Amazon ECS 的多容器任務定義
<a name="multi-container-ecs-updates"></a>

為了容納 Amazon ECS 的多容器結構，某些 API 資料類型不同。例如 
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-ecsProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-ecsProperties) 與單一容器定義`containerProperties`中的層級相同。如需詳細資訊，請參閱《 *AWS Batch API 參考指南*》中的 [EcsProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html)。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html#Batch-Type-EcsProperties-taskProperties) 包含為 Amazon ECS 任務定義的屬性。如需詳細資訊，請參閱《 *AWS Batch API 參考指南*》中的 [EcsProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsProperties.html)。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html#Batch-Type-EcsTaskProperties-containers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html#Batch-Type-EcsTaskProperties-containers) 在單一容器定義`containerProperties`中包含與 類似的資訊。主要差別在於`containers`可讓您定義最多十個容器。如需詳細資訊，請參閱《 *AWS Batch API 參考指南*》中的 [ECSTaskProperties：containers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EcsTaskProperties.html)。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-essential](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-essential) 參數指出容器如何影響任務。所有基本容器都必須成功完成 （以 0 結束），任務才能繼續。如果標示為基本的容器失敗 （結束為非 0)，則任務會失敗。

  預設值為 `true`，且至少有一個容器必須標記為 `essential`。如需詳細資訊，請參閱 [`essential` API 參考指南](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-essential)中的「*AWS Batch *」。
+ 使用 [https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-dependsOn](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-dependsOn) 參數，您可以定義容器相依性的清單。如需詳細資訊，請參閱 [`dependsOn` API 參考指南](https://docs.aws.amazon.com/batch/latest/APIReference/API_TaskContainerProperties.html#Batch-Type-TaskContainerProperties-dependsOn)中的「*AWS Batch *」。
**注意**  
`dependsOn` 清單的複雜性和相關聯的容器執行時間可能會影響任務的開始時間。如果相依性需要很長時間才能執行，任務將保持 `STARTING` 狀態，直到完成為止。

如需 `ecsProperties`和 結構的詳細資訊，請參閱 [ecsProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-ecsProperties) 的 [RegisterJobDefinition](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#API_RegisterJobDefinition_RequestBody) 請求語法。

## Amazon EKS 的多容器任務定義
<a name="multi-container-eks-updates"></a>

為了容納 Amazon EKS 的多容器結構，某些 API 資料類型不同。例如 
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EksContainer.html#Batch-Type-EksContainer-name](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksContainer.html#Batch-Type-EksContainer-name) 是容器的唯一識別符。單一容器不需要此物件，但在 Pod 中定義多個容器時需要此物件。如果`name`未針對單一容器定義 ，則會`default`套用預設名稱 。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-initContainers](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-initContainers) 在 [eksPodProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html) 資料類型中定義。它們會在應用程式容器之前執行，一律會執行到完成，而且必須在下一個容器啟動之前成功完成。

  這些容器已向 Amazon EKS Connector 代理程式註冊，並在 Amazon Elastic Kubernetes Service 後端資料存放區中保留註冊資訊。`initContainers` 物件最多可接受十 (10) 個元素。如需詳細資訊，請參閱 *Kubernetes 文件*中的[初始化容器](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/)。
**注意**  
`initContainers` 物件可能會影響任務的開始時間。如果 `initContainers`需要很長時間才能執行，任務將保持 `STARTING` 狀態，直到完成為止。
+ [https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-shareProcessNamespace](https://docs.aws.amazon.com/batch/latest/APIReference/API_EksPodProperties.html#Batch-Type-EksPodProperties-shareProcessNamespace) 指出 Pod 中的容器是否可以共用相同的程序命名空間。預設值為 `false`。將此設定為 `true`，以啟用容器查看位於相同 Pod 中其他容器中的程序並發出訊號。
+ 每個容器都很重要。所有容器都必須成功完成 （以 0 結束），任務才能成功。如果一個容器失敗 （除了 0 之外結束），則任務會失敗。

如需 `eksProperties`和 結構的詳細資訊，請參閱 [eksProperties](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#Batch-RegisterJobDefinition-request-eksProperties) 的 [RegisterJobDefinition](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html#API_RegisterJobDefinition_RequestBody) 請求語法。

# 使用 EcsProperties 的 Reference： AWS Batch job 案例
<a name="multi-container-jobs-scenarios"></a>

為了說明如何根據您的需求`EcsProperties`來建構 使用 AWS Batch 的任務定義，本主題提供下列[https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html](https://docs.aws.amazon.com/batch/latest/APIReference/API_RegisterJobDefinition.html)承載。您可以將這些範例複製到檔案中，根據您的需求自訂範例，然後使用 AWS Command Line Interface (AWS CLI) 呼叫 `RegisterJobDefinition`。

## AWS Batch Amazon EC2 上的 Amazon ECS 任務
<a name="multi-container-scenario-on-ec2"></a>

以下是 Amazon Elastic Compute Cloud 上的 Amazon Elastic Container Service AWS Batch 任務範例：

```
{
    "jobDefinitionName": "multicontainer-ecs-ec2",
    "type": "container",
    "ecsProperties": {
        "taskProperties": [
          {
            "containers": [
              {
                "name": "c1",
                "essential": false,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "2"
                  },
                  {
                    "type": "MEMORY",
                    "value": "4096"
                  }
                ]
              },
              {
                "name": "c2",
                "essential": false,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "2"
                  },
                  {
                    "type": "MEMORY",
                    "value": "4096"
                  }
                ]
              },
              {
                "name": "c3",
                "essential": true,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "firelensConfiguration": {
                  "type": "fluentbit",
                  "options": {
                    "enable-ecs-log-metadata": "true"
                  }
                 },
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "6"
                  },
                  {
                    "type": "MEMORY",
                    "value": "12288"
                  }
                ]
              }
            ]
          }
        ]
  }
}
```

## AWS Batch Fargate 上的 Amazon ECS 任務
<a name="multi-containers-ecs-on-fargate"></a>

以下是 上的 Amazon Elastic Container Service AWS Batch 任務範例 AWS Fargate：

```
{
    "jobDefinitionName": "multicontainer-ecs-fargate",
    "type": "container",
    "platformCapabilities": [
        "FARGATE"
    ],
    "ecsProperties": {
        "taskProperties": [
          {
            "containers": [
              {
                "name": "c1",
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "2"
                  },
                  {
                    "type": "MEMORY",
                    "value": "4096"
                  }
                ]
              },
              {
                "name": "c2",
                "essential": true,
                "command": [
                  "echo",
                  "hello world"
                ],
                "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                "resourceRequirements": [
                  {
                    "type": "VCPU",
                    "value": "6"
                  },
                  {
                    "type": "MEMORY",
                    "value": "12288"
                  }
                ]
              }
            ],
            "executionRoleArn": "arn:aws:iam::1112223333:role/ecsTaskExecutionRole"
          }
        ]
  }
}
```

## AWS Batch Amazon EKS 的任務
<a name="multi-containers-eks-example"></a>

以下是 Amazon Elastic Kubernetes Service AWS Batch 任務的範例：

```
{
  "jobDefinitionName": "multicontainer-eks",
  "type": "container",
  "eksProperties": {
    "podProperties": {
      "shareProcessNamespace": true,
      "initContainers": [
        {
          "name": "init-container",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "echo"
          ],
          "args": [
            "hello world"
          ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        },
        {
          "name": "init-container-2",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "echo",
            "my second init container"
          ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        }
      ],
      "containers": [
        {
          "name": "c1",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "echo world"
         ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        },
        {
          "name": "sleep-container",
          "image": "public.ecr.aws/amazonlinux/amazonlinux:2",
          "command": [
            "sleep",
            "20"
          ],
          "resources": {
            "requests": {
              "cpu": "1",
              "memory": "512Mi"
            }
          }
        }
      ]
    }
  }
}
```

## 每個節點具有多個容器的 MNP AWS Batch 任務
<a name="multi-container-ecs-mnp"></a>

以下是每個節點具有多個容器的多節點平行 (MNP) AWS Batch 任務範例：

```
{
  "jobDefinitionName": "multicontainer-mnp",
  "type": "multinode",
  "nodeProperties": {
    "numNodes": 6,
    "mainNode": 0,
    "nodeRangeProperties": [
      {
        "targetNodes": "0:5",
        "ecsProperties": {
          "taskProperties": [
            {
              "containers": [
                {
                  "name": "range05-c1",
                  "command": [
                    "echo",
                    "hello world"
                  ],
                  "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                  "resourceRequirements": [
                    {
                      "type": "VCPU",
                      "value": "2"
                    },
                    {
                      "type": "MEMORY",
                      "value": "4096"
                    }
                  ]
                },
                {
                  "name": "range05-c2",
                  "command": [
                    "echo",
                    "hello world"
                  ],
                  "image": "public.ecr.aws/amazonlinux/amazonlinux:latest",
                  "resourceRequirements": [
                    {
                      "type": "VCPU",
                      "value": "2"
                    },
                    {
                      "type": "MEMORY",
                      "value": "4096"
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}
```