• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Packer
  • Install
  • Tutorials
  • Documentation
  • Guides
  • Plugins
  • Try Cloud(opens in new tab)
  • Sign up
Integrations

Skip to main content
2 tutorials
  • Post-Processors - Vagrant
  • Build a Windows Image

  • Resources

  • Tutorial Library
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  1. Developer
  2. Packer
  3. Tutorials
  4. Integrations
  5. Post-Processors - Vagrant

Post-Processors - Vagrant

  • 5min

  • PackerPacker

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 named docker-ubuntu.pkr.hcl.

    packer {
      required_plugins {
        amazon = {
          version = ">= 0.0.1"
          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-xenial-16.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}-${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 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.

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" {}
}

Build and create Vagrant box

Build the images.

$ packer build .
mazon-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 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.

post-processors {
  post-processor "vagrant" {}
  post-processor "compress" {}
}

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.

 Back to Collection
 Next

This tutorial also appears in:

  •  
    6 tutorials
    Getting Started with AWS
    HashiCorp Packer automates the creation of any type of machine image, including AWS AMIs. You'll build an Ubuntu machine image on AWS in this tutorial.
    • Packer

On this page

  1. Post-Processors - Vagrant
  2. Prerequisites
  3. Add post-processor to template
  4. Build and create Vagrant box
  5. Sequential post-processing steps
  6. Managing the Image
  7. Next steps
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)