我有一个非常简单的PowerShell脚本,它将生成的测试文件从Windows 2008 R2 Datacenter服务器(干净的AWS实例)上传到AWS S3存储桶。如果我使用Terraform(remote-exec
配置程序)在服务器上远程运行脚本,则脚本在使用StackOverflowException
的S3上载时失败。当我直接在服务器上运行脚本时,它运行正常并上传文件。
我已经尝试了不同大小的文件,14.5MB似乎是在StackOverflowException发生之前有效的最大值。当我RDP到服务器并直接运行脚本时,几乎任何大小都可以正常工作。我已经测试了200MB并且工作正常。
知道为什么会这样或者我能做些什么来修复它?我需要上传的实际文件是50MB。
以下是重现问题的基本部分。 terraform.tf
档案:
resource "aws_instance" "windows" {
count = "1"
ami = "ami-e935fc94" #base win 2008 R2 datacenter
instance_type = "t2.micro"
connection {
type = "winrm"
user = "<username>"
password = "<password>"
timeout = "30m"
}
provisioner "file" {
source = "windows/upload.ps1"
destination = "C:\\scripts\\upload.ps1"
}
provisioner "remote-exec" {
inline = [
"powershell.exe -File C:\\scripts\\upload.ps1"
]
}
}
PowerShell脚本非常简单。 upload.ps1
:
$f = new-object System.IO.FileStream C:\Temp\test.dat, Create, ReadWrite
$f.SetLength(40MB) # change this to 14.5MB and it works!
$f.Close()
Write-S3Object -BucketName "mybucket" -Folder "C:\Temp" -KeyPrefix "20180322" -SearchPattern "*.dat"
从Terraform(remote-exec
配置程序)启动脚本时收到的错误:
aws_instance.windows (remote-exec): Process is terminated due to StackOverflowException.
在服务器上从RDP运行upload.ps1
可以正常工作,包括更大的文件(测试高达200MB)。
以下是版本信息:
Microsoft Windows Server 2008 R2 Datacenter
Powershell Version: 3.0
AWS Tools for Windows PowerShell, Version 3.3.245.0
Amazon Web Services SDK for .NET, Core Runtime Version 3.3.21.15
答案 0 :(得分:0)
此问题由Windows bug引起。对于标准的Windows服务器来说,这一切都很好 - 你可以修补并继续前进。但是,使用Terraform的AWS自动化更加棘手。
理想的解决方案将允许1)使用基本AMI,2)将修补程序应用于自身,3)然后运行WinRM remote-exec,全部来自Terraform。另一种解决方案是创建安装了修补程序的AMI,并让Terraform使用该AMI生成实例。但是,那么你就不能维持AMI了。
通常情况下,我使用过滤器获取Microsoft提供的基础AMI:
data "aws_ami" "windows2008" {
most_recent = true
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "name"
values = ["Windows_Server-2008-R2_SP1-English-64Bit-Base*",]
}
owners = ["801119661308", "amazon"]
}
然后我使用该AMI创建AWS实例:
resource "aws_instance" "windows" {
count = "1"
ami = "${data.aws_ami.windows2008.id}"
...
}
但是,基础AMI没有安装hotfix,允许您避免此WinRM / Windows错误。这是不是很棘手。
您可以使用userdata脚本执行多阶段设置。在实例的第一次启动(阶段1)中,我们将阻止实例,以便远程执行在我们准备好之前不会进入。然后,我们将下载并安装此修补程序,我们将重新启动(感谢Niklas Akerlund,Micky Balladelli和Techibee)。在第二次启动时(在描述的方法here中),我们将解除对实例的阻止(启用WinRM),以便远程执行程序可以连接。
这是我的userdata / PowerShell脚本:
$StateFile = "C:\Temp\userdata_state.txt"
If(-Not (Test-Path -Path $StateFile))
{
# PHASE 1
# Close the instance to WinRM connections until instance is ready (probably already closed, but just in case)
Start-Process -FilePath "winrm" -ArgumentList "set winrm/config/service/auth @{Basic=`"false`"}" -Wait
# Set the admin password for WinRM connections
$Admin = [adsi]("WinNT://./Administrator, user")
$Admin.psbase.invoke("SetPassword", "${tfi_rm_pass}")
# Create state file so after reboot it will know
New-Item -Path $StateFile -ItemType "file" -Force
# Make it so that userdata will run again after reboot
$EC2SettingsFile="C:\Program Files\Amazon\Ec2ConfigService\Settings\Config.xml"
$Xml = [xml](Get-Content $EC2SettingsFile)
$XmlElement = $Xml.get_DocumentElement()
$XmlElementToModify = $XmlElement.Plugins
Foreach ($Element in $XmlElementToModify.Plugin)
{
If ($Element.name -eq "Ec2HandleUserData")
{
$Element.State="Enabled"
}
}
$Xml.Save($EC2SettingsFile)
# Download and install hotfix
# Download self-extractor
$DownloadUrl = "https://hotfixv4.trafficmanager.net/Windows%207/Windows%20Server2008%20R2%20SP1/sp2/Fix467402/7600/free/463984_intl_x64_zip.exe"
$HotfixDir = "C:\hotfix"
$HotfixFile = "$HotfixDir\KB2842230.exe"
mkdir $HotfixDir
(New-Object System.Net.WebClient).DownloadFile($DownloadUrl, $HotfixFile)
# Extract self-extractor
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($HotfixFile, $HotfixDir)
# Install - NOTE: wusa returns immediately, before install completes, so you must check process to see when it finishes
Get-Item "$HotfixDir\*.msu" | Foreach { wusa ""$_.FullName /quiet /norestart"" ; While (@(Get-Process wusa -ErrorAction SilentlyContinue).Count -ne 0) { Start-Sleep 3 } }
# Reboot
Restart-Computer
}
Else
{
# PHASE 2
# Open WinRM for remote-exec
Start-Process -FilePath "winrm" -ArgumentList "quickconfig -q"
Start-Process -FilePath "winrm" -ArgumentList "set winrm/config/service @{AllowUnencrypted=`"true`"}" -Wait
Start-Process -FilePath "winrm" -ArgumentList "set winrm/config/service/auth @{Basic=`"true`"}" -Wait
Start-Process -FilePath "winrm" -ArgumentList "set winrm/config @{MaxTimeoutms=`"1900000`"}"
}