Packer, WinRM, AMIs - making it work
This has been a pretty frustrating task for me, so it felt like a good thing to write about. Pretty much every source had one thing or another wrong, and I'd ended up just mashing it all into one big huge WinRM script glob.
I would preface this with 'it's a packer build, not concerned with security and encryption', which is not terribly security-conscious of me, but here we are. One of the articles I found whilst googling had a basic SSL setup, but I opted to start with the Packer documentation after many many failures.
If you start with said documentation here (https://www.packer.io/intro/getting-started/build-image.html#a-windows-example) you will (possibly) discover that you are still blocked by 'waiting for winrm'. So add this to your user-data WinRM setup portion:
Yay, WinRM manages to connect!
But then you get to the end, and this happens:
Then, by a stroke of luck(?) our main build script (a powershell script provisioner) silently died, and let the rest of the process finish - and it finished successfully! So it's definitely something in our build scripts breaking the process. The following principles were applied:
For some context, here is the process we are using (with additional output sent to Slack):
I would preface this with 'it's a packer build, not concerned with security and encryption', which is not terribly security-conscious of me, but here we are. One of the articles I found whilst googling had a basic SSL setup, but I opted to start with the Packer documentation after many many failures.
If you start with said documentation here (https://www.packer.io/intro/getting-started/build-image.html#a-windows-example) you will (possibly) discover that you are still blocked by 'waiting for winrm'. So add this to your user-data WinRM setup portion:
(haven't extensively tested the above, but I strongly suspect it's the 3rd line that does it)set-item WSMan:\localhost\Client\AllowUnencrypted -Value True -Forceset-item WSMan:\localhost\Client\Auth\Basic -Value True -Forceset-item WSMan:\localhost\Client\TrustedHosts -Value * -ForceEnable-PSRemoting -force
Yay, WinRM manages to connect!
But then you get to the end, and this happens:
Reading up on it, this is an error due to WinRM getting interrupted at the end of the build (because the build has, indeed, finished correctly). This is extra irritating because installing all the garbage required by a build agent takes a good 40m.Build 'amazon-ebs' errored: Error processing command: Error uploading ps script containing env vars: Couldn't determine whether destination was a folder or file: unknown error Post http://IP.AD.DR.ES:5985/wsman: read tcp MYAD.DR.ES:63667->IP.AD.DR.ES:5985: read: connection reset by peer==> Some builds didn't complete successfully and had errors:--> amazon-ebs: Error processing command: Error uploading ps script containing env vars: Couldn't determine whether destination was a folder or file: unknown error Post http://IP.AD.DR.ES:5985/wsman: read tcp MY.AD.DR.ES:63667->IP.AD.DR.ES:5985: read: connection reset by peer
Then, by a stroke of luck(?) our main build script (a powershell script provisioner) silently died, and let the rest of the process finish - and it finished successfully! So it's definitely something in our build scripts breaking the process. The following principles were applied:
- If there is to be a system restart, let Packer handle it
- Be heavy on the output so you can follow along
- Don't rely on "it worked during debug"
For some context, here is the process we are using (with additional output sent to Slack):
- User-data script sets up WinRM for Packer
- Powershell script provisioner
- Clones our AWS config repo which has a pile of powershell scripts
- Gets EC2 Tags - 'additionalBuildSteps' where we can define 'buildAgent'
- It executes the collection of scripts related to 'allServers' and 'additionalBuildSteps'
- Powershell 'windows-restart' provisioner
- Powershell script provisioner
- EC2 Launch sysprep scripts
For reference, this blog article was the best/most relevant/recent: https://david-obrien.net/2016/12/packer-and-aws-windows-server-2016/
Comments
Post a Comment