github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/website/source/docs/builders/googlecompute.html.md (about)

     1  ---
     2  description: |
     3      The googlecompute Packer builder is able to create images for use with
     4      Google Cloud Compute Engine (GCE) based on existing images.
     5  layout: docs
     6  page_title: 'Google Compute - Builders'
     7  sidebar_current: 'docs-builders-googlecompute'
     8  ---
     9  
    10  # Google Compute Builder
    11  
    12  Type: `googlecompute`
    13  
    14  The `googlecompute` Packer builder is able to create
    15  [images](https://developers.google.com/compute/docs/images) for use with [Google
    16  Compute Engine](https://cloud.google.com/products/compute-engine)(GCE) based on
    17  existing images. Building GCE images from scratch is not possible from Packer at
    18  this time. For building images from scratch, please see
    19  [Building GCE Images from Scratch](https://cloud.google.com/compute/docs/tutorials/building-images).
    20  
    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  ``` shell
    43  $ gcloud compute --project YOUR_PROJECT instances create "INSTANCE-NAME" ... \
    44      --scopes "https://www.googleapis.com/auth/compute,https://www.googleapis.com/auth/devstorage.full_control" \
    45  ```
    46  
    47  For the [Google Developers Console](https://console.developers.google.com):
    48  
    49  1.  Choose "Show advanced options"
    50  2.  Tick "Enable Compute Engine service account"
    51  3.  Choose "Read Write" for Compute
    52  4.  Chose "Full" for "Storage"
    53  
    54  **The service account will be used automatically by Packer as long as there is
    55  no *account file* specified in the Packer configuration file.**
    56  
    57  ### Running Without a Compute Engine Service Account
    58  
    59  The [Google Developers Console](https://console.developers.google.com) allows
    60  you to create and download a credential file that will let you use the
    61  `googlecompute` Packer builder anywhere. To make the process more
    62  straightforwarded, it is documented here.
    63  
    64  1.  Log into the [Google Developers
    65      Console](https://console.developers.google.com) and select a project.
    66  
    67  2.  Under the "API Manager" section, click "Credentials."
    68  
    69  3.  Click the "Create credentials" button, select "Service account key"
    70  
    71  4.  Create new service account that at least has `Compute Engine Instance Admin (v1)` and `Service Account User` roles.
    72  
    73  5.  Chose `JSON` as Key type and click "Create".
    74      A JSON file will be downloaded automatically. This is your *account file*.
    75  
    76  ### Precedence of Authentication Methods
    77  
    78  Packer looks for credentials in the following places, preferring the first location found:
    79  
    80  1.  A `account_file` option in your packer file.
    81  
    82  2.  A JSON file (Service Account) whose path is specified by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable.
    83  
    84  3.  A JSON file in a location known to the `gcloud` command-line tool. (`gcloud` creates it when it's configured)
    85  
    86      On Windows, this is:
    87  
    88          %APPDATA%/gcloud/application_default_credentials.json
    89  
    90      On other systems:
    91  
    92          $HOME/.config/gcloud/application_default_credentials.json
    93  
    94  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)
    95  
    96  ## Basic Example
    97  
    98  Below is a fully functioning example. It doesn't do anything useful, since no
    99  provisioners or startup-script metadata are defined, but it will effectively
   100  repackage an existing GCE image. The account\_file is obtained in the previous
   101  section. If it parses as JSON it is assumed to be the file itself, otherwise it
   102  is assumed to be the path to the file containing the JSON.
   103  
   104  ``` json
   105  {
   106    "builders": [
   107      {
   108        "type": "googlecompute",
   109        "account_file": "account.json",
   110        "project_id": "my project",
   111        "source_image": "debian-7-wheezy-v20150127",
   112        "zone": "us-central1-a"
   113      }
   114    ]
   115  }
   116  ```
   117  
   118  ### Windows Example
   119  
   120  Running WinRM requires that it is opened in the firewall and that the VM enables WinRM for the
   121  user used to connect in a startup-script.
   122  
   123  ``` {.json}
   124  {
   125    "builders": [{
   126      "type": "googlecompute",
   127      "account_file": "account.json",
   128      "project_id": "my project",
   129      "source_image": "windows-server-2016-dc-v20170227",
   130      "disk_size": "50",
   131      "machine_type": "n1-standard-1",
   132      "communicator": "winrm",
   133      "winrm_username": "packer_user",
   134      "winrm_insecure": true,
   135      "winrm_use_ssl": true,
   136      "metadata": {
   137        "windows-startup-script-cmd": "winrm quickconfig -quiet & net user /add packer_user & net localgroup administrators packer_user /add & winrm set winrm/config/service/auth @{Basic=\"true\"}"
   138      },
   139      "zone": "us-central1-a"
   140    }]
   141  }
   142  ```
   143  
   144  ## Configuration Reference
   145  
   146  Configuration options are organized below into two categories: required and
   147  optional. Within each category, the available options are alphabetized and
   148  described.
   149  
   150  In addition to the options listed here, a
   151  [communicator](/docs/templates/communicator.html) can be configured for this
   152  builder.
   153  
   154  ### Required:
   155  
   156  -   `project_id` (string) - The project ID that will be used to launch instances
   157      and store images.
   158  
   159  -   `source_image` (string) - The source image to use to create the new image
   160      from. You can also specify `source_image_family` instead. If both
   161      `source_image` and `source_image_family` are specified, `source_image`
   162      takes precedence. Example: `"debian-8-jessie-v20161027"`
   163  
   164  -   `source_image_family` (string) - The source image family to use to create
   165      the new image from. The image family always returns its latest image that
   166      is not deprecated. Example: `"debian-8"`.
   167  
   168  -   `zone` (string) - The zone in which to launch the instance used to create
   169      the image. Example: `"us-central1-a"`
   170  
   171  ### Optional:
   172  
   173  -   `account_file` (string) - The JSON file containing your account credentials.
   174      Not required if you run Packer on a GCE instance with a service account.
   175      Instructions for creating file or using service accounts are above.
   176  
   177  -   `accelerator_count` (int) - Number of guest accelerator cards to add to the launched instance.
   178  
   179  -   `accelerator_type` (string) - Full or partial URL of the guest accelerator type. GPU accelerators can only be used with
   180      `"on_host_maintenance": "TERMINATE"` option set.
   181      Example: `"projects/project_id/zones/europe-west1-b/acceleratorTypes/nvidia-tesla-k80"`
   182  
   183  -   `address` (string) - The name of a pre-allocated static external IP address.
   184      Note, must be the name and not the actual IP address.
   185  
   186  -   `disk_name` (string) - The name of the disk, if unset the instance name will be
   187      used.
   188  
   189  -   `disk_size` (integer) - The size of the disk in GB. This defaults to `10`,
   190      which is 10GB.
   191  
   192  -   `disk_type` (string) - Type of disk used to back your instance, like `pd-ssd` or `pd-standard`. Defaults to `pd-standard`.
   193  
   194  -   `image_description` (string) - The description of the resulting image.
   195  
   196  -   `image_family` (string) - The name of the image family to which the
   197      resulting image belongs. You can create disks by specifying an image family
   198      instead of a specific image name. The image family always returns its
   199      latest image that is not deprecated.
   200  
   201  -   `image_labels` (object of key/value strings) - Key/value pair labels to
   202      apply to the created image.
   203  
   204  -   `image_name` (string) - The unique name of the resulting image. Defaults to
   205      `"packer-{{timestamp}}"`.
   206  
   207  -   `instance_name` (string) - A name to give the launched instance. Beware that
   208      this must be unique. Defaults to `"packer-{{uuid}}"`.
   209  
   210  -   `labels` (object of key/value strings) - Key/value pair labels to apply to
   211      the launched instance.
   212  
   213  -   `machine_type` (string) - The machine type. Defaults to `"n1-standard-1"`.
   214  
   215  -   `metadata` (object of key/value strings) - Metadata applied to the launched
   216      instance.
   217  
   218  -   `network` (string) - The Google Compute network id or URL to use for the
   219      launched instance. Defaults to `"default"`.
   220  
   221  -   `network_project_id` (string) - The project ID for the network and subnetwork
   222      to use for launched instance. Defaults to `project_id`.
   223  
   224  -   `omit_external_ip` (boolean) - If true, the instance will not have an external IP.
   225      `use_internal_ip` must be true if this property is true.
   226  
   227  -   `on_host_maintenance` (string) - Sets Host Maintenance Option. Valid
   228      choices are `MIGRATE` and `TERMINATE`. Please see [GCE Instance Scheduling
   229      Options](https://cloud.google.com/compute/docs/instances/setting-instance-scheduling-options),
   230      as not all machine\_types support `MIGRATE` (i.e. machines with GPUs).
   231      If preemptible is true this can only be `TERMINATE`. If preemptible
   232      is false, it defaults to `MIGRATE`
   233  
   234  -   `preemptible` (boolean) - If true, launch a preembtible instance.
   235  
   236  -   `region` (string) - The region in which to launch the instance. Defaults to
   237      to the region hosting the specified `zone`.
   238  
   239  -   `scopes` (array of strings) - The service account scopes for launched instance.
   240      Defaults to:
   241  
   242      ``` json
   243      [
   244        "https://www.googleapis.com/auth/userinfo.email",
   245        "https://www.googleapis.com/auth/compute",
   246        "https://www.googleapis.com/auth/devstorage.full_control"
   247      ]
   248      ```
   249  
   250  -   `source_image_project_id` (string) - The project ID of the
   251      project containing the source image.
   252  
   253  -   `startup_script_file` (string) - The filepath to a startup script to run on
   254      the VM from which the image will be made.
   255  
   256  -   `state_timeout` (string) - The time to wait for instance state changes.
   257      Defaults to `"5m"`.
   258  
   259  -   `subnetwork` (string) - The Google Compute subnetwork id or URL to use for
   260      the launched instance. Only required if the `network` has been created with
   261      custom subnetting. Note, the region of the subnetwork must match the `region`
   262      or `zone` in which the VM is launched.
   263  
   264  -   `tags` (array of strings)
   265  
   266  -   `use_internal_ip` (boolean) - If true, use the instance's internal IP
   267      instead of its external IP during building.
   268  
   269  ## Startup Scripts
   270  
   271  Startup scripts can be a powerful tool for configuring the instance from which the image is made.
   272  The builder will wait for a startup script to terminate. A startup script can be provided via the
   273  `startup_script_file` or 'startup-script' instance creation `metadata` field. Therefore, the build
   274  time will vary depending on the duration of the startup script. If `startup_script_file` is set,
   275  the 'startup-script' `metadata` field will be overwritten. In other words,`startup_script_file`
   276  takes precedence.
   277  
   278  The builder does not check for a pass/fail/error signal from the startup script, at this time. Until
   279  such support is implemented, startup scripts should be robust, as an image will still be built even
   280  when a startup script fails.
   281  
   282  ### Windows
   283  
   284  A Windows startup script can only be provided via the 'windows-startup-script-cmd' instance
   285  creation `metadata` field. The builder will *not* wait for a Windows startup scripts to
   286  terminate. You have to ensure that it finishes before the instance shuts down.
   287  
   288  ### Logging
   289  
   290  Startup script logs can be copied to a Google Cloud Storage (GCS) location specified via the
   291  'startup-script-log-dest' instance creation `metadata` field. The GCS location must be writeable by
   292  the credentials provided in the builder config's `account_file`.
   293  
   294  ## Gotchas
   295  
   296  CentOS and recent Debian images have root ssh access disabled by default. Set `ssh_username` to
   297  any user, which will be created by packer with sudo access.
   298  
   299  The machine type must have a scratch disk, which means you can't use an
   300  `f1-micro` or `g1-small` to build images.