Over a million developers have joined DZone.

Cloud Patterns: Baking VMs With Packer

DZone's Guide to

Cloud Patterns: Baking VMs With Packer

Why install your VM components at runtime when you can just put them into a VM image once and let it go? Packer can help with that, no matter which cloud you choose.

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

When managing a cloud infrastructure, there are foundational components that pretty much all your VMs will need. For those foundational components, instead of installing them at runtime as the VM is starting, you should use Packer to just put them into a VM image once. This is a good practice in general because, if done right, it will reduce startup time and lead to a more efficient and consistent fleet for your cloud infrastructure.

The template I use is very simple and relies on uploading a package bundle as a TXZ file that contains a setup (setup.sh) script. The Packer provisioner unpacks that TXZ file after uploading it and runs the script. The template below is for AWS but can easily be adapted to any other cloud provider, since there is very little that is assumed about AWS other than some conventions around subnets and VPCs.

  "variables": {
    "ami": null,
    "name": null,
    "description": null,
    "keypair": null,
    "subnet": null,
    "purpose": null,
    "vpc": null,
    "sg1": null,
    "ssh_keyfile": null,
    "instance_type": null
  "builders": [{
    "type": "amazon-ebs",
    "region": "us-west-2",
    "ami_regions": ["us-west-1"],
    "source_ami": "{{user `ami`}}",
    "ami_name": "{{user `name`}} {{timestamp}}",
    "ami_description": "{{user `description`}}",
    "ssh_keypair_name": "{{user `keypair`}}",
    "ssh_private_ip": true,
    "subnet_id": "{{user `subnet`}}",
    "run_tags": {
    "tags": {
      "Purpose": "{{user `purpose`}}"
    "vpc_id": "{{user `vpc`}}",
    "security_group_ids": [
      "{{user `sg1`}}"
    "communicator": "ssh",
    "ssh_private_key_file": "{{user `ssh_keyfile`}}",
    "instance_type": "{{user `instance_type`}}",
    "ssh_username": "ubuntu"
  "provisioners": [
      "type": "file",
      "source": "package.txz",
      "destination": "/home/ubuntu/package.txz"
      "type": "shell",
      "inline": [
        "cd /home/ubuntu && tar xf package.txz",
        "cd /home/ubuntu && ./setup.sh"

Modifying it to work for other cloud providers is left as an exercise for the reader.

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

cloud ,packer ,cloud infrastructure ,vms ,tutorial

Published at DZone with permission of David Karapetyan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}