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

     1  ---
     2  layout: "intro"
     3  page_title: "Build an Image"
     4  prev_url: "/intro/getting-started/setup.html"
     5  next_url: "/intro/getting-started/provision.html"
     6  next_title: "Provision"
     7  description: |-
     8    With Packer installed, let's just dive right into it and build our first image. Our first image will be an Amazon EC2 AMI with Redis pre-installed. This is just an example. Packer can create images for many platforms with anything pre-installed.
     9  ---
    10  
    11  # Build an Image
    12  
    13  With Packer installed, let's just dive right into it and build our first
    14  image. Our first image will be an [Amazon EC2 AMI](http://aws.amazon.com/ec2/)
    15  with Redis pre-installed. This is just an example. Packer can create images
    16  for [many platforms](/intro/platforms.html) with anything pre-installed.
    17  
    18  If you don't have an AWS account, [create one now](http://aws.amazon.com/free/).
    19  For the example, we'll use a "t2.micro" instance to build our image, which
    20  qualifies under the AWS [free-tier](http://aws.amazon.com/free/), meaning
    21  it will be free. If you already have an AWS account, you may be charged some
    22  amount of money, but it shouldn't be more than a few cents.
    23  
    24  -> **Note:** If you're not using an account that qualifies under the AWS
    25  free-tier, you may be charged to run these examples. The charge should only be
    26  a few cents, but we're not responsible if it ends up being more.
    27  
    28  Packer can build images for [many platforms](/intro/platforms.html) other than
    29  AWS, but AWS requires no additional software installed on your computer and
    30  their [free-tier](http://aws.amazon.com/free/) makes it free to use for most
    31  people. This is why we chose to use AWS for the example. If you're uncomfortable
    32  setting up an AWS account, feel free to follow along as the basic principles
    33  apply to the other platforms as well.
    34  
    35  ## The Template
    36  
    37  The configuration file used to define what image we want built and how
    38  is called a _template_ in Packer terminology. The format of a template
    39  is simple [JSON](http://www.json.org/). JSON struck the best balance between
    40  human-editable and machine-editable, allowing both hand-made templates as well
    41  as machine generated templates to easily be made.
    42  
    43  We'll start by creating the entire template, then we'll go over each section
    44  briefly. Create a file `example.json` and fill it with the following contents:
    45  
    46  ```javascript
    47  {
    48    "variables": {
    49      "aws_access_key": "",
    50      "aws_secret_key": ""
    51    },
    52    "builders": [{
    53      "type": "amazon-ebs",
    54      "access_key": "{{user `aws_access_key`}}",
    55      "secret_key": "{{user `aws_secret_key`}}",
    56      "region": "us-east-1",
    57      "source_ami": "ami-c65be9ae",
    58      "instance_type": "t1.micro",
    59      "ssh_username": "ubuntu",
    60      "ami_name": "packer-example {{timestamp}}"
    61    }]
    62  }
    63  ```
    64  
    65  When building, you'll pass in the `aws_access_key` and `aws_secret_key` as
    66  a [user variable](/docs/templates/user-variables.html), keeping your secret
    67  keys out of the template. You can create security credentials
    68  on [this page](https://console.aws.amazon.com/iam/home?#security_credential).
    69  An example IAM policy document can be found in the [Amazon EC2 builder docs](/docs/builders/amazon.html).
    70  
    71  This is a basic template that is ready-to-go. It should be immediately recognizable
    72  as a normal, basic JSON object. Within the object, the `builders` section
    73  contains an array of JSON objects configuring a specific _builder_. A
    74  builder is a component of Packer that is responsible for creating a machine
    75  and turning that machine into an image.
    76  
    77  In this case, we're only configuring a single builder of type `amazon-ebs`.
    78  This is the Amazon EC2 AMI builder that ships with Packer. This builder
    79  builds an EBS-backed AMI by launching a source AMI, provisioning on top of
    80  that, and re-packaging it into a new AMI.
    81  
    82  The additional keys within the object are configuration for this builder, specifying things
    83  such as access keys, the source AMI to build from, and more.
    84  The exact set of configuration variables available for a builder are
    85  specific to each builder and can be found within the [documentation](/docs).
    86  
    87  Before we take this template and build an image from it, let's validate the template
    88  by running `packer validate example.json`. This command checks the syntax
    89  as well as the configuration values to verify they look valid. The output should
    90  look similar to below, because the template should be valid. If there are
    91  any errors, this command will tell you.
    92  
    93  ```text
    94  $ packer validate example.json
    95  Template validated successfully.
    96  ```
    97  
    98  Next, let's build the image from this template.
    99  
   100  An astute reader may notice that we said earlier we'd be building an
   101  image with Redis pre-installed, and yet the template we made doesn't reference
   102  Redis anywhere. In fact, this part of the documentation will only cover making
   103  a first basic, non-provisioned image. The next section on provisioning will
   104  cover installing Redis.
   105  
   106  ## Your First Image
   107  
   108  With a properly validated template. It is time to build your first image.
   109  This is done by calling `packer build` with the template file. The output
   110  should look similar to below. Note that this process typically takes a
   111  few minutes.
   112  
   113  ```text
   114  $ packer build \
   115      -var 'aws_access_key=YOUR ACCESS KEY' \
   116      -var 'aws_secret_key=YOUR SECRET KEY' \
   117      example.json
   118  ==> amazon-ebs: amazon-ebs output will be in this color.
   119  
   120  ==> amazon-ebs: Creating temporary keypair for this instance...
   121  ==> amazon-ebs: Creating temporary security group for this instance...
   122  ==> amazon-ebs: Authorizing SSH access on the temporary security group...
   123  ==> amazon-ebs: Launching a source AWS instance...
   124  ==> amazon-ebs: Waiting for instance to become ready...
   125  ==> amazon-ebs: Connecting to the instance via SSH...
   126  ==> amazon-ebs: Stopping the source instance...
   127  ==> amazon-ebs: Waiting for the instance to stop...
   128  ==> amazon-ebs: Creating the AMI: packer-example 1371856345
   129  ==> amazon-ebs: AMI: ami-19601070
   130  ==> amazon-ebs: Waiting for AMI to become ready...
   131  ==> amazon-ebs: Terminating the source AWS instance...
   132  ==> amazon-ebs: Deleting temporary security group...
   133  ==> amazon-ebs: Deleting temporary keypair...
   134  ==> amazon-ebs: Build finished.
   135  
   136  ==> Builds finished. The artifacts of successful builds are:
   137  --> amazon-ebs: AMIs were created:
   138  
   139  us-east-1: ami-19601070
   140  ```
   141  
   142  At the end of running `packer build`, Packer outputs the _artifacts_
   143  that were created as part of the build. Artifacts are the results of a
   144  build, and typically represent an ID (such as in the case of an AMI) or
   145  a set of files (such as for a VMware virtual machine). In this example,
   146  we only have a single artifact: the AMI in us-east-1 that was created.
   147  
   148  This AMI is ready to use. If you wanted you can go and launch this AMI
   149  right now and it would work great.
   150  
   151  -> **Note:** Your AMI ID will surely be different than the
   152  one above. If you try to launch the one in the example output above, you
   153  will get an error. If you want to try to launch your AMI, get the ID from
   154  the Packer output.
   155  
   156  ## Managing the Image
   157  
   158  Packer only builds images. It does not attempt to manage them in any way.
   159  After they're built, it is up to you to launch or destroy them as you see
   160  fit. If you want to store and namespace images for easy reference, you
   161  can use [Atlas by HashiCorp](https://atlas.hashicorp.com). We'll cover
   162  remotely building and storing images at the end of this getting started guide.
   163  
   164  After running the above example, your AWS account
   165  now has an AMI associated with it. AMIs are stored in S3 by Amazon,
   166  so unless you want to be charged about $0.01
   167  per month, you'll probably want to remove it. Remove the AMI by
   168  first deregistering it on the [AWS AMI management page](https://console.aws.amazon.com/ec2/home?region=us-east-1#s=Images).
   169  Next, delete the associated snapshot on the
   170  [AWS snapshot management page](https://console.aws.amazon.com/ec2/home?region=us-east-1#s=Snapshots).
   171  
   172  Congratulations! You've just built your first image with Packer. Although
   173  the image was pretty useless in this case (nothing was changed about it),
   174  this page should've given you a general idea of how Packer works, what
   175  templates are, and how to validate and build templates into machine
   176  images.