This document explains how to create a virtual machine (VM) instance by using a boot disk image, a boot disk snapshot, or a container image. Some images support Shielded VM features, which offer security features such as UEFI-compliant firmware, Secure Boot, and vTPM-protected Measured Boot. On Shielded VMs, vTPM and integrity monitoring are enabled by default.
While creating your VM, you can create one or more disks for it. You can also add more disks to the VM after it's created. Compute Engine automatically starts the VM instance after you create it.
While creating a VM, you can also add multiple network interfaces. To mitigate your VM's exposure to threats on the internet, you can omit the external IP address when you add a network interface to the instance. In such cases, the VM is accessible only from other VMs in the same VPC network or a linked network unless you configure Cloud NAT.
If you are creating a VM for the first time, see Quickstart using a Linux VM or Quickstart using a Windows Server VM.
For more specific or complicated VM creation, see the following resources:
- Creating Windows Server instances
- Creating SQL Server instances
- Creating instances on sole-tenant nodes
- Creating a VM instance with a custom hostname
- Reserving instances and consuming reserved instances
- Creating VM instances that use the gVNIC network interface
- Creating and starting an Arm VM instance
- Configuring a VM instance with higher bandwidth
- Creating a VM instance with attached GPUs
- Creating a VM instance with a high-performance computing (HPC) image
- Using the bulk instance API
- Creating a VM instance with an attached instance schedule
- Creating a managed instance group (MIG)
If you are bringing an existing license, see Bringing your own license with sole-tenant nodes.
Before you begin
- If you want to use the command-line examples in this guide, do the following:
- Install or update to the latest version of the Google Cloud CLI.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
- When creating VMs from images or disks by using the Google Cloud CLI or the Compute Engine API, there's a limit of 20 VM instances per second. If you need to create a higher number of VMs per second, request a higher quota limit for the Images resource.
Create a VM instance from an image
This section explains how to create a VM from a public OS image or a custom image. A VM contains a bootloader, a boot file system, and an OS image.
View a list of public images available on Compute Engine
Before you create a VM by using a public image, review the list of public images that are available on Compute Engine.
For more information about the features available with each public image, see Feature support by operating system.
Console
In the Google Cloud console, go to the Images page.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Run the following command:
gcloud compute images list
Make a note of the name of the image or image family and the name of the project containing the image.
Optional: To determine whether the image supports Shielded VM features, run the following command:
gcloud compute images describe IMAGE_NAME \ --project=IMAGE_PROJECT
Replace the following:
IMAGE_NAME
: name of the image to check for support of Shielded VM featuresIMAGE_PROJECT
: project containing the image
If the image supports Shielded VM features, the following line appears in the output:
type: UEFI_COMPATIBLE
.
API
Run the following command:
GET https://compute.googleapis.com/compute/v1/projects/IMAGE_PROJECT/global/images/
Make a note of the name of the image or image family and the name of the project containing the image.
Optional: To determine whether the image supports Shielded VM features, run the following command:
GET https://compute.googleapis.com/compute/v1/projects/IMAGE_PROJECT/global/images/IMAGE_NAME
Replace the following:
IMAGE_PROJECT
: project containing the imageIMAGE_NAME
: name of the image to check for support of Shielded VM features
If the image supports Shielded VM features, the following line appears in the output:
type: UEFI_COMPATIBLE
.
C#
Before trying this sample, follow the C# setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine C# API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
Before trying this sample, follow the PHP setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine PHP API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Ruby API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Create a VM instance from a public image
Google, open source communities, and third-party vendors provide and maintain public OS images. By default, all Google Cloud projects can create VMs from public OS images. However, if your Google Cloud project has a defined list of trusted images, you can use only the images on that list to create a VM.
If you create a Shielded VM image with a local SSD, you can't shield data with integrity monitoring or the virtual platform trusted module (vTPM).
Console
In the Google Cloud console, go to the VM instances page.
Select your project and click Continue.
Click Create instance.
Specify a Name for your VM. For more information, see Resource naming convention.
Optional: Change the Zone for this VM. Compute Engine randomizes the list of zones within each region to encourage use across multiple zones.
Select a Machine configuration for your VM.
In the Boot disk section, click Change, and then do the following:
- On the Public images tab, choose the following:
- Operating system
- OS version
- Boot disk type
- Boot disk size
- Optional: For advanced configuration options, click Show advanced configuration.
- To confirm your boot disk options, click Select.
- On the Public images tab, choose the following:
In the Firewall section, to permit HTTP or HTTPS traffic to the VM, select Allow HTTP traffic or Allow HTTPS traffic. When you select one of these, Compute Engine adds a network tag to your VM, which associates the firewall rule with the VM. Then, Compute Engine creates the corresponding ingress firewall rule that allows all incoming traffic on
tcp:80
(HTTP) ortcp:443
(HTTPS).Optional: If you chose an OS image that supports Shielded VM features, you can modify the Shielded VM settings. To modify shielded VM settings, expand the Security section in the Networking, disks, security, management, sole tenancy section and do the following, as required:
To turn on Secure Boot, select Turn on Secure Boot. Secure Boot is disabled by default.
To turn off vTPM, clear the Turn on vTPM checkbox. vTPM is enabled by default. Disabling vTPM also disables integrity monitoring because integrity monitoring relies on data gathered by Measured Boot.
To turn off integrity monitoring, clear the Turn on Integrity Monitoring checkbox. Integrity monitoring is enabled by default.
To create and start the VM, click Create.
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
Select a public image. Make a note of the name of the image or image family and the name of the project containing the image.
Use the
gcloud compute instances create
command to create a VM from an image family or from a specific version of an OS image.If you specify the optional
--shielded-secure-boot
flag, Compute Engine creates a VM with all three of the Shielded VM features enabled:After Compute Engine starts your VM, you must stop the VM to modify Shielded VM options.
gcloud compute instances create VM_NAME \ [--image=IMAGE | --image-family=IMAGE_FAMILY] \ --image-project=IMAGE_PROJECT --machine-type=MACHINE_TYPE
Replace the following:
VM_NAME
: name of the new VMIMAGE
orIMAGE_FAMILY
: specify one of the following:IMAGE
: a specific version of a public imageFor example,
--image=debian-10-buster-v20200309
.IMAGE_FAMILY
: an image family.This creates the VM from the most recent, non-deprecated OS image. For example, if you specify
--image-family=debian-10
, Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.
IMAGE_PROJECT
: project containing the imageMACHINE_TYPE
: machine type, predefined or custom, for the new VMTo get a list of the machine types available in a zone, use the
gcloud compute machine-types list
command with the--zones
flag.
Verify that Compute Engine created the VM:
gcloud compute instances describe VM_NAME
Replace
VM_NAME
with the name of the VM.
Terraform
To create a VM, you can use the google_compute_instance
resource.
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.
To generate the Terraform code, you can use the Equivalent code component in the Google Cloud console.- In the Google Cloud console, go to the VM instances page.
- Click Create instance.
- Specify the parameters you want.
- At the top or bottom of the page, click Equivalent code, and then click the Terraform tab to view the Terraform code.
API
- Select a public image. Make a note of the name of the image or image family and the name of the project containing the image.
Use the
instances.insert
method to create a VM from an image family or from a specific version of an OS image:POST https://compute.googleapis.com/compute/v1/projects/
PROJECT_ID
/zones/ZONE
/instances { "machineType":"zones/MACHINE_TYPE_ZONE/machineTypes/MACHINE_TYPE", "name":"VM_NAME
", "disks":[ { "initializeParams":{ "sourceImage":"projects/IMAGE_PROJECT/global/images/IMAGE" }, "boot":true } ], "networkInterfaces":[ { "network":"global/networks/NETWORK_NAME" } ], "shieldedInstanceConfig":{ "enableSecureBoot":"ENABLE_SECURE_BOOT" } }Replace the following:
PROJECT_ID
: ID of the project to create the VM inZONE
: zone to create the VM inMACHINE_TYPE_ZONE
: zone containing the machine type to use for the new VMMACHINE_TYPE
: machine type, predefined or custom, for the new VMVM_NAME
: name of the new VMIMAGE_PROJECT
: project containing the image
For example, if you specifydebian-10
as the image family, specifydebian-cloud
as the image project.IMAGE or IMAGE_FAMILY
: specify one of the following:IMAGE
: a specific version of a public image
For example,"sourceImage": "projects/debian-cloud/global/images/debian-10-buster-v20200309"
IMAGE_FAMILY
: an image family
This creates the VM from the most recent, non-deprecated OS image. For example, if you specify"sourceImage": "projects/debian-cloud/global/images/family/debian-10"
, Compute Engine creates a VM from the latest version of the OS image in theDebian 10
image family.
NETWORK_NAME
: the VPC network that you want to use for the VM. You can specifydefault
to use your default network.ENABLE_SECURE_BOOT
: Optional: If you chose an image that supports Shielded VM features, Compute Engine, by default, enables the virtual trusted platform module (vTPM) and integrity monitoring. Compute Engine does not enable Secure Boot by default.If you specify
true
forenableSecureBoot
, Compute Engine creates a VM with all three Shielded VM features enabled. After Compute Engine starts your VM, to modify Shielded VM options, you must stop the VM.
C#
C#
Before trying this sample, follow the C# setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine C# API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Go
Go
Before trying this sample, follow the Go setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Go API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Before trying this sample, follow the Java setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Java API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Node.js API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
PHP
Before trying this sample, follow the PHP setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine PHP API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Before trying this sample, follow the Python setup instructions in the Compute Engine quickstart using client libraries. For more information, see the Compute Engine Python API reference documentation.
To authenticate to Compute Engine, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Ruby
Before trying this sample, follow the Ruby setup instructions in the