Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
Generating a Packer template from Scala using the Play Framework JSON library to allow annotations as comments.
package io.point.servermill
import play.api.libs.json.Json
object ServerMill extends App {
// generate a Packer template to build an AMI for a
// server instance on AWS.
// Some of this is cribbed from
// I learned a lot about chef-solo by messing
// with
// but may well not use it any time soon
// My main motivation for generating packer templates out of Scala was
// to make it possible to annotate the template (you can't put comments in JSON; who knew?)
// but other benefits may emerge.
// (Including having the ability to generate different templates
// and have them backed up in the project repo.)
// This also documents our external environmental dependencies.
// to use: In the Play console...
//[pointflow] $ run-main io.point.servermill.ServerMill target/packerTemplate
//[info] Updating {file:/C:/Users/maggie/Documents/}pointflow...
//[info] Done updating.
//[info] Compiling 2 Scala sources to C:\Users\maggie\Documents\\workflow\target\scala-2.10\classes...
//[warn] there were 1 feature warnings; re-run with -feature for details
//[warn] one warning found
//[info] Running io.point.servermill.ServerMill target/packerTemplate
//Packer template written to target/packerTemplate
//[success] Total time: 43 s, completed Feb 3, 2014 8:56:04 AM
val packerTemplate = Json.obj(
// these AWS credentials are set on the Packer command line
// and picked up in the amazon-ebs builder thus:
// packer build -var 'aws_access_key=XXXXXXXXXXXXXXXXXX' -var 'aws_secret_key=XXXXXXXXXXXXXXXXXXX' packerTemplate
"variables" -> Json.obj("aws_access_key" -> "", "aws_secret_key" -> ""),
"builders" -> Json.arr(
"type" -> "amazon-ebs",
// provide AWS user creds to provisioner
"access_key" -> "{{user `aws_access_key`}}",
"secret_key" -> "{{user `aws_secret_key`}}",
// ami-de0d9eb7 us-east-1 64-bit ebs Ubuntu 12.04.2 LTS (Precise Pangolin)
"region" -> "us-east-1",
"source_ami" -> "ami-de0d9eb7",
"instance_type" -> "t1.micro",
"ssh_username" -> "ubuntu",
"ami_name" -> "PointflowServer {{timestamp}}"
// provisioners: inject software packages needed by the app into the server image
"provisioners" -> Json.arr(
"type" -> "shell",
"inline" -> Json.arr(
"sleep 30", // allow time for the EC2 instance under construction to initialize
"sudo apt-get update",
"sudo apt-get install -y unzip",
"sudo apt-get install -y git",
"sudo apt-get install -y mc", // midnight commander; Maggie's favorite Unix shell tool
"sudo apt-get install -y openjdk-7-jdk", // Java
"sudo apt-get install -y scala", // Scala
"sudo wget", // Play
"sudo unzip -oq -d /opt",
"sudo mkdir /var/play", // where Pointflow goes
"cd /var/play",
"sudo git clone", // pull the source
"sudo /opt/play-2.1.5/play stage", // package for running
"sudo cp /var/play/workflow/conf/production.upstart /etc/init/play.conf" // configure play in upstart
val pw = new File(args head))
try pw.write(Json.prettyPrint(packerTemplate)) finally pw.close()
println("Packer template written to " + args.head)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment