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.
Delete a disk snapshot
Delete a disk snapshot from the specified project.
- Compute Engine
- Google Cloud
- Java
- Node JS
- JavaScript
- Go
- Python
Configure stateful IP addresses on MIG creation
Configure stateful IP addresses when you create a managed instance group (MIG).
- Google Cloud
- Compute Engine
- Terraform
Create an instance template for a MIG of GPU VMs
Create a global instance template that you can use to create a managed instance group (MIG) of GPU VMs.
- Compute Engine
- Google Cloud
- Terraform
Create an endpoint to access Google APIs
Use an internal IP address and create Private Service Connect endpoints to connect to Google APIs.
- Compute Engine
- Google Cloud
- Terraform
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.
- Google Cloud
- Compute Engine
- JavaScript
- Node JS
- Python
- Java
- Go
Create an empty disk
This sample creates a new empty disk in a project in a given zone.
- Google Cloud
- Compute Engine
- Go
- C++
- Python
- Java
Create an instance template based on an existing instance
Use the configuration of an existing VM to create an instance template.
- Compute Engine
- Google Cloud
- Python
- Java
- Node JS
- JavaScript
- Go
Create custom machine types without helper function
This sample creates 7 new VM instances without using a CustomMachineType helper function. It creates instances with different machine types, including custom machine types, and demonstrates how to specify the number of CPU cores and memory for each instance.
- Google Cloud
- Compute Engine
- Java
- JavaScript
- Node JS
- Go
- Python
Create an instance with a virtual display device
This sample creates a f1-micro instance with a virtual display device in us-central1-c zone.
- Compute Engine
- Google Cloud
- Terraform
Create an instance template with a subnet
Create an instance template that uses a specified subnetwork.
- Compute Engine
- Google Cloud
- Node JS
- Python
- Java
- Go
- JavaScript
Create a health check for autohealing in MIGs
The sample creates a global health check which you use to configure autohealing in a managed instance group (MIG).
- Google Cloud
- Compute Engine
- Terraform
Get a snapshot
This sample retrieves information about a snapshot.
- Compute Engine
- Google Cloud
- Go
- Java
- Python
List preemption history of a Google Compute Engine instance
This sample lists all preemption operations that happened in a given zone in a project. Optionally, you can filter the results to only include operations for a specific instance.
- Compute Engine
- Google Cloud
- Go
- Java
- Python
- JavaScript
- Node JS
Reserve a static IP Address
Reserve a fixed IP address in a specific region.
- Google Cloud
- Compute Engine
- Terraform
Delete a firewall rule
Delete a firewall rule from a specified project.
- Google Cloud
- Compute Engine
- PHP
- Node JS
- Go
- Java
- JavaScript
- C#
- Ruby
- Python
Check if a given instance is Spot VM or not
This sample checks if a given instance is Spot VM or not.
- Google Cloud
- Compute Engine
- Java
- Python
- Go
Create a preemptible VM
Use the specified network, machine type, and source image and create a preemptible VM.
- Google Cloud
- Compute Engine
- Java
- Node JS
- JavaScript
- Go
- Python
Create a firewall to allow health checks for autohealing in MIGs
This sample creates a firewall rule to allow health check probes to connect to your app when autohealing is configured in your MIG.
- Google Cloud
- Compute Engine
- Terraform
Add memory to an existing VM
Update a VM with extended memory per vCPU.
- Compute Engine
- Google Cloud
- Go
- Python
- JavaScript
- Java
- Node JS
Update a firewall rule
Modify the priority of a firewall rule.
- Google Cloud
- Compute Engine
- JavaScript
- Java
- Python
- Go
- PHP
- C#
- Node JS
- Ruby
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.
- Google Cloud
- Compute Engine
- Go
Create an instance template
Use the specified machine type, boot disk and network to create an instance template.
- Compute Engine
- Google Cloud
- Go
- JavaScript
- Java
- Node JS
- Terraform
- Python
List images in a project
This sample demonstrates how to list all the images available in a given project.
- Compute Engine
- Google Cloud
- Java
- Python
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.
- Google Cloud
- Compute Engine
- Go
- Python
- Node JS
- PHP
- Java
- JavaScript
Create a regional MIG with a target distribution shape
Create a regional managed instance group (MIG) with BALANCED target distribution shape.
- Google Cloud
- Compute Engine
- Terraform
Enable OS Login for a Cloud Project
Use this sample to enable OS Login to all instances in a project.
- Compute Engine
- Google Cloud
- Terraform
Create an image from a snapshot
This sample creates an image based on a snapshot.
- Compute Engine
- Google Cloud
- Python
- Go
- Java
Delete a VM instance
Delete an instance based on the project, zone, and instance name that you provide.
- Google Cloud
- Compute Engine
- PHP
- C#
- JavaScript
- Java
- Python
- Ruby
- Node JS
- Go
Create a VM in a subnet
Create a VM in a custom VPC network and a specific subnet.
- Compute Engine
- Google Cloud
- Go
- Node JS
- Java
- Terraform
- Python
- JavaScript
Set stateful metadata on VM instance creation in a MIG
In a managed instance group (MIG), create a managed instance with a custom name and set stateful metadata on that VM.
- Google Cloud
- Compute Engine
- Terraform
Create a route to Windows activation host
If you have Windows instances without external IP addresses, you must also enable Private Google Access so that instances with only internal IP addresses can send traffic to the external IP address.
- Compute Engine
- Google Cloud
- Python
- Java
- Go
Get information about an instance template
Get the basic information and instance configuration defined in an instance template.
- Compute Engine
- Google Cloud
- Go
- Python
- Java
- Node JS
- JavaScript
Create a VM instance
Create a VM instance in the specified project and zone.
- Compute Engine
- Google Cloud
- C#
- Node JS
- JavaScript
- Terraform
- Go
- PHP
- Java
- Python
- Ruby
Create a VM with shared-core machine type
This sample demonstrates how to create a new VM instance with a custom machine type using shared CPUs.
- Google Cloud
- Compute Engine
- Python
- Java
- Go
Create a KMS encrypted disk
This sample creates a new disk encrypted with a customer-supplied encryption key from Cloud KMS.
- Google Cloud
- Compute Engine
- Go
- Java
- Python
Get the newest image from a given family
This sample retrieves the newest image that is part of a given family in a project.
- Compute Engine
- Google Cloud
- Go
- Java
- Python
Enable deletion protection for a new VM
Create a VM in the specified project and zone with deletion protection enabled.
- Google Cloud
- Compute Engine
- Go
- Python
- Java
- Node JS
- JavaScript
Set and retrieve Compute Engine usage export bucket
This sample shows you how to set and retrieve the Compute Engine usage export bucket for a project. The usage export bucket is a Google Cloud Storage bucket where Compute Engine usage reports are stored. The usage reports can be used for billing and cost management purposes.
- Google Cloud
- Compute Engine
- Python
Create an instance with a shutdown script for Apache server
Create an instance with a shutdown script for Apache server
- Google Cloud
- Compute Engine
- Terraform
Get the hostname of a Compute Engine instance
This sample retrieves the hostname of a given Compute Engine instance.
- Google Cloud
- Compute Engine
- Java
- JavaScript
- Python
- Node JS
- Go
Create a Compute Engine instance static IP address
Create a Compute Engine instance with a fixed IP address in a specified region.
- Google Cloud
- Compute Engine
- Terraform
Start a VM instance
Restart a stopped VM instance.
- Compute Engine
- Google Cloud
- PHP
- Node JS
- Java
- Python
- Go
- JavaScript
Delete an instance template
Delete an instance template from a specified project.
- Google Cloud
- Compute Engine
- Node JS
- Python
- JavaScript
- Java
- Go
Encrypt customer private key with Google public key
This sample demonstrates how to encrypt a customer private key with the Google public key, ensuring that only Google can decrypt it.
- Compute Engine
- Google Cloud
- Python
Create a local reservation
Create a single-project reservation for N2 machine types
- Compute Engine
- Google Cloud
- Terraform
Change the deletion protection setting for an existing VM
Enable deletion protection for an existing VM without stopping the VM.
- Google Cloud
- Compute Engine
- JavaScript
- Go
- Java
- Node JS
- Python
Create a MIG with stateful disks
Create a managed instance group (MIG) that preserves disks for all VMs in the group.
- Google Cloud
- Compute Engine
- Terraform
Start a VM with encrypted disks
Provide an encryption key and start a VM instance that has encrypted disks.
- Compute Engine
- Google Cloud
- Python
- Node JS
- Go
- JavaScript
- PHP
- Java
Add OS login admin permissions for Compute Engine Service Agent
Use this example to add OS Login admin role to the Compute Engine service account.
- Google Cloud
- Compute Engine
- Terraform
Configure static IP addresses on VM creation in a MIG
Configure static IP addresses on VM creation in a managed instance group (MIG).
- Google Cloud
- Compute Engine
- Terraform
Create a Windows Server image
Create a Windows Server image from the specified source disk.
- Google Cloud
- Compute Engine
- Node JS
- JavaScript
- Python
- Java
- Go
Create a VM with a local SSD
Attach a local SSD to a VM during VM creation.
- Google Cloud
- Compute Engine
- Node JS
- Go
- Terraform
- Java
- Python
- JavaScript
Configure stateful disks individually for VMs in a MIG
This sample configures stateful disks individually for VMs in a managed instance group (MIG) by configuring the per-instance configuration of that VM.
- Google Cloud
- Compute Engine
- Terraform
Wait for extended operations
This sample demonstrates how to wait for an extended (long-running) operation to complete.
- Compute Engine
- Google Cloud
- Python
Create a regional MIG
Create a MIG with VMs evenly distributed across multiple zones in a region.
- Compute Engine
- Google Cloud
- Terraform
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.
- Compute Engine
- Google Cloud
- JavaScript
- Node JS
- Java
- Go
- Python
Create a firewall rule
Create a firewall rule that allows incoming HTTP and HTTPS access from the entire internet.
- Compute Engine
- Google Cloud
- Python
- Java
- Go
- Terraform
- Ruby
- C#
- Node JS
- PHP
- JavaScript
Resize a persistent disk
This sample resizes a persistent disk to a specified size in GB. After you resize the disk, you must also resize the file system so that the operating system can access the additional space.
- Google Cloud
- Compute Engine
- Go
- Java
- Python
List all VM instances
List all VM instances in a project, across all zones.
- Google Cloud
- Compute Engine
- Ruby
- PHP
- Python
- Node JS
- Java
- Go
- JavaScript
- C#
Create a custom VPC Subnet
Create a subnet with a custom IP address range.
- Google Cloud
- Compute Engine
- Terraform
Add SSH key to instance metadata
Add a public SSH key to the instance metadata during VM creation.
- Google Cloud
- Compute Engine
- Terraform
Create a disk from an image
This sample creates a new disk in a project in a given zone using an image as the base.
- Google Cloud
- Compute Engine
- Go
- Python
- Java
Resize a persistent disk
This sample resizes a persistent disk to a specified size in GB. After you resize the disk, you must also resize the file system so that the operating system can access the additional space.
- Compute Engine
- Google Cloud
- Java
- Python
Run a VM as a service account
Assign a service account for a VM, add access scopes, and set up the VM to run as a service account.
- Google Cloud
- Compute Engine
- Terraform
Create a disk snapshot
Create a zonal or a regional snapshot of a disk in the specified Cloud Storage location.
- Google Cloud
- Compute Engine
- Terraform
- Java
- Python
- Go
- Node JS
- JavaScript
Configure autoscaling in a managed instance group (MIG)
Configure autoscaling based on CPU utilization in a zonal MIG.
- Compute Engine
- Google Cloud
- Terraform
Delete a disk image
Delete a disk image from the specified project.
- Compute Engine
- Google Cloud
- Go
- Java
- Python
Check operation status
Request to wait until an operation completes and get the operation status before continuing code execution.
- Google Cloud
- Compute Engine
- C#
- Go
- Ruby
- PHP
- Python
- JavaScript
- Java
- Node JS
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.
- Google Cloud
- Compute Engine
- Node JS
- Python
- Go
- JavaScript
- Java
Create an image from another image
This sample creates a copy of another image.
- Compute Engine
- Google Cloud
- Java
- Python
- Go
Create Spot Instance
Create a spot instance using scheduling block
- Compute Engine
- Google Cloud
- Terraform
Create a VM from existing disk
Create a disk from a snapshot and attach the disk to a new VM.
- Google Cloud
- Compute Engine
- Python
- Node JS
- Java
- Go
- JavaScript
List snapshots
This sample lists all snapshots in a project. You can filter the results by specifying a filter expression.
- Compute Engine
- Google Cloud
- Go
- Python
- Java
Create a Debian 10 VM instance from a public image
This sample creates a new VM instance with Debian 10 operating system using an image from the public images provided by Google Cloud.
- Google Cloud
- Compute Engine
- Go
- Python
- Java
Create VMs with custom machine types
This sample demonstrates how to create custom machine types for use with Compute Engine instances. Custom machine types allow you to specify the exact number of cores and memory for your instance, providing greater flexibility and control over your compute resources.
- Compute Engine
- Google Cloud
- JavaScript
- Node JS
- Go
- Python
- Java
List firewall rules in a project
Get a list of all firewall rules defined in your project.
- Compute Engine
- Google Cloud
- PHP
- Java
- C#
- Python
- JavaScript
- Node JS
- Ruby
- Go
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.
- Google Cloud
- Compute Engine
- Node JS
- JavaScript
- Java
- Go
- Python
Create a VM with a custom hostname
Create a VM with a custom hostname that conforms to the RFC 1035 requirements for valid hostnames.
- Compute Engine
- Google Cloud
- Python
- JavaScript
- Node JS
- Java
- Terraform
- Go
Delete a regional disk
This sample deletes a regional disk from a project.
- Compute Engine
- Google Cloud
- Python
- Go
- Java
List VMs in a zone
Get a list of VMs in a specific zone in a project.
- Google Cloud
- Compute Engine
- Go
- Node JS
- Ruby
- C#
- Python
- JavaScript
- Java
- PHP
Delete snapshots by filter
This sample deletes all snapshots in a project that meet the filter criteria.
- Compute Engine
- Google Cloud
- Java
- Go
- Python
Create a firewall rule for Windows activation host
This sample creates an egress firewall rule with the highest priority for host kms.windows.googlecloud.com for Windows activation.
- Compute Engine
- Google Cloud
- Java
- Go
- Python
Add SSH key to project metadata
Add a public SSH key to the project metadata during VM creation.
- Google Cloud
- Compute Engine
- Terraform
Create a disk from a snapshot
Create a zonal disk from a snapshot.
- Compute Engine
- Google Cloud
- Go
- Java
- Node JS
- Python
- JavaScript
Add a public SSH key to your Google account
Associate a public SSH key with your Google account for VMs that use OS Login.
- Compute Engine
- Google Cloud
- Terraform
Create an instance template for a MIG with preemptible VMs
Create an instance template which you can use to create a managed instance group (MIG) with preemptible VMs.
- Google Cloud
- Compute Engine
- Terraform
List instance templates in a project
Get a list of instance templates defined in a project.
- Google Cloud
- Compute Engine
- Java
- Python
- Node JS
- Go
- JavaScript
Configure an autohealing policy in a MIG
This sample configures an autohealing policy to apply health check in a managed instance group (MIG).
- Google Cloud
- Compute Engine
- Terraform
Create a regional MIG with proactive instance redistribution disabled
Create a regional managed instance group (MIG) with proactive instance redistribution disabled.
- Compute Engine
- Google Cloud
- Terraform
Create a Compute Engine instance with OS Login
Create a Compute Engine instance with OS Login enabled
- Compute Engine
- Google Cloud
- Terraform
Create a VM instance from an instance template
Use an instance template to create a VM instance in the specified project and zone.
- Compute Engine
- Google Cloud
- JavaScript
- Java
- Go
- Node JS
- Python
Create a disk image
Create a disk image from a source disk and store it in a Cloud Storage location.
- Compute Engine
- Google Cloud
- Python
- Go
- Java
List all non-deprecated images in a project
Get a list of all non-deprecated images available in a specified project.
- Google Cloud
- Compute Engine
- Python
- Go
- Ruby
- Java
- PHP
- C#
- Node JS
- JavaScript
Create a VM with a custom machine type
Create a VM with the specified number of vCPUs and the specified amount of memory.
- Google Cloud
- Compute Engine
- JavaScript
- Python
- Node JS
- Go
- Java
Create a VM from a custom image
Use a custom image as the boot disk and create a VM.
- Compute Engine
- Google Cloud
- Go
- Python
- JavaScript
- Java
- Node JS
Check if a Compute Engine instance is preemptible
This sample checks if a given Compute Engine instance is preemptible or not.
- Compute Engine
- Google Cloud
- Node JS
- Python
- JavaScript
- Java
- Go
Create a VM from a non-boot disk snapshot
This sample creates a VM instance with Debian 10 operating system and a data disk created from a snapshot. The snapshot link must be in the form of projects/{project_name}/global/snapshots/{snapshot_name}.
- Compute Engine
- Google Cloud
- Go
- Python
- Java
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.
- Google Cloud
- Compute Engine
- Go
- Java
- Python
Delete a disk
Delete a persistent disk from the specified zone.
- Compute Engine
- Google Cloud
- JavaScript
- Go
- Python
- Node JS
- C++
- Java
Create a persistent disk with Debian image on SSD
Create a persistent disk with Debian image on SSD
- Compute Engine
- Google Cloud
- Terraform
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.
- Google Cloud
- Compute Engine
- JavaScript
- Python
- Java
- Go
- Node JS
Create a Spot VM instance with Debian 11
This sample creates a new Spot VM instance with Debian 11 operating system. Spot VMs are a type of virtual machine that can be preempted at any time, but offer a significant discount compared to regular VMs. This sample demonstrates how to create a Spot VM instance using the Compute Engine API.
- Google Cloud
- Compute Engine
- Java
- Python
- Go
Suspend a running VM
Suspend a running VM in the specified project and zone.
- Compute Engine
- Google Cloud
- Python
- PHP
- JavaScript
- Node JS
- Java
- Go
Resume a suspended VM
Resume a suspended VM in the specified project and zone.
- Compute Engine
- Google Cloud
- PHP
- Go
- JavaScript
- Node JS
- Java
- Python
Get the deletion protection status of a VM
Determine whether deletion protection is enabled for a specified VM.
- Compute Engine
- Google Cloud
- Python
- Java
- Node JS
- Go
- JavaScript
Increase a managed instance group's (MIG's) size limit
The sample creates a zonal MIG and increases the MIG's size limit from 1,000 VMs (default) to 2,000 VMs. The size limit increases when you enable pagination by setting the list_managed_instances_results argument to PAGINATED.
- Compute Engine
- Google Cloud
- Terraform
Create a route in a network
This sample creates a new route in a selected network by providing a destination and next hop name.
- Compute Engine
- Google Cloud
- Go
- Python
- Java
Create a MIG with stateful disks that are deleted when its VM gets permanently deleted
This sample creates a managed instance group (MIG) with stateful disks and sets the disks to delete when its VM is permanently deleted from the MIG.
- Google Cloud
- Compute Engine
- Terraform
Stop a VM instance
Stop a running VM instance.
- Google Cloud
- Compute Engine
- JavaScript
- Python
- Node JS
- Java
- Go
- PHP
Get an image
This sample retrieves detailed information about a single image from a project.
- Google Cloud
- Compute Engine
- Java
- Python
- Go
Retrieves the list of Route resources available in the specified project
This sample lists all network routes in a given project. A route defines a path from VM instances in the VPC network to a specific destination. This destination can be inside or outside the VPC network.
- Compute Engine
- Google Cloud
- Go
- Python
- Java
List disks in a zone
This sample lists all the disks in a given zone.
- Google Cloud
- Compute Engine
- Python
- Go
- Java