Create a VM instance from an instance template with overrides
Stay organized with collections
Save and categorize content based on your preferences.
Create a VM instance from an instance template, then override the disk and machine type options in the template.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
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.
import (
"context"
"fmt"
"io"
compute "cloud.google.com/go/compute/apiv1"
computepb "google.golang.org/genproto/googleapis/cloud/compute/v1"
"google.golang.org/protobuf/proto"
)
// createInstanceFromTemplate creates a Compute Engine VM instance from an instance template, but overrides the disk and machine type options in the template.
func createInstanceFromTemplateWithOverrides(w io.Writer, projectID, zone, instanceName, instanceTemplateName, machineType, newDiskSourceImage string) error {
// projectID := "your_project_id"
// zone := "europe-central2-b"
// instanceName := "your_instance_name"
// instanceTemplateName := "your_instance_template_name"
// machineType := "n1-standard-2"
// newDiskSourceImage := "projects/debian-cloud/global/images/family/debian-10"
ctx := context.Background()
instancesClient, err := compute.NewInstancesRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewInstancesRESTClient: %w", err)
}
defer instancesClient.Close()
intanceTemplatesClient, err := compute.NewInstanceTemplatesRESTClient(ctx)
if err != nil {
return fmt.Errorf("NewInstanceTemplatesRESTClient: %w", err)
}
defer instancesClient.Close()
// Retrieve an instance template by name.
reqGetTemplate := &computepb.GetInstanceTemplateRequest{
Project: projectID,
InstanceTemplate: instanceTemplateName,
}
instanceTemplate, err := intanceTemplatesClient.Get(ctx, reqGetTemplate)
if err != nil {
return fmt.Errorf("unable to get intance template: %w", err)
}
fmt.Printf("%s", "asdfadf")
for _, disk := range instanceTemplate.Properties.Disks {
diskType := disk.InitializeParams.GetDiskType()
if diskType != "" {
disk.InitializeParams.DiskType = proto.String(fmt.Sprintf(`zones/%s/diskTypes/%s`, zone, diskType))
}
}
reqInsertInstance := &computepb.InsertInstanceRequest{
Project: projectID,
Zone: zone,
InstanceResource: &computepb.Instance{
Name: proto.String(instanceName),
MachineType: proto.String(fmt.Sprintf(`zones/%s/machineTypes/%s`, zone, machineType)),
Disks: append(
// If you override a repeated field, all repeated values
// for that property are replaced with the
// corresponding values provided in the request.
// When adding a new disk to existing disks,
// insert all existing disks as well.
instanceTemplate.Properties.Disks,
&computepb.AttachedDisk{
InitializeParams: &computepb.AttachedDiskInitializeParams{
DiskSizeGb: proto.Int64(10),
SourceImage: &newDiskSourceImage,
},
AutoDelete: proto.Bool(true),
Boot: proto.Bool(false),
Type: proto.String(computepb.AttachedDisk_PERSISTENT.String()),
},
),
},
SourceInstanceTemplate: instanceTemplate.SelfLink,
}
op, err := instancesClient.Insert(ctx, reqInsertInstance)
if err != nil {
return fmt.Errorf("unable to create instance: %w", err)
}
if err = op.Wait(ctx); err != nil {
return fmt.Errorf("unable to wait for the operation: %w", err)
}
fmt.Fprintf(w, "Instance created\n")
return nil
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]