首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在AWS上使用Terraform安装Windows HotFix

在AWS上使用Terraform安装Windows HotFix
EN

Stack Overflow用户
提问于 2018-03-22 18:06:42
回答 1查看 1.3K关注 0票数 2

我有一个非常简单的PowerShell脚本,它将生成的测试文件从Windows2008AWS数据中心服务器(干净的S3实例)上传到AWS S3桶。如果我使用Terraform (remote-exec provisioner)远程在服务器上运行脚本,那么脚本将在带有StackOverflowException的S3上传时失败。当我直接在服务器上运行脚本时,它运行良好并上传文件。

我已经对文件进行了不同大小的实验,14.5MB似乎是StackOverflowException出现之前的最大容量。当我将RDP放入服务器并直接运行脚本时,任何大小都可以正常工作。我测试了200 it,运行良好。

知道为什么会发生这种事吗?或者我能做些什么来解决这个问题?我需要上传的实际文件是50 is。

以下是重现这个问题的关键部分。terraform.tf文件:

代码语言:javascript
复制
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

代码语言:javascript
复制
$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提供程序)启动脚本时收到的错误:

代码语言:javascript
复制
aws_instance.windows (remote-exec): Process is terminated due to StackOverflowException.

在服务器本身上从RDP运行upload.ps1可以正常工作,包括更大的文件(测试最多可达200 up )。

以下是版本信息:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-31 03:06:51

这个问题是由窗口错误造成的。对于一个标准的Windows服务器来说,这一切都很好,而且很好--您可以修补并继续前进。但是,在使用Terraform的AWS自动化方面,情况要复杂得多。

理想的解决方案是: 1)使用基本AMI;2)将修补程序应用于自身;3)然后运行WinRM remote-exec,所有这些都来自Terraform。另一种解决方案是使用安装好的修补程序创建一个AMI,并让Terraform使用该AMI生成实例。然而,你却被困在维护非盟驻苏特派团了。

通常,我使用过滤器获取Microsoft提供的基本AMI:

代码语言:javascript
复制
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实例:

代码语言:javascript
复制
resource "aws_instance" "windows" {
  count                       = "1"
  ami                         = "${data.aws_ami.windows2008.id}"
  ...
}

但是, --基本AMI没有安装热修复 --允许您避免这个WinRM/Windows。如果事情变得棘手的话。

您可以使用userdata脚本来执行多阶段设置。在实例的第一次引导(第一阶段)中,我们将阻止该实例,以便远程执行程序在准备就绪之前不会出现。然后,我们将下载并安装修补程序,然后重新启动(多亏了尼可拉斯·阿克伦德米基·巴拉德利泰奇比)。在第二次引导时(在描述这里的方法中),我们将解除对实例(启用WinRM)的阻塞,以便远程执行部分能够连接。

下面是我的userdata/PowerShell脚本:

代码语言:javascript
复制
$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`"}"
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49435628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档