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. Show 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: If you are bringing an existing license, see Bringing your own license with sole-tenant nodes. Before you begin
Create a VM instance from an imageThis 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 EngineBefore 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.
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 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).
A custom image belongs only to your project. To create a VM with a custom image, you must first create a custom image if you don't already have one. Note: You must have access to the custom image to use it when you create a VM. By default, you have access to all 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 a VM.
Run the gcloud compute instances create command to create a VM with a custom image: gcloud compute instances create VM_NAME \ --image-project IMAGE_PROJECT \ [--image IMAGE | --image-family IMAGE_FAMILY] --subnet SUBNET Replace the following:
The process for creating a VM with a custom image in the API is the same as if you were creating a VM with a publicly available image. To create the VM from a custom image, use the instances.insert method. 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:
Run the gcloud compute instances create command to create a VM with additional non-boot disks. You can add up to 128 non-boot disks while you're creating your VM. Specify the --create-disk flag for each non-boot disk you create. To create non-boot disks from a public or stock image, specify the image or image-family and image-project properties with the --create-disk flag. To create a blank disk, don't include these properties. You can optionally include properties for the disk size and type. gcloud compute instances create VM_NAME \ [--image=IMAGE | --image-family=IMAGE_FAMILY] \ --image-project=IMAGE_PROJECT \ --create-disk [image=DISK_IMAGE | image-family=DISK_IMAGE_FAMILY ],image-project=DISK_IMAGE_PROJECT,size=SIZE_GB,type=DISK_TYPEReplace the following:
You can create up to 128 non-boot disks at the time you create a VM by using the initializeParams 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. 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 }, { "initializeParams":{ "diskSizeGb":"SIZE_GB", "sourceImage":"projects/DISK_IMAGE_PROJECT/global/images/DISK_IMAGE", "diskType":"DISK_TYPE" } }, { "initializeParams":{ "diskSizeGb":"SIZE_GB", "diskType":"DISK_TYPE" } } ], "networkInterfaces":[ { "network":"global/networks/NETWORK_NAME" } ], "shieldedInstanceConfig":{ "enableSecureBoot":"ENABLE_SECURE_BOOT" } } Replace the following:
Format and mount the disks before using them. If another user has shared an image with you, you can use the image to create a VM.
Create a VM by using the gcloud compute instances create command, and use the --image and --image-project flags to specify the image name and the project where the image resides: gcloud compute instances create VM_NAME \ --image=IMAGE \ --image-project=IMAGE_PROJECTReplace the following:
If the command is successful, gcloud responds with the properties of the new VM: Created [https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-b/instances/example-instance]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-instance us-central1-b e2-standard-2 10.240.0.4 104.198.53.60 RUNNINGFollow the API instructions to create a VM from a public image, but specify the image field in the request body. To add blank disks, don't specify an image source. You can optionally specify the diskSizeGb, diskType, and labels properties. [... image: "projects/PROJECT_ID/global/images/IMAGE_NAME { "initializeParams": { "diskSizeGb": "SIZE_GB", "sourceImage": "IMAGE" } } ...]Replace the following:
For blank disks, don't specify an image source. Create a VM from a snapshotYou can create a new VM from a snapshot in the following ways: To quickly create more than one VM with the same boot disk, create a custom image, then create VMs from that image instead of using a snapshot. Create a VM instance from a container imageTo deploy and launch a container on a Compute Engine VM, specify a container image name and optional configuration parameters when you create the VM. Compute Engine creates the VM by 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. For more information, see Deploying containers on VMs. To create a VM from a container image, you must use the Google Cloud console or gcloud.
Run the gcloud compute instances create-with-container command: gcloud compute instances create-with-container VM_NAME \ --container-image=CONTAINER_IMAGEReplace the following:
For example, the following command creates a VM named nginx-vm, which launches and runs the container image: gcr.io/cloud-marketplace/google/nginx1:1.12 gcloud compute instances create-with-container nginx-vm \ --container-image=gcr.io/cloud-marketplace/google/nginx1:1.12To deploy an Apache container image from Docker Hub, always specify the full Docker image name: docker.io/httpd:2.4. If you plan to run an application on your VM that needs access to other Google Cloud services, create a service account before creating the VM, and then set up the VM 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 services. For more information, see Service accounts. By default, Google Cloud creates an auto mode VPC network called default for each project. To use a different network or a subnet that you manually created in an auto mode or custom mode VPC network, you must specify the subnet when you create the VM. While creating a VM in a subnet, consider these rules:
Using the Google Cloud CLI, follow the same instructions to create a VM from an image or a snapshot, and add the --subnet=SUBNET_NAME and --zone=ZONE flags when you run the gcloud compute instances create command: gcloud compute instances create VM_NAME \ --network=NETWORK_NAME \ --subnet=SUBNET_NAME \ --zone=ZONEReplace the following:
Follow the API instructions to create a VM from an image or a snapshot, but specify the subnet field in the request body. To add blank disks, do not add a source image. You can optionally specify the diskSizeGb, diskType, and labels properties. 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", "subnetwork":"regions/REGION/subnetworks/SUBNET_NAME", "accessConfigs":{ "name":"External NAT", "type":"ONE_TO_ONE_NAT" } } ], "shieldedInstanceConfig":{ "enableSecureBoot":"ENABLE_SECURE_BOOT" } } Replace the following:
To find methods for resolving common VM creation errors, see Troubleshooting VM creation. What's next?
If you're new to Google Cloud, create an account to evaluate how Compute Engine performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads. Try Compute Engine free |