

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

# 引導 Amazon ECS Windows 容器執行個體傳遞資料
<a name="bootstrap_windows_container_instance"></a>

當您啟動 Amazon EC2 執行個體時，可以將使用者資料傳送到 EC2 執行個體。此資料可用來執行常見的自動化組態任務，甚至在執行個體啟動時，執行指令碼。對於 Amazon ECS，使用者資料的最常用案例是將組態資訊傳送到 Docker 常駐程式和 Amazon ECS 容器代理程式。

您可以將多種類型的使用者資料傳遞給 Amazon EC2，包含雲端 boothook、shell 指令碼和 `cloud-init` 指令。如需這些和其他格式類型的詳細資訊，請參閱 [Cloud-Init 文件](https://cloudinit.readthedocs.io/en/latest/explanation/format.html)。

您可以在使用 Amazon EC2 啟動精靈時傳遞此使用者資料。如需詳細資訊，請參閱[啟動 Amazon ECS Linux 容器執行個體](launch_container_instance.md)。

## 預設 Windows 使用者資料
<a name="windows-default-userdata"></a>

此範例使用者資料指令碼顯示您使用主控台時，Windows 容器執行個體所收到的預設使用者資料。下列指令碼會執行下列動作：
+ 將叢集名稱設定為您輸入的名稱。
+ 設定任務的 IAM 角色。
+ 將 `json-file` 和 `awslogs` 設定為可用的記錄驅動程式。

此外，當您使用 `awsvpc` 網路模式時，可使用以下選項。
+ `EnableTaskENI`：此標記會開啟任務聯網，並且當您使用 `awsvpc` 網路模式時需要它。
+ `AwsvpcBlockIMDS`：此選擇性標記會封鎖在 `awsvpc` 網路模式中執行的任務容器的 IMDS 存取。
+ `AwsvpcAdditionalLocalRoutes`：此選擇性標記可讓您擁有其他路由。

  將 `ip-address` 替換為其他路由的 IP 地址，例如 172.31.42.23/32。

您可以將此指令碼用於您自己的容器執行個體 (前提是執行個體是從 Amazon ECS 最佳化 Windows Server AMI 啟動)。

替換 `-Cluster cluster-name` 行以指定您自己的叢集名稱。

```
<powershell>
Initialize-ECSAgent -Cluster cluster-name -EnableTaskIAMRole -LoggingDrivers '["json-file","awslogs"]' -EnableTaskENI -AwsvpcBlockIMDS -AwsvpcAdditionalLocalRoutes
'["ip-address"]'
</powershell>
```

 對於設定為使用 `awslogs` 日誌記錄驅動程式的 Windows 任務，您也必須在容器執行個體上設定 `ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE` 環境變數。請使用下列語法。

替換 `-Cluster cluster-name` 行以指定您自己的叢集名稱。

```
<powershell>
[Environment]::SetEnvironmentVariable("ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE", $TRUE, "Machine")
Initialize-ECSAgent -Cluster cluster-name -EnableTaskIAMRole -LoggingDrivers '["json-file","awslogs"]'
</powershell>
```

## Windows 代理程式安裝使用者資料
<a name="agent-service-userdata"></a>

此範例使用者資料指令碼會將 Amazon ECS 容器代理程式安裝在以 **Windows\$1Server-2016-English-Full-Containers** AMI 啟動的執行個體上。它已從 [Amazon ECS Container Agent GitHub 儲存庫](https://github.com/aws/amazon-ecs-agent)讀我檔案頁面的代理程式安裝說明調整。

**注意**  
此指令碼會基於舉例用途而共享。使用 Amazon ECS 最佳化 Windows Server AMI，更容易開始使用 Windows 容器。如需詳細資訊，請參閱[為 Fargate 工作負載建立 Amazon ECS 叢集](create-cluster-console-v2.md)。

如需有關如何在 Windows Server 2022 Full 上安裝 Amazon ECS 代理程式的資訊，請參閱 GitHub 上的 [Issue 3753](https://github.com/aws/amazon-ecs-agent/issues/3753)。

您可以對您自己的容器執行個體使用此指令碼 (假設是使用 **Windows\$1Server-2016-English-Full-Containers** AMI 啟動它們)。務必取代 `windows` 一行，以指定您自己的叢集名稱 (如果您使用的叢集名稱不是 `windows`)。

```
<powershell>
# Set up directories the agent uses
New-Item -Type directory -Path ${env:ProgramFiles}\Amazon\ECS -Force
New-Item -Type directory -Path ${env:ProgramData}\Amazon\ECS -Force
New-Item -Type directory -Path ${env:ProgramData}\Amazon\ECS\data -Force
# Set up configuration
$ecsExeDir = "${env:ProgramFiles}\Amazon\ECS"
[Environment]::SetEnvironmentVariable("ECS_CLUSTER", "windows", "Machine")
[Environment]::SetEnvironmentVariable("ECS_LOGFILE", "${env:ProgramData}\Amazon\ECS\log\ecs-agent.log", "Machine")
[Environment]::SetEnvironmentVariable("ECS_DATADIR", "${env:ProgramData}\Amazon\ECS\data", "Machine")
# Download the agent
$agentVersion = "latest"
$agentZipUri = "https://s3.amazonaws.com/amazon-ecs-agent/ecs-agent-windows-$agentVersion.zip"
$zipFile = "${env:TEMP}\ecs-agent.zip"
Invoke-RestMethod -OutFile $zipFile -Uri $agentZipUri
# Put the executables in the executable directory.
Expand-Archive -Path $zipFile -DestinationPath $ecsExeDir -Force
Set-Location ${ecsExeDir}
# Set $EnableTaskIAMRoles to $true to enable task IAM roles
# Note that enabling IAM roles will make port 80 unavailable for tasks.
[bool]$EnableTaskIAMRoles = $false
if (${EnableTaskIAMRoles}) {
  $HostSetupScript = Invoke-WebRequest https://raw.githubusercontent.com/aws/amazon-ecs-agent/master/misc/windows-deploy/hostsetup.ps1
  Invoke-Expression $($HostSetupScript.Content)
}
# Install the agent service
New-Service -Name "AmazonECS" `
        -BinaryPathName "$ecsExeDir\amazon-ecs-agent.exe -windows-service" `
        -DisplayName "Amazon ECS" `
        -Description "Amazon ECS service runs the Amazon ECS agent" `
        -DependsOn Docker `
        -StartupType Manual
sc.exe failure AmazonECS reset=300 actions=restart/5000/restart/30000/restart/60000
sc.exe failureflag AmazonECS 1
Start-Service AmazonECS
</powershell>
```