github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/website/source/intro/getting-started/build-image.html.md (about)

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