How to update machine type of gcp instnace using nodejs client library

I have a node server which handles gcp instance operations. I am trying to update the machine type of a existing running instance. I don’t want to update any other properties like disk size or anything.

    
    const computeClient = new Compute.InstancesClient({
      projectId: "project",
      keyFilename: "keyfile",
    });
        let resource = {
          instance: "testinstance",
          instanceResource: {
            machineType : "zones/us-central1-a/machineTypes/e2-standard-4",
            name: "testinstance"
          },
          project: "project",
          zone : "us-central1-a"
        }
        const resp1 = await computeClient.update(resource);

When I try to run above code this error occurs

    Stacktrace:
    ==================== 
    Error: Invalid value for field 'resource.disks': ''. No disks are specified.
        at Function.parseHttpError (////node_modules/google-gax/build/src/googleError.js:49:37)
        at decodeResponse (///node_modules/google-gax/build/src/fallbackRest.js:72:49)
        at ////node_modules/google-gax/build/src/fallbackServiceStub.js:90:42
        at processTicksAndRejections (node:internal/process/task_queues:96:5)

    node version : v16.14.0 
    @google-cloud/compute version : 3.1.2

Any solution ? Any code sample to update machine type ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you only want to update your instance’s machine type you should use the setMachineType method directly which is meant for this specifically. See example below:

// Imports the Compute library
const {InstancesClient} = require('@google-cloud/compute').v1;

// Instantiates a client
const computeClient = new InstancesClient();
const instance = "instance-name";
const instancesSetMachineTypeRequestResource = {machineType: "zones/us-central1-a/machineTypes/n1-standard-1"}
const project = "project-id";
const zone = "us-central1-a";

async function callSetMachineType() {
  // Construct request
  const request = {
    instance,
    instancesSetMachineTypeRequestResource,
    project,
    zone,
  };

  // Run request
  const response = await computeClient.setMachineType(request);
  console.log(response);
}

callSetMachineType();

Note that machine type can only be changed on a TERMINATED instance as documented here. You’ll need to first ensure the instance is stopped or stop it in your code prior to updating machine type. More details on available methods here.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x