首页 文章

在AWS上使用Terraform安装Windows HotFix

提问于
浏览
1

我有一个非常简单的PowerShell脚本,它将生成的测试文件从Windows 2008 R2 Datacenter服务器(干净的AWS实例)上传到AWS S3存储桶 . 如果我使用Terraform( remote-exec provisioner)在服务器上远程运行脚本,则脚本在使用 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 provisioner)启动脚本时收到的错误:

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

1 回答

  • 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}"
      ...
    }
    

    But, 基础AMI没有安装hotfix,允许您避免此WinRM / Windows错误 . 这是棘手的 .

    您可以使用userdata脚本执行多阶段设置 . 在实例的第一次启动(阶段1)中,我们'll block the instance so that the remote-exec doesn' t在我们下载并安装此修补程序之前进入并且我们将重新启动(感谢Niklas AkerlundMicky BalladelliTechibee) . 在第二次启动时(在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`"}"
    }
    

相关问题