Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

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.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}