

# 在 Amazon ECS 任务定义中将卷配置推迟到启动时间
<a name="specify-ebs-config"></a>

要将 Amazon EBS 卷配置为附加到您的任务，您必须在任务定义中指定卷和挂载点配置和给卷命名。您还必须将 `configuredAtLaunch` 设置为 `true`，因为无法在任务定义中为附加配置 Amazon EBS 卷。反之，在部署期间可配置 Amazon EBS 卷进行附加。

要使用 AWS Command Line Interface（AWS CLI）注册任务定义，请将模板另存为 JSON 文件，然后将该文件作为 `[register-task-definition](https://docs.aws.amazon.com/cli/latest/reference/ecs/register-task-definition.html)` 命令的输入传递。

要使用 AWS 管理控制台 创建和注册任务定义，请参阅[使用控制台创建 Amazon ECS 任务定义](create-task-definition.md)。

以下任务定义在任务定义中显示 `mountPoints` 和 `volumes` 对象的语法。有关任务定义参数的更多信息，请参阅[Fargate 的 Amazon ECS 任务定义参数](task_definition_parameters.md)。要使用此示例，请将 `user input placeholders` 替换为您自己的信息。

## Linux
<a name="linux-example"></a>

```
{
    "family": "mytaskdef",
    "containerDefinitions": [
        {
            "name": "nginx",
            "image": "public.ecr.aws/nginx/nginx:latest",
            "networkMode": "awsvpc",
           "portMappings": [
                {
                    "name": "nginx-80-tcp",
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp",
                    "appProtocol": "http"
                }
            ],
            "mountPoints": [
                {
                    "sourceVolume": "myEBSVolume",
                    "containerPath": "/mount/ebs",
                    "readOnly": true
                }
            ]
        }
    ],
    "volumes": [
        {
            "name": "myEBSVolume",
            "configuredAtLaunch": true
        }
    ],
    "requiresCompatibilities": [
        "FARGATE", "EC2"
    ],
    "cpu": "1024",
    "memory": "3072",
    "networkMode": "awsvpc"
}
```

## Windows
<a name="windows-example"></a>

```
{
    "family": "mytaskdef",
     "memory": "4096",
     "cpu": "2048",
    "family": "windows-simple-iis-2019-core",
    "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole",
    "runtimePlatform": {"operatingSystemFamily": "WINDOWS_SERVER_2019_CORE"},
    "requiresCompatibilities": ["EC2"]
    "containerDefinitions": [
        {
             "command": ["New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p>'; C:\\ServiceMonitor.exe w3svc"],
            "entryPoint": [
                "powershell",
                "-Command"
            ],
            "essential": true,
            "cpu": 2048,
            "memory": 4096,
            "image": "mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019",
            "name": "sample_windows_app",
            "portMappings": [
                {
                    "hostPort": 443,
                    "containerPort": 80,
                    "protocol": "tcp"
                }
            ],
            "mountPoints": [
                {
                    "sourceVolume": "myEBSVolume",
                    "containerPath": "drive:\ebs",
                    "readOnly": true
                }
            ]
        }
    ],
    "volumes": [
        {
            "name": "myEBSVolume",
            "configuredAtLaunch": true
        }
    ],
    "requiresCompatibilities": [
        "FARGATE", "EC2"
    ],
    "cpu": "1024",
    "memory": "3072",
    "networkMode": "awsvpc"
}
```

`mountPoints`  
类型：对象数组  
必需：否  
容器中数据卷的挂载点。此参数对应于 create-container Docker API 中的 `Volumes` 和 docker run 命令的 `--volume` 选项。  
Windows 容器可在 `$env:ProgramData` 所在的驱动器上挂载整个目录。Windows 容器无法在其他驱动器上挂载目录，并且挂载点不能跨驱动器使用。您必须指定挂载点才能将 Amazon EBS 卷直接附加到 Amazon ECS 任务。    
`sourceVolume`  
类型：字符串  
必需：是，当使用 `mountPoints` 时  
要挂载的卷的名称。  
`containerPath`  
类型：字符串  
必需：是，当使用 `mountPoints` 时  
挂载卷的容器中的路径。  
`readOnly`  
类型：布尔值  
必需：否  
如果此值为`true`，则容器具有对卷的只读访问权。如果此值为`false`，则容器可对卷进行写入。默认值为 `false`。  
对于在运行 Windows 操作系统的 EC2 实例上运行的任务，请保留默认值 `false`。

`name`  
类型：字符串  
必需：否  
卷的名称。最多允许 255 个字母（大写和小写字母）、数字、连字符（`-`）和下划线（`_`）。此名称已在容器定义 `mountPoints` 对象的 `sourceVolume` 参数中引用。

`configuredAtLaunch`  
类型：布尔值  
必需：是，当您要使用将 EBS 卷直接附加到任务时。  
指定卷在启动时是否可配置。如果设置为 `true`，您可以在运行独立任务或者创建或更新服务时配置卷。如果设置为 `false`，则无法在任务定义中提供其他卷配置。必须提供此参数并将其设置为 `true` 才能将 Amazon EBS 卷配置为附加到一个任务中。