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 Vagrant boxes from your AMIs.
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
.packer { required_plugins { amazon = { version = ">= 1.2.8" source = "github.com/hashicorp/amazon" } }} variable "ami_prefix" { type = string default = "learn-packer-linux-aws-redis"} locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "")} source "amazon-ebs" "ubuntu" { ami_name = "${var.ami_prefix}-${local.timestamp}" instance_type = "t2.micro" region = "us-west-2" source_ami_filter { filters = { name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*" root-device-type = "ebs" virtualization-type = "hvm" } most_recent = true owners = ["099720109477"] } ssh_username = "ubuntu"} source "amazon-ebs" "ubuntu-focal" { ami_name = "${var.ami_prefix}-focal-${local.timestamp}" instance_type = "t2.micro" region = "us-west-2" source_ami_filter { filters = { name = "ubuntu/images/*ubuntu-focal-20.04-amd64-server-*" root-device-type = "ebs" virtualization-type = "hvm" } most_recent = true owners = ["099720109477"] } ssh_username = "ubuntu"} build { name = "learn-packer" sources = [ "source.amazon-ebs.ubuntu", "source.amazon-ebs.ubuntu-focal" ] provisioner "shell" { environment_vars = [ "FOO=hello world", ] inline = [ "echo Installing Redis", "sleep 30", "sudo apt-get update", "sudo apt-get install -y redis-server", "echo \"FOO is $FOO\" > example.txt", ] } provisioner "shell" { inline = ["echo This provisioner runs last"] }}
Initialize the Packer template.
$ packer init .
Once you have successfully initialized the template, you can continue with the rest of this tutorial.
Add post-processor to template
Add the vagrant
post-processor block inside the build block but after the provisioner step. This post-processor block will create a Vagrant box from each AMI.
post-processor "vagrant" {}
Your build block should look like the following.
build { name = "learn-packer" sources = [ "source.amazon-ebs.ubuntu", "source.amazon-ebs.ubuntu-focal" ] provisioner "shell" { environment_vars = [ "FOO=hello world", ] inline = [ "echo Installing Redis", "sleep 30", "sudo apt-get update", "sudo apt-get install -y redis-server", "echo \"FOO is $FOO\" > example.txt", ] } provisioner "shell" { inline = ["echo This provisioner runs last"] } post-processor "vagrant" {}}
Next add the Vagrant plugin to the required_plugins
block.
vagrant = { version = ">= 1.1.1" source = "github.com/hashicorp/vagrant"}
Your packer
block should look like the following.
packer { required_plugins { amazon = { version = ">= 1.2.8" source = "github.com/hashicorp/amazon" } vagrant = { version = ">= 1.1.1" source = "github.com/hashicorp/vagrant" } }}
Build and create Vagrant box
Initialize the Packer configuration to download the Vagrant plugin that you added.
$ packer init .
Next, build the images.
$ packer build .learn-packer.amazon-ebs.ubuntu: output will be in this color.learn-packer.amazon-ebs.ubuntu-focal: output will be in this color. ==> learn-packer.amazon-ebs.ubuntu-focal: Prevalidating any provided VPC information==> learn-packer.amazon-ebs.ubuntu-focal: Prevalidating AMI Name: learn-packer-linux-aws-redis-focal-20210429173057==> learn-packer.amazon-ebs.ubuntu: Prevalidating any provided VPC information==> learn-packer.amazon-ebs.ubuntu: Prevalidating AMI Name: learn-packer-linux-aws-redis-20210429173057 learn-packer.amazon-ebs.ubuntu-focal: Found Image ID: ami-0554443e7f55d3eeb### ... ==> Wait completed after 8 minutes 47 seconds ==> Builds finished. The artifacts of successful builds are:--> learn-packer.amazon-ebs.ubuntu: AMIs were created:us-west-2: ami-0e3795bd3628d430b --> learn-packer.amazon-ebs.ubuntu-focal: AMIs were created:us-west-2: ami-014a9d431eddddc2d
List the files in your current directory. You should find Vagrant box files.
$ ls -l-rw-r--r-- 1 youruser staff 1.6K Apr 29 10:41 aws-ubuntu.pkr.hcl-rw-r--r-- 1 youruser staff 385B Apr 29 10:45 packer_ubuntu-focal_aws.box-rw-r--r-- 1 youruser staff 385B Apr 29 10:45 packer_ubuntu_aws.box
Notice that there are two Vagrant boxes, one for each AMI that you built.
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.
post-processors { post-processor "vagrant" {} post-processor "compress" {}}
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.