github.com/ratanraj/packer@v1.3.2/website/source/intro/getting-started/provision.html.md (about)

     1  ---
     2  layout: intro
     3  sidebar_current: intro-getting-started-provision
     4  page_title: Provision - Getting Started
     5  description: |-
     6    In the previous page of this guide, you created your first image with Packer.
     7    The image you just built, however, was basically just a repackaging of a
     8    previously existing base AMI. The real utility of Packer comes from being able
     9    to install and configure software into the images as well. This stage is also
    10    known as the *provision* step. Packer fully supports automated provisioning in
    11    order to install software onto the machines prior to turning them into images.
    12  ---
    13  
    14  # Provision
    15  
    16  In the previous page of this guide, you created your first image with Packer.
    17  The image you just built, however, was basically just a repackaging of a
    18  previously existing base AMI. The real utility of Packer comes from being able
    19  to install and configure software into the images as well. This stage is also
    20  known as the *provision* step. Packer fully supports automated provisioning in
    21  order to install software onto the machines prior to turning them into images.
    22  
    23  In this section, we're going to complete our image by installing Redis on it.
    24  This way, the image we end up building actually contains Redis pre-installed.
    25  Although Redis is a small, simple example, this should give you an idea of what
    26  it may be like to install many more packages into the image.
    27  
    28  Historically, pre-baked images have been frowned upon because changing them has
    29  been so tedious and slow. Because Packer is completely automated, including
    30  provisioning, images can be changed quickly and integrated with modern
    31  configuration management tools such as Chef or Puppet.
    32  
    33  ## Configuring Provisioners
    34  
    35  Provisioners are configured as part of the template. We'll use the built-in
    36  shell provisioner that comes with Packer to install Redis. Modify the
    37  `example.json` template we made previously and add the following. We'll explain
    38  the various parts of the new configuration following the code block below.
    39  
    40  ```json
    41  {
    42    "variables": ["..."],
    43    "builders": ["..."],
    44  
    45    "provisioners": [{
    46      "type": "shell",
    47      "inline": [
    48        "sleep 30",
    49        "sudo apt-get update",
    50        "sudo apt-get install -y redis-server"
    51      ]
    52    }]
    53  }
    54  ```
    55  
    56  -> **Note:** The `sleep 30` in the example above is very important. Because
    57  Packer is able to detect and SSH into the instance as soon as SSH is available,
    58  Ubuntu actually doesn't get proper amounts of time to initialize. The sleep
    59  makes sure that the OS properly initializes.
    60  
    61  Hopefully it is obvious, but the `builders` section shouldn't actually contain
    62  "...", it should be the contents setup in the previous page of the getting
    63  started guide. Also note the comma after the `"builders": [...]` section, which
    64  was not present in the previous lesson.
    65  
    66  To configure the provisioners, we add a new section `provisioners` to the
    67  template, alongside the `builders` configuration. The provisioners section is an
    68  array of provisioners to run. If multiple provisioners are specified, they are
    69  run in the order given.
    70  
    71  By default, each provisioner is run for every builder defined. So if we had two
    72  builders defined in our template, such as both Amazon and DigitalOcean, then the
    73  shell script would run as part of both builds. There are ways to restrict
    74  provisioners to certain builds, but it is outside the scope of this getting
    75  started guide. It is covered in more detail in the complete
    76  [documentation](/docs/index.html).
    77  
    78  The one provisioner we defined has a type of `shell`. This provisioner ships
    79  with Packer and runs shell scripts on the running machine. In our case, we
    80  specify two inline commands to run in order to install Redis.
    81  
    82  ## Build
    83  
    84  With the provisioner configured, give it a pass once again through
    85  `packer validate` to verify everything is okay, then build it using
    86  `packer build example.json`. The output should look similar to when you built
    87  your first image, except this time there will be a new step where the
    88  provisioning is run.
    89  
    90  The output from the provisioner is too verbose to include in this guide, since
    91  it contains all the output from the shell scripts. But you should see Redis
    92  successfully install. After that, Packer once again turns the machine into an
    93  AMI.
    94  
    95  If you were to launch this AMI, Redis would be pre-installed. Cool!
    96  
    97  This is just a basic example. In a real world use case, you may be provisioning
    98  an image with the entire stack necessary to run your application. Or maybe just
    99  the web stack so that you can have an image for web servers pre-built. This
   100  saves tons of time later as you launch these images since everything is
   101  pre-installed. Additionally, since everything is pre-installed, you can test the
   102  images as they're built and know that when they go into production, they'll be
   103  functional.