I’m trying to connect with windows AWS using winrm in packer to perform some provisioning.
I want to choose some other user other than “Administrator” for using winrm.
But I’m not able to do so
Also wanted to know why no custom user_data_file
is required in Azure windows and also winrm_username
can be any other thing other than “Administrator” in Azure windows image
For AWS windows packer file-
I’m using this user_data_file
<powershell>
write-output "Running User Data Script"
write-host "(host) Running User Data Script"
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force -ErrorAction Ignore
# Don't set this before Set-ExecutionPolicy as it throws an error
$ErrorActionPreference = "stop"
# Remove HTTP listener
Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
$Cert = New-SelfSignedCertificate -CertstoreLocation Cert:\LocalMachine\My -DnsName "packer"
New-Item -Path WSMan:\LocalHost\Listener -Transport HTTPS -Address * -CertificateThumbPrint $Cert.Thumbprint -Force
# WinRM
write-output "Setting up WinRM"
write-host "(host) setting up WinRM"
cmd.exe /c winrm quickconfig -q
cmd.exe /c winrm set "winrm/config" '@{MaxTimeoutms="1800000"}'
cmd.exe /c winrm set "winrm/config/winrs" '@{MaxMemoryPerShellMB="1024"}'
cmd.exe /c winrm set "winrm/config/service" '@{AllowUnencrypted="true"}'
cmd.exe /c winrm set "winrm/config/client" '@{AllowUnencrypted="true"}'
cmd.exe /c winrm set "winrm/config/service/auth" '@{Basic="true"}'
cmd.exe /c winrm set "winrm/config/client/auth" '@{Basic="true"}'
cmd.exe /c winrm set "winrm/config/service/auth" '@{CredSSP="true"}'
cmd.exe /c winrm set "winrm/config/listener?Address=*+Transport=HTTPS" "@{Port=`"5986`";Hostname=`"packer`";CertificateThumbprint=`"$($Cert.Thumbprint)`"}"
cmd.exe /c netsh advfirewall firewall set rule group="remote administration" new enable=yes
cmd.exe /c netsh firewall add portopening TCP 5986 "Port 5986"
cmd.exe /c net stop winrm
cmd.exe /c sc config winrm start= auto
cmd.exe /c net start winrm
</powershell>
And this is my packer builder for AWS windows
"builders": [
{
"access_key": "{{user `aws_access_key`}}",
"instance_type": "i3.large",
"secret_key": "{{user `aws_secret_key`}}",
"type": "amazon-ebs",
"name": "first",
"ami_name": "postgres-vanilla-automated-ami-{{timestamp}}",
"region": "ap-south-1",
"source_ami_filter": {
"filters": {
"name": "Windows_Server-2016-English-Full-Base-*",
"root-device-type": "ebs",
"virtualization-type": "hvm"
},
"most_recent": true,
"owners": [
"amazon"
]
},
"user_data_file": "../../../../tessell-packer-framework/scripts/windows_config_template/SetUpWinRM.ps1",
"communicator": "winrm",
"winrm_timeout": "5m",
"winrm_username": "Administrator",
"winrm_use_ssl": true,
"winrm_insecure": true
}
Error which I’m getting if I choose winrm_username=“Packer”
2023/04/11 22:33:11 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/11 22:33:11 Waiting for WinRM, up to timeout: 5m0s
==> first: Waiting for WinRM to become available...
2023/04/11 22:33:11 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/11 22:33:11 Using host value: 3.108.218.212
2023/04/11 22:33:11 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/11 22:33:11 [INFO] Attempting WinRM connection...
2023/04/11 22:33:11 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/11 22:33:11 [DEBUG] connecting to remote shell using WinRM
2023/04/11 22:33:22 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/11 22:33:22 [ERROR] connection error: http response error: 401 - invalid content type
2023/04/11 22:33:22 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/11 22:33:22 [ERROR] WinRM connection err: http response error: 401 - invalid content type
....