github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/website/source/docs/builders/googlecompute.html.md (about) 1 --- 2 description: | 3 The `googlecompute` Packer builder is able to create images for use with Google 4 Compute Engine (GCE) based on existing images. Building GCE images from scratch 5 is not possible from Packer at this time. For building images from scratch, please see 6 [Building GCE Images from Scratch](https://cloud.google.com/compute/docs/tutorials/building-images). 7 layout: docs 8 page_title: Google Compute Builder 9 ... 10 11 # Google Compute Builder 12 13 Type: `googlecompute` 14 15 The `googlecompute` Packer builder is able to create 16 [images](https://developers.google.com/compute/docs/images) for use with [Google 17 Compute Engine](https://cloud.google.com/products/compute-engine)(GCE) based on 18 existing images. Building GCE images from scratch is not possible from Packer at 19 this time. For building images from scratch, please see 20 [Building GCE Images from Scratch](https://cloud.google.com/compute/docs/tutorials/building-images). 21 ## Authentication 22 23 Authenticating with Google Cloud services requires at most one JSON file, called 24 the *account file*. The *account file* is **not** required if you are running 25 the `googlecompute` Packer builder from a GCE instance with a 26 properly-configured [Compute Engine Service 27 Account](https://cloud.google.com/compute/docs/authentication). 28 29 ### Running With a Compute Engine Service Account 30 31 If you run the `googlecompute` Packer builder from a GCE instance, you can 32 configure that instance to use a [Compute Engine Service 33 Account](https://cloud.google.com/compute/docs/authentication). This will allow 34 Packer to authenticate to Google Cloud without having to bake in a separate 35 credential/authentication file. 36 37 To create a GCE instance that uses a service account, provide the required 38 scopes when launching the instance. 39 40 For `gcloud`, do this via the `--scopes` parameter: 41 42 ``` {.sh} 43 gcloud compute --project YOUR_PROJECT instances create "INSTANCE-NAME" ... \ 44 --scopes "https://www.googleapis.com/auth/compute" \ 45 "https://www.googleapis.com/auth/devstorage.full_control" \ 46 ... 47 ``` 48 49 For the [Google Developers Console](https://console.developers.google.com): 50 51 1. Choose "Show advanced options" 52 2. Tick "Enable Compute Engine service account" 53 3. Choose "Read Write" for Compute 54 4. Chose "Full" for "Storage" 55 56 **The service account will be used automatically by Packer as long as there is 57 no *account file* specified in the Packer configuration file.** 58 59 ### Running Without a Compute Engine Service Account 60 61 The [Google Developers Console](https://console.developers.google.com) allows 62 you to create and download a credential file that will let you use the 63 `googlecompute` Packer builder anywhere. To make the process more 64 straightforwarded, it is documented here. 65 66 1. Log into the [Google Developers 67 Console](https://console.developers.google.com) and select a project. 68 69 2. Under the "APIs & Auth" section, click "Credentials." 70 71 3. Click the "Create new Client ID" button, select "Service account", and click 72 "Create Client ID" 73 74 4. Click "Generate new JSON key" for the Service Account you just created. A 75 JSON file will be downloaded automatically. This is your *account file*. 76 77 ### Precedence of Authentication Methods 78 79 Packer looks for credentials in the following places, preferring the first location found: 80 81 1. A `account_file` option in your packer file. 82 83 2. A JSON file (Service Account) whose path is specified by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. 84 85 3. A JSON file in a location known to the `gcloud` command-line tool. (`gcloud` creates it when it's configured) 86 87 On Windows, this is: `%APPDATA%/gcloud/application_default_credentials.json`. 88 89 On other systems: `$HOME/.config/gcloud/application_default_credentials.json`. 90 91 4. On Google Compute Engine and Google App Engine Managed VMs, it fetches credentials from the metadata server. (Needs a correct VM authentication scope configuration, see above) 92 93 ## Basic Example 94 95 Below is a fully functioning example. It doesn't do anything useful, since no 96 provisioners or startup-script metadata are defined, but it will effectively 97 repackage an existing GCE image. The account_file is obtained in the previous 98 section. If it parses as JSON it is assumed to be the file itself, otherwise it 99 is assumed to be the path to the file containing the JSON. 100 101 ``` {.json} 102 { 103 "builders": [{ 104 "type": "googlecompute", 105 "account_file": "account.json", 106 "project_id": "my project", 107 "source_image": "debian-7-wheezy-v20150127", 108 "zone": "us-central1-a" 109 }] 110 } 111 ``` 112 113 ## Configuration Reference 114 115 Configuration options are organized below into two categories: required and 116 optional. Within each category, the available options are alphabetized and 117 described. 118 119 In addition to the options listed here, a 120 [communicator](/docs/templates/communicator.html) can be configured for this 121 builder. 122 123 ### Required: 124 125 - `project_id` (string) - The project ID that will be used to launch instances 126 and store images. 127 128 - `source_image` (string) - The source image to use to create the new image 129 from. You can also specify `source_image_family` instead. If both 130 `source_image` and `source_image_family` are specified, `source_image` 131 takes precedence. Example: `"debian-8-jessie-v20161027"` 132 133 - `source_image_family` (string) - The source image family to use to create 134 the new image from. The image family always returns its latest image that 135 is not deprecated. Example: `"debian-8"`. 136 137 - `zone` (string) - The zone in which to launch the instance used to create 138 the image. Example: `"us-central1-a"` 139 140 ### Optional: 141 142 - `account_file` (string) - The JSON file containing your account credentials. 143 Not required if you run Packer on a GCE instance with a service account. 144 Instructions for creating file or using service accounts are above. 145 146 - `address` (string) - The name of a pre-allocated static external IP address. 147 Note, must be the name and not the actual IP address. 148 149 - `disk_size` (integer) - The size of the disk in GB. This defaults to `10`, 150 which is 10GB. 151 152 - `disk_type` (string) - Type of disk used to back your instance, like `pd-ssd` or `pd-standard`. Defaults to `pd-standard`. 153 154 - `image_description` (string) - The description of the resulting image. 155 156 - `image_family` (string) - The name of the image family to which the 157 resulting image belongs. You can create disks by specifying an image family 158 instead of a specific image name. The image family always returns its 159 latest image that is not deprecated. 160 161 - `image_name` (string) - The unique name of the resulting image. Defaults to 162 `"packer-{{timestamp}}"`. 163 164 - `instance_name` (string) - A name to give the launched instance. Beware that 165 this must be unique. Defaults to `"packer-{{uuid}}"`. 166 167 - `machine_type` (string) - The machine type. Defaults to `"n1-standard-1"`. 168 169 - `metadata` (object of key/value strings) - Metadata applied to the launched 170 instance. 171 172 - `network` (string) - The Google Compute network to use for the 173 launched instance. Defaults to `"default"`. 174 175 - `network_project_id` (string) - The project ID for the network and subnetwork 176 to use for launched instance. Defaults to `project_id`. 177 178 - `omit_external_ip` (boolean) - If true, the instance will not have an external IP. 179 `use_internal_ip` must be true if this property is true. 180 181 - `preemptible` (boolean) - If true, launch a preembtible instance. 182 183 - `region` (string) - The region in which to launch the instance. Defaults to 184 to the region hosting the specified `zone`. 185 186 - `scopes` (array of strings) - The service account scopes for launched instance. 187 Defaults to: 188 189 ``` {.json} 190 [ "https://www.googleapis.com/auth/userinfo.email", 191 "https://www.googleapis.com/auth/compute", 192 "https://www.googleapis.com/auth/devstorage.full_control" ] 193 ``` 194 195 - `source_image_project_id` (string) - The project ID of the 196 project containing the source image. 197 198 - `startup_script_file` (string) - The filepath to a startup script to run on 199 the VM from which the image will be made. 200 201 - `state_timeout` (string) - The time to wait for instance state changes. 202 Defaults to `"5m"`. 203 204 - `subnetwork` (string) - The Google Compute subnetwork to use for the launched 205 instance. Only required if the `network` has been created with custom 206 subnetting. 207 Note, the region of the subnetwork must match the `region` or `zone` in 208 which the VM is launched. 209 210 - `tags` (array of strings) 211 212 - `use_internal_ip` (boolean) - If true, use the instance's internal IP 213 instead of its external IP during building. 214 215 ## Startup Scripts 216 217 Startup scripts can be a powerful tool for configuring the instance from which the image is made. 218 The builder will wait for a startup script to terminate. A startup script can be provided via the 219 `startup_script_file` or 'startup-script' instance creation `metadata` field. Therefore, the build 220 time will vary depending on the duration of the startup script. If `startup_script_file` is set, 221 the 'startup-script' `metadata` field will be overwritten. In other words,`startup_script_file` 222 takes precedence. 223 224 The builder does not check for a pass/fail/error signal from the startup script, at this time. Until 225 such support is implemented, startup scripts should be robust, as an image will still be built even 226 when a startup script fails. 227 228 ### Windows 229 Startup scripts do not work on Windows builds, at this time. 230 231 ### Logging 232 Startup script logs can be copied to a Google Cloud Storage (GCS) location specified via the 233 'startup-script-log-dest' instance creation `metadata` field. The GCS location must be writeable by 234 the credentials provided in the builder config's `account_file`. 235 236 ## Gotchas 237 238 Centos and recent Debian images have root ssh access disabled by default. Set `ssh_username` to 239 any user, which will be created by packer with sudo access. 240 241 The machine type must have a scratch disk, which means you can't use an 242 `f1-micro` or `g1-small` to build images.