Creating and Starting a VM Instance

This document explains how to create a virtual machine instance using a boot
disk image, a boot disk snapshot, or a container image. You can create multiple
disks for your VM instance during the creation process. Compute Engine
automatically starts the VM instance after you create it. You can add more disks
to the instance after it is created.

Before you begin

Creating an instance from an image

This section explains how to create an instance from an image of a specific
operating system. A boot disk image contains the
bootloader, root filesystem, and an operating system that runs on the instance.

For advanced users, you can create a custom image to
use in place of the public images offered by Compute Engine.

You can create an instance with an image in the Google Cloud Platform Console, the
gcloud command-line tool, or the API.

Creating an instance from a public image

Public images are provided and maintained by Google, open-source
communities, and third-party vendors. By default, all projects have
access to these images and can use them to create instances with common
operating system images.

Note: You must have access to the public image in order to use it when you
create an instance. By default, you have access to all of the
public images. However, if your project has a defined
list of trusted images,
you can use only the images on that list to create an instance.

Note: The list of zones is randomized
within each region to encourage use across multiple zones.

Select a Machine type for your instance.

In the Boot disk section, click Change to configure your
boot disk.
Create a boot disk no larger than 2 TB to account for the limitations
of MBR partitions.

In the OS images tab, choose an image.

Click Select.

To add secondary non-root disks to your VM instance:

Click on the Management, disks, networking, SSH keys.

Select the Disks tab.

Under Additional disks click Add item.

Specify a disk Name, Mode, and set the
When deleting instance option.

Add additional disks as needed.

Click the Create button to create and start the instance.

gcloud

Before you create an instance, select an operating system image to use for
the boot disk of your instance. Use the
gcloud compute images list
command to see the full list of public images with their image IDs, image
families, and image projects:

To use the latest version of an operating system image, specify the
--image-family and --image-project flags. For example,
debian-9 is an image family that returns the most recent version
of the Debian 9 image that is not deprecated and debian-cloud is the
image project.

If you need to use a specific image version instead of the latest version,
specify the --image and --image-project flags. For example, to create
an instance that uses the debian-9-stretch-v20170619 Debian image, specify
--image debian-9-stretch-v20170619 and --image-project debian-cloud.

You can add up to 15 secondary non-boot disks while you are
creating your instance. Specify the --create-disk flag for each
secondary disk you create.

To create secondary disks from a public or stock image, specify the image
field in the --create-disk flag.
To create a blank disk, do not specify an image source. Optionally, you
can include the disk size and type.

API

If your VPC network is a
custom mode VPC network, you must
also specify the subnet where you want the instance to be created.

After you decide on your resource properties, create a request body
and make your API request. To learn more about constructing API requests
and handling API responses, read the
Creating API Requests and Handling Responses documentation.

Making the API request

If you use the API client library, you can start a new instance by
directly calling the REST API or using the instances().insert. Here is
an example of each option:

REST

In the API, construct a POST request to the instances URI with the same
request body. You can add up to 15 secondary non-boot disks at the
time you create a VM instance by using the initializeParam property for
each additional disk. Create additional disks with a public or a private
image. To add blank disks, do not specify an image source. Optionally, you
can include the diskSizeGb and diskType properties.

[IMAGE_PROJECT] is the image project
that the image belongs to, such as debian-cloud, ubuntu-os-cloud,
and so on.

[IMAGE] is one of the
available public image families.
For example, family/debian-9 uses the latest version of the Debian 9
image. Alternatively, you can use a specific image version such as
debian-9-stretch-v20170619 without the family/ path. For blanks disks
do not specify an image source.

[SIZE_GB] is the disk size.

[DISK_TYPE] is the type of persistent disk, either pd-standard
or pd-ssd.

If you create an instance with blank secondary disks,
format and mount
those disks so that your guest operating system can use them.

Creating an instance from a custom image

A custom image belongs only to your project. To create an instance with a
custom image, you must first have a custom image. To learn how to create a
custom image, read
Creating a Custom Image.

Note: You must have access to the custom image to use it when you
create an instance. By default, you have access to all of the custom images
in your project. However, if your project has a defined
list of trusted images,
you can use only the images on that list to create an instance.

[IMAGE] is an optional field. Use a private or public image. If no
image is specified, the disk will be blank.

If you created your custom images as part of an image family, specify
that image family instead of the image name. By doing so, the instance
automatically uses the most recent, non-deprecated image in the image family.

You can create up to 15 secondary non-boot disks while you are creating your instance.
Include the --create-disk flag with the gcloud compute instances create
command. Use the --create-disk flag for every additional disk you create.
Specify a source image in the image field for each additional disk.
To create a blank disk, do not specify an image source. Optionally, you
can specify the disk size and type.

API

The process for creating an instance with a custom image in the API
is the same as if you were creating an instance with a
publicly-available image. In the sourceImage URI, provide
your own project ID and the image name.

You can create up to 15 secondary non-boot disks at the time you create a
VM instance by using the initializeParam property for each additional disk.
Create additional disks with a public or private image.
To add a blank disk, define the initializeParams entry with no
sourceImage value.

To create up to 15 secondary non-boot disks while you are creating your
instance use the gcloud compute instances create command with
the --create-disk flag. Specify a source image, in the image field.
Use the --create-disk flag for every disk you create. To create a blank
disk, do not specify an image source. Optionally, you can specify the disk
size and type.

API

Follow the API instructions to
create an instance from a public image but specify the
image field in the request body. You can add up to 15 secondary non-boot disks by
specifying the initalizeParams field for every additional disk.
To add blank disks, do not specify an image source. Optionally, you can
specify the diskSizeGb and diskType properties.

Creating an instance from a snapshot

If you backed up a root persistent disk with a
snapshot, you can use that snapshot
to create a new instance. Creating an instance from a snapshot is useful when
you need to roll an instance back to a previous state or restore the root
disk of an instance that you deleted unintentionally.

Note: Persistent disk snapshots are better suited for backing up persistent
disks or copying persistent disks. If you want to modify a root persistent
disk to a certain state and retain that state to start new instances,
create a custom image
of your root persistent disk instead.

Note: The list of zones is randomized
within each region to encourage use across multiple zones.

Select a Machine type for your instance.

In the Boot disk section, click Change to configure your
boot disk.
Create a boot disk no larger than 2 TB to account for the limitations
of MBR partitions.

Click the Snapshots tab and choose a snapshot from the list.

Click Select.

To add secondary non-root disks to your VM instance:

Click on the Management, disks, networking, SSH keys.

Select the Disks tab.

Under Additional disks click Add item.

Specify a disk Name, Mode, and set the
When deleting instance option.

Add additional disks as needed.

Click the Create button to create and start the instance.

gcloud

Using the gcloud command-line tool, you cannot use a snapshot to
directly create an instance the same way that you can in the console.
First, create a new standalone root persistent disk from a snapshot.
Then use that disk to create a new instance.

When you create your instance, you can add up to 15 secondary non-boot
disks from a source image, by using the --create-disk flag and
specifying the image field for each additional disk. To create a blank
disk, do not include a source image. Optionally you can include the
disk size and type.

API

In the API, you cannot use a snapshot to directly create an instance
the same way that you can in the console. First, create a new standalone
root persistent disk from a snapshot. Then use that disk to create a new
instance.

Restrictions:

Only one persistent disk can be the root persistent disk.

You must attach the root persistent disk as the first disk for that
instance.

If you specify the source property, you cannot also specify the
initializeParams property. Providing a source indicates that the
root persistent disk exists already, but the initializeParams
property indicates that Compute Engine should create
a new root persistent disk.

Attach the disk when you create a new instance. In the request body
include the properties to create a new instance. In the disks
property, include the source field with a URL to the persistent disk
that you want to attach. To add up to 15 secondary non-boot disks, use
the initializeParams property for every disk. To add blank disks,
do not include a source image.
Optionally, you can specify the diskSizeGb and diskType properties.

Creating an instance from a container image

Beta

This is a Beta release of
Containers on Compute Engine. This feature
is not covered by any SLA or deprecation policy and might be subject to backward-incompatible
changes.

To deploy and launch a container on a Compute Engine instance, specify
a container image name and optional configuration parameters when you
create the instance. Compute Engine creates the instance using the
latest version of the
Container-Optimized OS public image, which has
Docker installed. Then, Compute Engine launches the container when
the VM starts. See
Deploying Containers on VMs for
more information.

When using a container image from Docker Hub, you must always specify a full
Docker image name. For example, specify the following image name to deploy
an Apache container image: docker.io/httpd:2.4.

Creating an instance with access to other Google Cloud Platform Services

If you plan to run an application on your virtual machine instance that needs
access to other Google Cloud Platform services,
create a service account
before creating the instance, and then follow the instructions to
set up an instance to run as a service account.
A service account is a special account whose credentials you can use in your
application code to access other Google Cloud Platform services.

To create up to 15 secondary non-boot disks from a source image while you are
creating your instance, use the gcloud compute instances create command
with the --create-disk flag. Use the --create-disk flag for every disk
you create. To create a blank disk, do not include a source image.
Optionally you can include the disk size and type.

API

Follow the API instructions to
create an instance from an image or a
snapshot, but specify the subnet field in the request
body. To add up to 15 secondary non-boot disks, use the initializeParams
property for every disk you create. To add blank disks, do not add a source
image. Optionally, you can specify the diskSizeGb and diskType properties.