2013-06-21 19:48:01 -04:00
|
|
|
---
|
2015-07-22 22:31:00 -04:00
|
|
|
layout: intro
|
2017-03-25 18:13:52 -04:00
|
|
|
sidebar_current: intro-getting-started-build-image
|
|
|
|
page_title: Build an Image - Getting Started
|
|
|
|
description: |-
|
|
|
|
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.
|
|
|
|
---
|
2013-06-21 19:48:01 -04:00
|
|
|
|
|
|
|
# Build an Image
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
With Packer installed, let's just dive right into it and build our first image.
|
2016-01-14 15:31:19 -05:00
|
|
|
Our first image will be an [Amazon EC2 AMI](https://aws.amazon.com/ec2/) with
|
2015-07-22 22:31:00 -04:00
|
|
|
Redis pre-installed. This is just an example. Packer can create images for [many
|
2017-03-28 18:28:34 -04:00
|
|
|
platforms][platforms] with anything pre-installed.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2016-01-14 15:31:19 -05:00
|
|
|
If you don't have an AWS account, [create one now](https://aws.amazon.com/free/).
|
2014-12-30 14:55:16 -05:00
|
|
|
For the example, we'll use a "t2.micro" instance to build our image, which
|
2016-01-14 15:31:19 -05:00
|
|
|
qualifies under the AWS [free-tier](https://aws.amazon.com/free/), meaning it
|
2015-07-22 22:31:00 -04:00
|
|
|
will be free. If you already have an AWS account, you may be charged some amount
|
|
|
|
of money, but it shouldn't be more than a few cents.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
-> **Note:** If you're not using an account that qualifies under the AWS
|
2015-07-22 22:31:00 -04:00
|
|
|
free-tier, you may be charged to run these examples. The charge should only be a
|
|
|
|
few cents, but we're not responsible if it ends up being more.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2017-03-28 18:28:34 -04:00
|
|
|
Packer can build images for [many platforms][platforms] other than
|
2013-06-21 19:48:01 -04:00
|
|
|
AWS, but AWS requires no additional software installed on your computer and
|
2016-01-14 15:31:19 -05:00
|
|
|
their [free-tier](https://aws.amazon.com/free/) makes it free to use for most
|
2013-06-21 19:48:01 -04:00
|
|
|
people. This is why we chose to use AWS for the example. If you're uncomfortable
|
|
|
|
setting up an AWS account, feel free to follow along as the basic principles
|
|
|
|
apply to the other platforms as well.
|
|
|
|
|
|
|
|
## The Template
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
The configuration file used to define what image we want built and how is called
|
|
|
|
a *template* in Packer terminology. The format of a template is simple
|
|
|
|
[JSON](http://www.json.org/). JSON struck the best balance between
|
2013-06-21 19:48:01 -04:00
|
|
|
human-editable and machine-editable, allowing both hand-made templates as well
|
|
|
|
as machine generated templates to easily be made.
|
|
|
|
|
|
|
|
We'll start by creating the entire template, then we'll go over each section
|
|
|
|
briefly. Create a file `example.json` and fill it with the following contents:
|
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
```json
|
2013-06-21 19:48:01 -04:00
|
|
|
{
|
2013-11-25 07:36:32 -05:00
|
|
|
"variables": {
|
|
|
|
"aws_access_key": "",
|
|
|
|
"aws_secret_key": ""
|
|
|
|
},
|
2013-06-21 19:48:01 -04:00
|
|
|
"builders": [{
|
|
|
|
"type": "amazon-ebs",
|
2013-11-25 07:36:32 -05:00
|
|
|
"access_key": "{{user `aws_access_key`}}",
|
|
|
|
"secret_key": "{{user `aws_secret_key`}}",
|
2013-06-21 19:48:01 -04:00
|
|
|
"region": "us-east-1",
|
2017-06-20 14:21:49 -04:00
|
|
|
"source_ami_filter": {
|
|
|
|
"filters": {
|
|
|
|
"virtualization-type": "hvm",
|
2017-09-05 20:20:48 -04:00
|
|
|
"name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
|
2017-06-20 14:21:49 -04:00
|
|
|
"root-device-type": "ebs"
|
|
|
|
},
|
|
|
|
"owners": ["099720109477"],
|
|
|
|
"most_recent": true
|
|
|
|
},
|
2016-01-13 16:52:17 -05:00
|
|
|
"instance_type": "t2.micro",
|
2013-06-21 19:48:01 -04:00
|
|
|
"ssh_username": "ubuntu",
|
2013-08-08 20:01:33 -04:00
|
|
|
"ami_name": "packer-example {{timestamp}}"
|
2013-06-21 19:48:01 -04:00
|
|
|
}]
|
|
|
|
}
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2016-02-20 10:55:22 -05:00
|
|
|
When building, you'll pass in `aws_access_key` and `aws_secret_key` as
|
|
|
|
[user variables](/docs/templates/user-variables.html), keeping your secret keys
|
2015-07-22 22:31:00 -04:00
|
|
|
out of the template. You can create security credentials on [this
|
|
|
|
page](https://console.aws.amazon.com/iam/home?#security_credential). An example
|
|
|
|
IAM policy document can be found in the [Amazon EC2 builder
|
|
|
|
docs](/docs/builders/amazon.html).
|
|
|
|
|
|
|
|
This is a basic template that is ready-to-go. It should be immediately
|
|
|
|
recognizable as a normal, basic JSON object. Within the object, the `builders`
|
|
|
|
section contains an array of JSON objects configuring a specific *builder*. A
|
|
|
|
builder is a component of Packer that is responsible for creating a machine and
|
|
|
|
turning that machine into an image.
|
|
|
|
|
|
|
|
In this case, we're only configuring a single builder of type `amazon-ebs`. This
|
|
|
|
is the Amazon EC2 AMI builder that ships with Packer. This builder builds an
|
|
|
|
EBS-backed AMI by launching a source AMI, provisioning on top of that, and
|
|
|
|
re-packaging it into a new AMI.
|
|
|
|
|
|
|
|
The additional keys within the object are configuration for this builder,
|
2016-02-20 10:55:22 -05:00
|
|
|
specifying things such as access keys, the source AMI to build from and more.
|
2015-07-22 22:31:00 -04:00
|
|
|
The exact set of configuration variables available for a builder are specific to
|
2017-03-28 18:28:34 -04:00
|
|
|
each builder and can be found within the [documentation](/docs/index.html).
|
2015-07-22 22:31:00 -04:00
|
|
|
|
|
|
|
Before we take this template and build an image from it, let's validate the
|
|
|
|
template by running `packer validate example.json`. This command checks the
|
|
|
|
syntax as well as the configuration values to verify they look valid. The output
|
|
|
|
should look similar to below, because the template should be valid. If there are
|
2013-06-21 19:48:01 -04:00
|
|
|
any errors, this command will tell you.
|
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
```text
|
2013-06-21 19:48:01 -04:00
|
|
|
$ packer validate example.json
|
|
|
|
Template validated successfully.
|
|
|
|
```
|
|
|
|
|
|
|
|
Next, let's build the image from this template.
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
An astute reader may notice that we said earlier we'd be building an image with
|
|
|
|
Redis pre-installed, and yet the template we made doesn't reference Redis
|
|
|
|
anywhere. In fact, this part of the documentation will only cover making a first
|
|
|
|
basic, non-provisioned image. The next section on provisioning will cover
|
|
|
|
installing Redis.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
|
|
|
## Your First Image
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
With a properly validated template. It is time to build your first image. This
|
|
|
|
is done by calling `packer build` with the template file. The output should look
|
|
|
|
similar to below. Note that this process typically takes a few minutes.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
-> **Note:** For the tutorial it is convenient to use the credentials in the
|
2016-11-26 12:58:56 -05:00
|
|
|
command line. However, it is potentially insecure. See our documentation for
|
|
|
|
other ways to [specify Amazon credentials](/docs/builders/amazon.html#specifying-amazon-credentials).
|
2016-11-26 07:46:12 -05:00
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
-> **Note:** When using packer on Windows, replace the single-quotes in the
|
2016-08-21 15:04:05 -04:00
|
|
|
command below with double-quotes.
|
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
```text
|
2013-11-25 07:36:32 -05:00
|
|
|
$ packer build \
|
|
|
|
-var 'aws_access_key=YOUR ACCESS KEY' \
|
|
|
|
-var 'aws_secret_key=YOUR SECRET KEY' \
|
|
|
|
example.json
|
2013-06-21 19:48:01 -04:00
|
|
|
==> amazon-ebs: amazon-ebs output will be in this color.
|
|
|
|
|
|
|
|
==> amazon-ebs: Creating temporary keypair for this instance...
|
|
|
|
==> amazon-ebs: Creating temporary security group for this instance...
|
|
|
|
==> amazon-ebs: Authorizing SSH access on the temporary security group...
|
|
|
|
==> amazon-ebs: Launching a source AWS instance...
|
|
|
|
==> amazon-ebs: Waiting for instance to become ready...
|
|
|
|
==> amazon-ebs: Connecting to the instance via SSH...
|
|
|
|
==> amazon-ebs: Stopping the source instance...
|
|
|
|
==> amazon-ebs: Waiting for the instance to stop...
|
|
|
|
==> amazon-ebs: Creating the AMI: packer-example 1371856345
|
|
|
|
==> amazon-ebs: AMI: ami-19601070
|
|
|
|
==> amazon-ebs: Waiting for AMI to become ready...
|
|
|
|
==> amazon-ebs: Terminating the source AWS instance...
|
|
|
|
==> amazon-ebs: Deleting temporary security group...
|
|
|
|
==> amazon-ebs: Deleting temporary keypair...
|
|
|
|
==> amazon-ebs: Build finished.
|
|
|
|
|
|
|
|
==> Builds finished. The artifacts of successful builds are:
|
|
|
|
--> amazon-ebs: AMIs were created:
|
|
|
|
|
|
|
|
us-east-1: ami-19601070
|
|
|
|
```
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
At the end of running `packer build`, Packer outputs the *artifacts* that were
|
|
|
|
created as part of the build. Artifacts are the results of a build, and
|
|
|
|
typically represent an ID (such as in the case of an AMI) or a set of files
|
|
|
|
(such as for a VMware virtual machine). In this example, we only have a single
|
|
|
|
artifact: the AMI in us-east-1 that was created.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2016-02-20 10:55:22 -05:00
|
|
|
This AMI is ready to use. If you wanted you could go and launch this AMI right now
|
2015-07-22 22:31:00 -04:00
|
|
|
and it would work great.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2017-03-25 18:13:52 -04:00
|
|
|
-> **Note:** Your AMI ID will surely be different than the one above. If you
|
2015-07-22 22:31:00 -04:00
|
|
|
try to launch the one in the example output above, you will get an error. If you
|
|
|
|
want to try to launch your AMI, get the ID from the Packer output.
|
2013-06-21 19:48:01 -04:00
|
|
|
|
2017-01-05 14:29:15 -05:00
|
|
|
-> **Note:** If you see a `VPCResourceNotSpecified` error, Packer might not be
|
2017-01-05 15:58:22 -05:00
|
|
|
able to determine the default VPC, which the `t2` instance types require. This
|
2017-01-26 19:03:57 -05:00
|
|
|
can happen if you created your AWS account before `2013-12-04`. You can either
|
2017-01-05 15:58:22 -05:00
|
|
|
change the `instance_type` to `m3.medium`, or specify a VPC. Please see
|
|
|
|
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/default-vpc.html for more
|
2017-03-25 18:13:52 -04:00
|
|
|
information. If you specify a `vpc_id`, you will also need to set `subnet_id`.
|
2017-01-27 18:03:02 -05:00
|
|
|
Unless you modify your subnet's [IPv4 public addressing attribute](
|
|
|
|
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-ip-addressing.html#subnet-public-ip),
|
2017-03-25 18:13:52 -04:00
|
|
|
you will also need to set `associate_public_ip_address` to `true`, or set up a
|
2017-01-27 18:03:02 -05:00
|
|
|
[VPN](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_VPN.html).
|
2017-01-05 14:29:15 -05:00
|
|
|
|
2013-06-21 19:48:01 -04:00
|
|
|
## Managing the Image
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
Packer only builds images. It does not attempt to manage them in any way. After
|
|
|
|
they're built, it is up to you to launch or destroy them as you see fit. If you
|
2017-05-09 14:37:49 -04:00
|
|
|
want to store and namespace images for quick reference, you can use [Atlas by
|
2015-07-22 22:31:00 -04:00
|
|
|
HashiCorp](https://atlas.hashicorp.com). We'll cover remotely building and
|
|
|
|
storing images at the end of this getting started guide.
|
|
|
|
|
|
|
|
After running the above example, your AWS account now has an AMI associated with
|
|
|
|
it. AMIs are stored in S3 by Amazon, so unless you want to be charged about
|
2015-09-21 09:32:18 -04:00
|
|
|
$0.01 per month, you'll probably want to remove it. Remove the AMI by first
|
2015-07-22 22:31:00 -04:00
|
|
|
deregistering it on the [AWS AMI management
|
|
|
|
page](https://console.aws.amazon.com/ec2/home?region=us-east-1#s=Images). Next,
|
|
|
|
delete the associated snapshot on the [AWS snapshot management
|
|
|
|
page](https://console.aws.amazon.com/ec2/home?region=us-east-1#s=Snapshots).
|
|
|
|
|
|
|
|
Congratulations! You've just built your first image with Packer. Although the
|
|
|
|
image was pretty useless in this case (nothing was changed about it), this page
|
2016-02-20 10:55:22 -05:00
|
|
|
should've given you a general idea of how Packer works, what templates are and
|
2015-07-22 22:31:00 -04:00
|
|
|
how to validate and build templates into machine images.
|
2017-03-28 18:28:34 -04:00
|
|
|
|
|
|
|
[platforms]: /docs/builders/index.html
|