DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > Cloud Patterns: Baking VMs With Packer

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.

David Karapetyan user avatar by
David Karapetyan
·
Jan. 07, 17 · Cloud Zone · Tutorial
Like (1)
Save
Tweet
4.48K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Cloud Baking

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deploying Java Applications to AWS Elastic Beanstalk
  • Getting Started Building on the NEAR Network with Infura
  • What Is Edge Compute? It’s Kind of Like Knitting Dog Hats
  • Key Design Elements for IoT Sensors

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo