title: Build Windows EC2 Image with Packer
body: <p>when running packer build, I am getting Error while <code>Waiting for WinRM to become available...</code> and failing after 15 minutes. Any suggestions, please!</p>
  
  <p>I am using <code>packer -version --&gt; 1.5.5</code></p>
  
  <p>Running the packer build as <code>packer build packer-aws-windows-ami.json</code></p>
  
  <p>Details are given below:-  </p>
  
  <p><strong>Execution Logs:</strong></p>
  
  <pre><code>amazon-ebs: output will be in this color.
  
  ==&gt; amazon-ebs: Prevalidating any provided VPC information
  ==&gt; amazon-ebs: Prevalidating AMI Name: Nigam-Packer-Win2016-Test
      amazon-ebs: Found Image ID: ami-0b2ce977c1b36856d
  ==&gt; amazon-ebs: Creating temporary keypair: packer_5ea79a20-dd01-a7d5-6760-xa134e097ff1
  ==&gt; amazon-ebs: Launching a source AWS instance...
  ==&gt; amazon-ebs: Adding tags to source instance
      amazon-ebs: Adding tag: "Name": "Packer Builder"
      amazon-ebs: Instance ID: i-025xebcexa6b7cc28
  ==&gt; amazon-ebs: Waiting for instance (i-025xebcexa6b7cc28) to become ready...
  ==&gt; amazon-ebs: Skipping waiting for password since WinRM password set...
  ==&gt; amazon-ebs: Using winrm communicator to connect: 16.226.144.187
  ==&gt; amazon-ebs: Waiting for WinRM to become available...
  ==&gt; amazon-ebs: Timeout waiting for WinRM.
  ==&gt; amazon-ebs: Terminating the source AWS instance...
  ==&gt; amazon-ebs: Cleaning up any extra volumes...
  ==&gt; amazon-ebs: No volumes to clean up, skipping
  ==&gt; amazon-ebs: Deleting temporary keypair...
  Build 'amazon-ebs' errored: Timeout waiting for WinRM.
  
  ==&gt; Some builds didn't complete successfully and had errors:
  --&gt; amazon-ebs: Timeout waiting for WinRM.
  
  ==&gt; Builds finished but no artifacts were created.
  </code></pre>
  
  <p><strong>packer-aws-windows-ami.json</strong></p>
  
  <pre><code>{
    "variables": {
      "build_version": "{{isotime \"2020.04.29.103307\"}}",
      "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
      "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
      "region": "us-east-2",
      "instance_type": "t3.large",
      "vpc_id": "myvpc",
      "subnet_id": "mysubnet",
      "ssh_keypair_name": "./key.pem",
      "security_group_id": "sg-id",
      "user_data_file": "userdata.txt",
      "name": "Packer_AMI_TEST"
    },
    "builders": [
        {
          "type": "amazon-ebs",
          "access_key": "{{ user `aws_access_key` }}",
          "secret_key": "{{ user `aws_secret_key` }}",
          "region": "{{ user `region` }}",
          "ami_name": "Test_Packer-Win-{{timestamp}}",
          "source_ami_filter": {
            "filters": {
              "name": "Windows_Server-2016-English-Full-Base-*",
              "root-device-type": "ebs",
              "virtualization-type": "hvm" },
            "most_recent": true,
            "owners": ["amazon"]
          },
          "instance_type": "{{user `instance_type`}}",
          "vpc_id": "{{user `vpc_id`}}",
          "subnet_id": "{{user `subnet_id`}}",
          "security_group_id": "{{user `security_group_id`}}",
          "user_data_file": "{{user `user_data_file`}}",
          "ami_name": "Nigam-Packer-Win2016-Test",
          "ssh_private_key_file": "{{user `ssh_keypair`}}",
          "associate_public_ip_address": true,
          "communicator": "winrm",
          "winrm_username": "Administrator",
          "winrm_password": "SuperS3cr3t!",
          "winrm_timeout": "15m",
          "winrm_use_ssl": true,
          "winrm_insecure": true
        }
    ],
    "provisioners": [
      {
        "type": "powershell",
        "inline": [
          "Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole",
          "Enable-WindowsOptionalFeature -Online -FeatureName IIS-WebServer"
        ]
      },
      {
        "type": "windows-restart",
        "restart_check_command": "powershell -command \"&amp;amp; {Write-Output 'Machine restarted.'}\""
      },
      {
        "type": "powershell",
        "inline": [
          "C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\InitializeInstance.ps1 -Schedule",
          "C:\\ProgramData\\Amazon\\EC2-Windows\\Launch\\Scripts\\SysprepInstance.ps1 -NoShutdown"
        ]
      }
  ]
  
  }
  
  </code></pre>
  
  <p><strong>userdata.txt</strong></p>
  
  <pre><code># Create username and password
  net user Administrator SuperS3cr3t!!!
  wmic useraccount where "name='Administrator'" set PasswordExpires=FALSE
  
  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
  
  # Create a self-signed certificate to let ssl work
  $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"
  
  # Configure WinRM to allow unencrypted communication, and provide the
  # self-signed cert to the WinRM listener.
  cmd.exe /c winrm quickconfig -q
  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)`"}"
  
  # Make sure appropriate firewall port openings exist
  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"
  
  # Restart WinRM, and set it so that it auto-launches on startup.
  cmd.exe /c net stop winrm
  cmd.exe /c sc config winrm start= auto
  cmd.exe /c net start winrm
  </code></pre>