3

I'm trying to build a Windows AMI with packer.io.

In the powershell file I have run the following command as the last command of the provisioning step:

Write-Host "Running the EC2Config.exe file to sysprep the image for UserData to run on next boot."
cmd.exe /c "C:\Program Files\Amazon\Ec2ConfigService\ec2config.exe" -sysprep

In my packer output, I see the following text:

amazon-ebs: Running the EC2Config.exe file to sysprep the image for UserData to run on next boot.
amazon-ebs: Running in foreground...
amazon-ebs: SysprepUtils: Setting bundle/Sysprep operations for Vista/2008.
amazon-ebs: SysprepUtils: Reading Bundle Properties from C:\Program Files\Amazon\Ec2ConfigService\Settings\BundleConfig.xml
amazon-ebs: SysprepUtils: Processing property: AutoSysprep
amazon-ebs: SysprepUtils: Processing property: SetRDPCertificate
amazon-ebs: SysprepUtils: Changing plugin Ec2ConfigureRDP state to Disabled
amazon-ebs: SysprepUtils: Changing plugin Ec2OutputRDPCert state to Enabled
amazon-ebs: SysprepUtils: Processing property: SetPasswordAfterSysprep
amazon-ebs: SysprepUtils: Changing plugin Ec2SetPassword state to Enabled
amazon-ebs: SysprepUtils: Changing plugin Ec2WindowsActivate state to Enabled
amazon-ebs: SysprepUtils: Changing plugin Ec2HandleUserData state to Enabled
amazon-ebs: SysprepUtils: Changing plugin Ec2DynamicBootVolumeSize state to Enabled
amazon-ebs: SysprepUtils: Sysprep command: 'C:\windows\system32\sysprep\sysprep.exe'
amazon-ebs: SysprepUtils: Sysprep args: '"/unattend:C:\Program Files\Amazon\Ec2ConfigService\sysprep2008.xml" /oobe /shutdown /generalize'
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: No AMIs to cleanup
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' errored: Script exited with non-zero exit status: 1. Allowed exit codes are: [0]

So...

It's running successfully, and then running sysprep, but sysprep is causing packer to see that it isn't ran successfully. I've tried running packer with -force, but, that doesn't seem to allow me to build the images successfully.

I'm kind of at a loss here -- I'm not sure if the easiest way is for me to try to force packer to see that an image is being built, or, to try to add another step at the end of my powershell script for packer to try to force that result to have a good error code and move forward.

Ian Wilson
  • 226
  • 2
  • 6
  • Did you ever solve this issue? I haven't had a problem with the Ec2ConfigService, but the new Ec2Launch (http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch.html#ec2launch-sysprep) script provided with Windows 2016 instances is causing the same problem. – Castrohenge Jan 09 '17 at 11:42

2 Answers2

2

Found this question while searching for an answer to my own question of 'sysprepping prior to Packer shutting down the build instance', and it gave me just the right clue to solve the issue.

Turns out there is an optional parameter to Packer's PowerShell provisioner called valid_exit_codes. Since you're seeing an exit code of 1, set valid_exit_codes to "0,1". In your Packer windows.json:

"valid_exit_codes": "0,1"

I don't know how new this is; I'm using Packer 0.12.2.


My Results

Interestingly, my ec2config.exe -sysprep run got an exit of 0:

amazon-ebs: SysprepUtils: Sysprep args: '"/unattend:C:\Program Files\Amazon\Ec2ConfigService\sysprep2008.xml" /oobe /shutdown /generalize'

2017/01/23 20:18:33 packer: 2017/01/23 20:18:33 [INFO] command 'powershell -executionpolicy bypass -encodedCommand aQBmACAAKAB ..deleted.. GUAcwB0AC0A=' exited with code: 0

Another Possible Option

One other thing you may want to consider trying (I haven't yet due to time constraints):

Remove the /shutdown flag from the sysprep arguments. You can find that in the C:\Program Files\Amazon\Ec2ConfigService\Settings\BundleConfig.xml. Just use a PowerShell script to do a find and replace.

Hope this helps!

KJH
  • 372
  • 1
  • 14
0

If a shutdown is initiated in the target machine, we have seen instances on the latest Packer binary (0.10.1) where it would Terminate with a similar error.

You should be able to just us the args /generalize /oobe /quiet and then Packer will handle the shutdown before it generates the AMI.

Brennen Smith
  • 1,638
  • 7
  • 11
  • Unfortunately when using the AWS Ec2ConfigService this uses shutdown by default and cannot be modified. – Castrohenge Jan 09 '17 at 11:37
  • @Castrohenge - it does, but you can modify it. It's in the file `C:\Program Files\Amazon\Ec2ConfigService\Settings\BundleConfig.xml` and if you're using Packer, you can pass in a script to modify the file. – KJH Jan 23 '17 at 20:57