Problem with packer in AWS windows

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
....

Hi, first i’d advise to use winrm_password in addition to your winrm_username.

Sometimes error 401 is related to winRM configuration. For instance, a misconfigured listener or so on…
I already got this issue, in my case, i let down https/port 5986 to first make http/5985 works.

I tried out with winrm_password="SuperS3cr3t!!!"

Then also it’s not working (stuck again at
==> first: Waiting for WinRM to become available...)

logs which I get

==> first: Authorizing access to port 5986 from [0.0.0.0/0] in the temporary security groups...
2023/04/12 09:08:37 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:08:37 [DEBUG] base64 encoding user data...
==> first: Launching a source AWS instance...
    first: Instance ID: i-067313f71842c654c
==> first: Waiting for instance (i-067313f71842c654c) to become ready...
==> first: Skipping waiting for password since WinRM password set...
==> first: Using WinRM communicator to connect: 35.154.66.122
==> first: Waiting for WinRM to become available...
2023/04/12 09:08:54 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:08:54 Waiting for WinRM, up to timeout: 5m0s
2023/04/12 09:08:54 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:08:54 Using host value: 35.154.66.122
2023/04/12 09:08:54 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:08:54 [INFO] Attempting WinRM connection...
2023/04/12 09:08:54 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:08:54 [DEBUG] connecting to remote shell using WinRM
2023/04/12 09:09:24 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:09:24 [ERROR] connection error: unknown error Post "https://35.154.66.122:5986/wsman": dial tcp 35.154.66.122:5986: i/o timeout
2023/04/12 09:09:24 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:09:24 [ERROR] WinRM connection err: unknown error Post "https://35.154.66.122:5986/wsman": dial tcp 35.154.66.122:5986: i/o timeout
2023/04/12 09:09:29 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:09:29 Using host value: 35.154.66.122
2023/04/12 09:09:29 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:09:29 [INFO] Attempting WinRM connection...
2023/04/12 09:09:29 packer-plugin-amazon_v1.2.1_x5.0_darwin_arm64 plugin: 2023/04/12 09:09:29 [DEBUG] connecting to remote shell using WinRM

Did you tried with port 5985 first instead of 5986 ?
Can you manually try from your side an enter-pssession to your host ? Maybe it’s a winrm config issue, maybe there’s a firewall filtering ? :slight_smile: