Packer - Window Restart Timeout Issue

I’m trying to build an AWS EC2 AMI using packer. As part of creating this image, I’m trying to achieve two things:

  1. Install .net 472 version as part of user data (as part of builder)
  2. Reboot machine using ‘Windows_restart’ provisioner

However, my script always fails with timeout error: [1;31mBuild ‘amazon-ebs’ errored: Timeout waiting for machine to restart I tried increasing restart_timeout to 15m, but it did not help.

Packer template:
{
“variables”: {
“aws_region”: “us-east-1”,
“aws_ami_name”: “ami-windows-netframework-packed-{{timestamp}}”
},
“builders”: [{
“type”: “amazon-ebs”,
“region”: “{{user aws_region}}”,
“instance_type”: “t2.micro”,
“ami_name”: “{{user aws_ami_name}}”,
“ami_description”: “Customized Golden image”,
“associate_public_ip_address”: “false”,
“subnet_id”: “subnet-",
“security_group_id”: "sg-
”,
“user_data_file”: “./packer-windows-netframework-user-data”,
“communicator”: “winrm”,
“winrm_username”: “Administrator”,
“ssh_interface”: “private_ip”,
“winrm_use_ssl”: true,
“winrm_insecure”: true,
“source_ami_filter”: {
“filters”: {
“virtualization-type”: “hvm”,
“name”: “amazon-full-base-windows*”,
“root-device-type”: “ebs”
},
“owners”: [“123456789”],
“most_recent”: true
}
}],

"provisioners": [
    {
      "type": "powershell",
      "environment_vars": ["SAMPLE_ENV=PACKER"],
      "inline": [
        "Write-Host \"THIS IMAGES GENERATED FROM $Env:SAMPLE_ENV\""
      ]
    },
    {
    "type": "windows-restart",
    "restart_timeout": "15m"
  }
  ]

}

Logs:
1592758745378,[1;32m==> amazon-ebs: Using winrm communicator to connect: 10.57.225.121[0m
1592758745378,[1;32m==> amazon-ebs: Waiting for WinRM to become available…[0m
1592758796212,[0;32m amazon-ebs: WinRM connected.[0m
1592758796212,[1;31m==> amazon-ebs: #< CLIXML[0m
1592758796213,"[1;31m==> amazon-ebs: <Objs Version="“1.1.0.1"” xmlns="“http://schemas.microsoft.com/powershell/2004/04"”><Obj S="“progress”" RefId="“0"”><TN RefId="“0"”>System.Management.Automation.PSCustomObjectSystem.Object<I64 N="“SourceId”">1<PR N="“Record”">Preparing modules for first use.0-1-1Completed-1 <Obj S="“progress”" RefId="“1"”><TNRef RefId="“0"” /><I64 N="“SourceId”">1<PR N="“Record”">Preparing modules for first use.0-1-1Completed-1 [0m"
1592758796213,[1;32m==> amazon-ebs: Connected to WinRM![0m
1592758796215,[1;32m==> amazon-ebs: Provisioning with Powershell…[0m
1592758796215,[1;32m==> amazon-ebs: Provisioning with powershell script: /tmp/powershell-provisioner533626044[0m
1592758799986,[0;32m amazon-ebs: THIS IMAGES GENERATED FROM PACKER[0m
1592758799988,[1;32m==> amazon-ebs: Restarting Machine[0m
1592758800259,[1;32m==> amazon-ebs: Waiting for machine to restart…[0m
1592758819084,[1;31m==> amazon-ebs: A system shutdown is in progress.(1115)[0m
1592759700260,[1;31m==> amazon-ebs: Timeout waiting for machine to restart.[0m
1592759700261,"[1;32m==> amazon-ebs: Provisioning step had errors: Running the cleanup provisioner, if present…[0m"
1592759700262,[1;32m==> amazon-ebs: Terminating the source AWS instance…[0m
1592759730817,[1;32m==> amazon-ebs: Cleaning up any extra volumes…[0m
1592759730890,"[1;32m==> amazon-ebs: No volumes to clean up, skipping[0m"
1592759730891,[1;32m==> amazon-ebs: Deleting temporary keypair…[0m
1592759731109,[1;31mBuild ‘amazon-ebs’ errored: Timeout waiting for machine to restart.[0m
1592759731110,==> Some builds didn’t complete successfully and had errors:
1592759731110,–> amazon-ebs: Timeout waiting for machine to restart.
1592759731110,==> Builds finished but no artifacts were created.

Has anyone encountered similar issue? Appreciate any suggestions.

have the same issue exactly with windows 2012 r2

Wow. This long and no answers. I had something similar happen, and this is what I had to do.

    {
      "type": "powershell",
      "elevated_user": "SYSTEM",
      "elevated_password": "",
      "max_retries": "3",
      "start_retry_timeout": "2m",
      "script": "../common/scripts/installWindowsUpdates.ps1"
    },
    {
      "type": "windows-restart",
      "restart_check_command": "powershell -command \"& {Write-Verbose 'restarted.' -Verbose}\"",
      "restart_timeout": "30m",
      "pause_before": "2m"
    },

typically after installing patches, I’d get the same error you were experiencing. Setting the restart_timeout to 30 minutes and doing a two minute pause before the reboot seemed to resolve the issue.