This page contains code samples for Compute Engine. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.
View in documentation
Attach a regional disk to a Compute Engine VM instance in read-only mode
Attach a regional disk to a Compute Engine VM instance in read-write mode Attach a regional disk to a single virtual machine running in Google Compute Engine. The disk also needs to be mounted by the operating system before it can be used. The disk is attached in read-only mode. The disk can be attached to multiple VM instances simultaneously.
Attach a regional disk to a Compute Engine VM instance in read-write mode
Attach a regional disk to a single virtual machine running in Google Compute Engine. The disk also needs to be mounted by the operating system before it can be used. The disk is attached in read-write mode. The disk can only be attached to one VM instance at a time.
Change the auto-delete setting of a disk
Change the value of the auto-delete flag, which determines whether the disk attached to a VM is automatically deleted or not when you delete the VM.
View in documentation
Change the deletion protection setting for an existing VM
Enable deletion protection for an existing VM without stopping the VM.
View in documentation
View in documentation
Create a disk image
Create a disk image from a source disk and store it in a Cloud Storage location.
View in documentation
Create a disk snapshot
Create a zonal or a regional snapshot of a disk in the specified Cloud Storage location.
View in documentation
Create a preemptible VM
Use the specified network, machine type, and source image and create a preemptible VM.
View in documentation
Create a VM from a custom image
Use a custom image as the boot disk and create a VM.
View in documentation
Create a VM from existing disk
Create a disk from a snapshot and attach the disk to a new VM.
View in documentation
Create a VM instance
Create a VM instance in the specified project and zone.
View in documentation
Create a VM instance from an instance template
Use an instance template to create a VM instance in the specified project and zone.
View in documentation
Create a VM instance from an instance template with overrides
Create a VM instance from an instance template, then override the disk and machine type options in the template.
View in documentation
Create a VM with a custom machine type
Create a VM with the specified number of vCPUs and the specified amount of memory.
View in documentation
View in documentation
Create a VM with custom machine type and extended memory
Extend the amount of memory per vCPU and create a VM with that custom machine type.
View in documentation
Create a Windows Server image
Create a Windows Server image from the specified source disk.
View in documentation
Create a Windows Server VM with external IP address
Configure routes and firewall rules in your custom VPC network and create a Windows Server VM that has an external IP address.
View in documentation
Create a Windows Server VM with internal IP address
Configure routes, firewall rules, and subnets in your custom VPC network and create a Windows Server VM that has an internal IP address.
View in documentation
Create an instance template based on an existing instance
Use the configuration of an existing VM to create an instance template.
View in documentation
Create an instance template with a subnet
Create an instance template that uses a specified subnetwork.
View in documentation
View in documentation
View in documentation
View in documentation
Delete a VM instance
Delete an instance based on the project, zone, and instance name that you provide.
View in documentation
Delete an instance template
Delete an instance template from a specified project.
View in documentation
Enable deletion protection for a new VM
Create a VM in the specified project and zone with deletion protection enabled.
View in documentation
Enable OS Login
Use this sample to set the project-level or instance-level metadata and enable OS Login for VMs in a project.
View in documentation
Get information about an instance template
Get the basic information and instance configuration defined in an instance template.
View in documentation
Get the deletion protection status of a VM
Determine whether deletion protection is enabled for a specified VM.
View in documentation
List all non-deprecated images in a project
Get a list of all non-deprecated images available in a specified project.
View in documentation
List instance templates in a project
Get a list of instance templates defined in a project.
View in documentation
Reset a VM instance
Performing a reset on your VM is similar to doing a hard reset on your computer, where you might press a reset button or press and hold the power button. Resetting a VM forcibly wipes the memory contents of the machine and resets the VM to its initial state. The VM does not perform a clean shutdown of the guest OS. Throughout this process, the VM remains in the RUNNING state.
View in documentation