All Compute Engine code samples

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.

Add memory to an existing VM

Update a VM with extended memory per vCPU.

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.

Change the deletion protection setting for an existing VM

Enable deletion protection for an existing VM without stopping the VM.

View in documentation

Check operation status

Request to wait until an operation completes and get the operation status before continuing code execution.

View in documentation

Create a disk from a snapshot

Create a zonal disk from a snapshot.

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.

Create a firewall rule

Create a firewall rule that allows incoming HTTP and HTTPS access from the entire internet.

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.

Create a VM instance

Create a VM instance in the specified project and zone.

Create a VM instance from an instance template

Use an instance template to create a VM instance in the specified project and zone.

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.

Create a VM with a custom hostname

Create a VM with a custom hostname that conforms to the RFC 1035 requirements for valid hostnames.

Create a VM with a custom machine type

Create a VM with the specified number of vCPUs and the specified amount of memory.

Create a VM with a local SSD

Attach a local SSD to a VM during VM creation.

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.

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

Use the specified machine type, boot disk and network to create an instance template.

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

Delete a disk

Delete a persistent disk from the specified zone.

Delete a disk image

Delete a disk image from the specified project.

View in documentation

Delete a disk snapshot

Delete a disk snapshot from the specified project.

Delete a firewall rule

Delete a firewall rule from a specified project.

View in documentation

Delete a VM instance

Delete an instance based on the project, zone, and instance name that you provide.

Delete an instance template

Delete an instance template from a specified project.

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.

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.

List firewall rules in a project

Get a list of all firewall rules defined in your project.

View in documentation

List instance templates in a project

Get a list of instance templates defined in a project.

List VM instances in a project

List instances in a project, either in one zone or across all zones.

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

Resume a suspended VM

Resume a suspended VM in the specified project and zone.

View in documentation

Start a VM instance

Restart a stopped VM instance.

View in documentation

Start a VM with encrypted disks

Provide an encryption key and start a VM instance that has encrypted disks.

View in documentation

Stop a VM instance

Stop a running VM instance.

View in documentation

Suspend a running VM

Suspend a running VM in the specified project and zone.

View in documentation

Update a firewall rule

Modify the priority of a firewall rule.

View in documentation