Post-processors - Vagrant
In the last tutorial, you updated your template to build multiple images in parallel.
While provisioners are run against an instance while it is running, post-processors run only after Packer saves the instance as an image. Post-processors are extremely varied in their function; they can compress your artifact, upload your artifact into a cloud, or create a file that describes the artifact and build.
In this tutorial, you will add the vagrant post-processor to create a Vagrant box from your AMI.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorials. If not, follow the steps below before continuing.
Install Packer
Create a directory named
packer_tutorial
and paste the following configuration into a file nameddocker-ubuntu.pkr.hcl
.Initialize the Packer template.
Once you have successfully initialized the template, you can continue with the rest of this tutorial.
Add post-processor to template
Add the following post-processor block inside the build block but after the provisioner step. These post-processor blocks will tag each image with the appropriate tags.
Your build block should look like the following.
Build and create Vagrant box
Build the images.
List the files in your current directory. You should find Vagrant box files.
Notice how there are only three images. This is because the ubuntu-bionic
post-processing step overrid the image created by ubuntu-xenial
. You can verify this by going through the Packer build logs.
Sequential post-processing steps
You may add as many post-processors as you want using the post-processor
syntax, but each one will start from the original artifact output by the builder, not the
artifact created by a previously-declared post-processor.
Use the post-processors
(note the pluralization) block to create post-processing pipelines where the output of one post-processor becomes the input to another post-processor.
For example, the following configuration will create a Vagrant box and then compress it. You do not need to add this to your configuration.
Note that you can add as many post-processors blocks as you want, or mix-and-match the one-off post-processors with the "post-processors" blocks to create multiple pathways for post-processing.
Managing the Image
Packer only builds images. It does not attempt to manage them in any way. After they're built, it is up to you to launch or destroy them as you see fit.
After running the above example, your AWS account now has an AMI associated with it. AMIs are stored in S3 by Amazon so you may be charged.
You can remove the AMI by first deregistering it on the AWS AMI management page. Next, delete the associated snapshot on the AWS snapshot management page.
Next steps
In this tutorial, you added post-processors to your Packer template to create a Vagrant box and compress it.
This is the end of the getting started tutorials for Packer. You should now be comfortable with basic Packer usage, should understand templates, defining builds, provisioners, post-processors, and more. At this point, you are ready to begin playing with and using Packer in real scenarios.
The most important reference in your Packer journey will be the documentation. The documentation contains reference material for Packer's features and configuration options.
As you use Packer more, please voice your comments and concerns on the community forum. Additionally, Packer is open source so please contribute if you'd like to. Contributions are very welcome.