github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/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 --- 8 9 # Build an Image 10 11 With Packer installed, let's just dive right into it and build our first 12 image. Our first image will be an [Amazon EC2 AMI](http://aws.amazon.com/ec2/) 13 with Redis pre-installed. This is just an example. Packer can create images 14 for [many platforms](/intro/platforms.html) with anything pre-installed. 15 16 If you don't have an AWS account, [create one now](http://aws.amazon.com/free/). 17 For the example, we'll use a "t1.micro" instance to build our image, which 18 qualifies under the AWS [free-tier](http://aws.amazon.com/free/), meaning 19 it will be free. If you already have an AWS account, you may be charged some 20 amount of money, but it shouldn't be more than a few cents. 21 22 <div class="alert alert-block alert-warn"> 23 <strong>Note</strong> that if you're not using an account that qualifies under 24 the AWS <a href="http://aws.amazon.com/free/">free-tier</a>, you may be 25 charged to run these examples. The charge should only be a few cents, but 26 we're not responsible if it ends up being more. 27 </div> 28 29 Packer can build images for [many platforms](/intro/platforms.html) other than 30 AWS, but AWS requires no additional software installed on your computer and 31 their [free-tier](http://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 39 is called a _template_ in Packer terminology. The format of a template 40 is simple [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 <pre class="prettyprint"> 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": "ami-de0d9eb7", 59 "instance_type": "t1.micro", 60 "ssh_username": "ubuntu", 61 "ami_name": "packer-example {{timestamp}}" 62 }] 63 } 64 </pre> 65 66 When building, you'll pass in the `aws_access_key` and `aws_access_key` as 67 a [user variable](/docs/templates/user-variables.html), keeping your secret 68 keys out of the template. You can create security credentials 69 on [this page](https://console.aws.amazon.com/iam/home?#security_credential). 70 An example IAM policy document can be found in the [Amazon EC2 builder docs](/docs/builders/amazon.html). 71 72 This is a basic template that is ready-to-go. It should be immediately recognizable 73 as a normal, basic JSON object. Within the object, the `builders` section 74 contains an array of JSON objects configuring a specific _builder_. A 75 builder is a component of Packer that is responsible for creating a machine 76 and turning that machine into an image. 77 78 In this case, we're only configuring a single builder of type `amazon-ebs`. 79 This is the Amazon EC2 AMI builder that ships with Packer. This builder 80 builds an EBS-backed AMI by launching a source AMI, provisioning on top of 81 that, and re-packaging it into a new AMI. 82 83 The additional keys within the object are configuration for this builder, specifying things 84 such as access keys, the source AMI to build from, and more. 85 The exact set of configuration variables available for a builder are 86 specific to each builder and can be found within the [documentation](/docs). 87 88 Before we take this template and build an image from it, let's validate the template 89 by running `packer validate example.json`. This command checks the syntax 90 as well as the configuration values to verify they look valid. The output should 91 look similar to below, because the template should be valid. If there are 92 any errors, this command will tell you. 93 94 ``` 95 $ packer validate example.json 96 Template validated successfully. 97 ``` 98 99 Next, let's build the image from this template. 100 101 An astute reader may notice that we said earlier we'd be building an 102 image with Redis pre-installed, and yet the template we made doesn't reference 103 Redis anywhere. In fact, this part of the documentation will only cover making 104 a first basic, non-provisioned image. The next section on provisioning will 105 cover installing Redis. 106 107 ## Your First Image 108 109 With a properly validated template. It is time to build your first image. 110 This is done by calling `packer build` with the template file. The output 111 should look similar to below. Note that this process typically takes a 112 few minutes. 113 114 ``` 115 $ packer build \ 116 -var 'aws_access_key=YOUR ACCESS KEY' \ 117 -var 'aws_secret_key=YOUR SECRET KEY' \ 118 example.json 119 ==> amazon-ebs: amazon-ebs output will be in this color. 120 121 ==> amazon-ebs: Creating temporary keypair for this instance... 122 ==> amazon-ebs: Creating temporary security group for this instance... 123 ==> amazon-ebs: Authorizing SSH access on the temporary security group... 124 ==> amazon-ebs: Launching a source AWS instance... 125 ==> amazon-ebs: Waiting for instance to become ready... 126 ==> amazon-ebs: Connecting to the instance via SSH... 127 ==> amazon-ebs: Stopping the source instance... 128 ==> amazon-ebs: Waiting for the instance to stop... 129 ==> amazon-ebs: Creating the AMI: packer-example 1371856345 130 ==> amazon-ebs: AMI: ami-19601070 131 ==> amazon-ebs: Waiting for AMI to become ready... 132 ==> amazon-ebs: Terminating the source AWS instance... 133 ==> amazon-ebs: Deleting temporary security group... 134 ==> amazon-ebs: Deleting temporary keypair... 135 ==> amazon-ebs: Build finished. 136 137 ==> Builds finished. The artifacts of successful builds are: 138 --> amazon-ebs: AMIs were created: 139 140 us-east-1: ami-19601070 141 ``` 142 143 At the end of running `packer build`, Packer outputs the _artifacts_ 144 that were created as part of the build. Artifacts are the results of a 145 build, and typically represent an ID (such as in the case of an AMI) or 146 a set of files (such as for a VMware virtual machine). In this example, 147 we only have a single artifact: the AMI in us-east-1 that was created. 148 149 This AMI is ready to use. If you wanted you can go and launch this AMI 150 right now and it would work great. 151 152 <div class="alert alert-block alert-info"> 153 <strong>Note:</strong> Your AMI ID will surely be different than the 154 one above. If you try to launch the one in the example output above, you 155 will get an error. If you want to try to launch your AMI, get the ID from 156 the Packer output. 157 </div> 158 159 ## Managing the Image 160 161 Packer only builds images. It does not attempt to manage them in any way. 162 After they're built, it is up to you to launch or destroy them as you see 163 fit. As a result of this, after running the above example, your AWS account 164 now has an AMI associated with it. 165 166 AMIs are stored in S3 by Amazon, 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.