Build a Windows image
You can use Packer to build Amazon Machine Images (AMIs) for any supported operating system. In this tutorial, you will use an existing AMI managed by AWS as the base image for your own AMI that you will customize using scripts and build templates. By building machine images to include your team's required tools and system settings, you can shorten the time it takes to deploy new instances.
Prerequisites
This tutorial assumes that you are familiar with the Packer workflow. If you are new to Packer, complete the Get Started tutorials first.
For this tutorial, you will need:
- Packer 1.7.10+ installed locally.
- An AWS account with credentials configured for Packer. Your user must have permissions to create, modify and delete EC2
instances. Refer to the
documentation
to find the full list IAM permissions required to run the
amazon-ebs
builder.
Tip
AWS charges for storing AMIs and any snapshots. Deregister the AMI and delete its snapshots at the end of the tutorial to avoid incurring unnecessary charges.
Clone repository
Clone the example repository for this tutorial, which contains Packer templates to build a Windows AMI.
Change into the repository directory.
Review configuration
This configuration modifies an existing Windows base image managed by AWS. To build your AMI, Packer launches a build instance that runs the base image, connects to it to execute your build scripts, then takes a snapshot of your instance to create the AMI.
The Windows base image does not automatically allow ingress traffic, which Packer requires in order to connect to the instance. To work around this, the configuration specifies a user data script that will run on the build instance at launch and allow connections over WinRM.
Open bootstrap_win.txt
to review the user data script.
Warning
If you enable WinRM to customize your images, be sure to disable it or restrict its permissions in your Packer shutdown script to secure your instance.
The <powershell>
and </powershell>
tags at the top and bottom of the file
instruct the instance to execute the script using PowerShell. You can also use
<script></script>
tags to enclose any commands that you would normally run in
a Command Prompt window. This script configures the build instance and the services
running on it.
Review the AWS documentation on running commands in Windows instances at launch to learn more about the startup script.
Now, open the windows.pkr.hcl
file to review the build template that instructs Packer how to build your new AMI.
First, the template defines the required plugins and versions, an input variable for the region, and a local variable for the build timestamp.
The configuration configures an Amazon EBS builder and uses the
source_ami_filter
to select an Amazon-managed base image for your build. It
also configures the instance to use your custom boot script as the
user_data_file
to enable Packer connections over WinRM.
Next, the build template uses provisioners to customize your image.
Note the three different provisioners:
- The first
powershell
provisioner uses theinline
argument to execute shorter snippets of code. - The
windows-restart
provisioner triggers a reboot, which may be necessary in your build scripts for changes to take effect. - The second
powershell
provisioner uses ascript
instead ofinline
, which is convenient if you have more complex or extensive customizations to apply.
Open sample_script.ps1
to review an example provisioner script.
This script prints out the variable values passed in by the builder.
Build the AMI
Build your new AMI by running packer build windows.pkr.hcl
. Packer will use your AWS credentials to authenticate and build your image. This process may take up to 30 minutes to complete.
Next, navigate to your AMI dashboard to confirm Packer created your new AMI.
Deregister AMI
To limit incurring charges, deregister your AMI in the AWS console. Select the new AMI, then select Deregister AMI from the Actions dropdown menu.
Then, delete any associated snapshots.
Next steps
In this tutorial, you used Packer to build a customized Windows AMI. You can replicate this pattern to build and customize AMIs with the tools and settings required by your team.
Review the following resources to learn more about building images with Packer:
- Review the build block documentation to review the options available for AMI customization.
- Learn how to build a golden image pipeline with HCP Packer
- Learn how to automate your AMI builds using GitHub Actions.