github.com/aspring/packer@v0.8.1-0.20150629211158-9db281ac0f89/website/source/intro/getting-started/parallel-builds.html.markdown (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Parallel Builds"
     4  prev_url: "/intro/getting-started/provision.html"
     5  next_url: "/intro/getting-started/vagrant.html"
     6  next_title: "Vagrant Boxes"
     7  description: |-
     8    So far we've shown how Packer can automatically build an image and provision it. This on its own is already quite powerful. But Packer can do better than that. Packer can create multiple images for multiple platforms in parallel, all configured from a single template.
     9  ---
    10  
    11  # Parallel Builds
    12  
    13  So far we've shown how Packer can automatically build an image and provision it.
    14  This on its own is already quite powerful. But Packer can do better than that.
    15  Packer can create multiple images for multiple platforms _in parallel_, all
    16  configured from a single template.
    17  
    18  This is a very useful and important feature of Packer. As an example,
    19  Packer is able to make an AMI and a VMware virtual machine
    20  in parallel provisioned with the _same scripts_, resulting in near-identical
    21  images. The AMI can be used for production, the VMware machine can be used
    22  for development. Or, another example, if you're using Packer to build
    23  [software appliances](http://en.wikipedia.org/wiki/Software_appliance),
    24  then you can build the appliance for every supported platform all in
    25  parallel, all configured from a single template.
    26  
    27  Once you start taking advantage of this feature, the possibilities begin
    28  to unfold in front of you.
    29  
    30  Continuing on the example in this getting started guide, we'll build
    31  a [DigitalOcean](http://www.digitalocean.com) image as well as an AMI. Both
    32  will be near-identical: bare bones Ubuntu OS with Redis pre-installed.
    33  However, since we're building for both platforms, you have the option of
    34  whether you want to use the AMI, or the DigitalOcean snapshot. Or use both.
    35  
    36  ## Setting Up DigitalOcean
    37  
    38  [DigitalOcean](https://www.digitalocean.com/) is a relatively new, but
    39  very popular VPS provider that has popped up. They have a quality offering
    40  of high performance, low cost VPS servers. We'll be building a DigitalOcean
    41  snapshot for this example.
    42  
    43  In order to do this, you'll need an account with DigitalOcean.
    44  [Sign up for an account now](https://www.digitalocean.com/). It is free
    45  to sign up. Because the "droplets" (servers) are charged hourly, you
    46  _will_ be charged $0.01 for every image you create with Packer. If
    47  you're not okay with this, just follow along.
    48  
    49  !> **Warning!** You _will_ be charged $0.01 by DigitalOcean per image
    50  created with Packer because of the time the "droplet" is running.
    51  
    52  Once you sign up for an account, grab your API token from
    53  the [DigitalOcean API access page](https://cloud.digitalocean.com/settings/applications).
    54  Save these values somewhere; you'll need them in a second.
    55  
    56  ## Modifying the Template
    57  
    58  We now have to modify the template to add DigitalOcean to it. Modify the
    59  template we've been using and add the following JSON object to the `builders`
    60  array.
    61  
    62  ```javascript
    63  {
    64    "type": "digitalocean",
    65    "api_token": "{{user `do_api_token`}}",
    66  	"image": "ubuntu-14-04-x64",
    67  	"region": "nyc3",
    68  	"size": "512mb",
    69  }
    70  ```
    71  
    72  You'll also need to modify the `variables` section of the template
    73  to include the access keys for DigitalOcean.
    74  
    75  ```javascript
    76  "variables": {
    77    "do_api_token": "",
    78    // ...
    79  }
    80  ```
    81  
    82  The entire template should now look like this:
    83  
    84  ```javascript
    85  {
    86  	"variables": {
    87  		"aws_access_key": "",
    88  		"aws_secret_key": "",
    89  		"do_api_token": ""
    90  	},
    91  	"builders": [{
    92  		"type": "amazon-ebs",
    93  		"access_key": "{{user `aws_access_key`}}",
    94  		"secret_key": "{{user `aws_secret_key`}}",
    95  		"region": "us-east-1",
    96  		"source_ami": "ami-c65be9ae",
    97  		"instance_type": "t1.micro",
    98  		"ssh_username": "ubuntu",
    99  		"ami_name": "packer-example {{timestamp}}"
   100  	},{
   101  		"type": "digitalocean",
   102  		"api_token": "{{user `do_api_token`}}",
   103  		"image": "ubuntu-14-04-x64",
   104  		"region": "nyc3",
   105  		"size": "512mb"
   106  	}],
   107  	"provisioners": [{
   108  		"type": "shell",
   109  		"inline": [
   110  			"sleep 30",
   111  			"sudo apt-get update",
   112  			"sudo apt-get install -y redis-server"
   113  		]
   114  	}]
   115  }
   116  ```
   117  
   118  Additional builders are simply added to the `builders` array in the template.
   119  This tells Packer to build multiple images. The builder `type` values don't
   120  even need to be different! In fact, if you wanted to build multiple AMIs,
   121  you can do that as long as you specify a unique `name` for each build.
   122  
   123  Validate the template with `packer validate`. This is always a good practice.
   124  
   125  -> **Note:** If you're looking for more **DigitalOcean configuration options**,
   126  you can find them on the
   127  [DigitalOcean Builder page](/docs/builders/digitalocean.html) in the
   128  documentation. The documentation is more of a reference manual that contains a
   129  listing of all the available configuration options.
   130  
   131  ## Build
   132  
   133  Now run `packer build` with your user variables. The output is too verbose to include
   134  all of it, but a portion of it is reproduced below. Note that the ordering
   135  and wording of the lines may be slightly different, but the effect is the
   136  same.
   137  
   138  ```text
   139  $ packer build \
   140      -var 'aws_access_key=YOUR ACCESS KEY' \
   141      -var 'aws_secret_key=YOUR SECRET KEY' \
   142      -var 'do_api_token=YOUR API TOKEN' \
   143      example.json
   144  ==> amazon-ebs: amazon-ebs output will be in this color.
   145  ==> digitalocean: digitalocean output will be in this color.
   146  
   147  ==> digitalocean: Creating temporary ssh key for droplet...
   148  ==> amazon-ebs: Creating temporary keypair for this instance...
   149  ==> amazon-ebs: Creating temporary security group for this instance...
   150  ==> digitalocean: Creating droplet...
   151  ==> amazon-ebs: Authorizing SSH access on the temporary security group...
   152  ==> amazon-ebs: Launching a source AWS instance...
   153  ==> digitalocean: Waiting for droplet to become active...
   154  ==> amazon-ebs: Waiting for instance to become ready...
   155  ==> digitalocean: Connecting to the droplet via SSH...
   156  ==> amazon-ebs: Connecting to the instance via SSH...
   157  ...
   158  ==> Builds finished. The artifacts of successful builds are:
   159  --> amazon-ebs: AMIs were created:
   160  
   161  us-east-1: ami-376d1d5e
   162  --> digitalocean: A snapshot was created: packer-1371870364
   163  ```
   164  
   165  As you can see, Packer builds both the Amazon and DigitalOcean images
   166  in parallel. It outputs information about each in different colors
   167  (although you can't see that in the block above) so that it is easy to identify.
   168  
   169  At the end of the build, Packer outputs both of the artifacts created
   170  (an AMI and a DigitalOcean snapshot). Both images created are bare bones
   171  Ubuntu installations with Redis pre-installed.