MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

compute/createInstance.js:35–105  ·  view source on GitHub ↗

* Sends an instance creation request to GCP and waits for it to complete. * * @param {string} projectId - ID or number of the project you want to use. * @param {string} zone - Name of the zone you want to check, for example: us-west3-b * @param {string} instanceName - Name of the new machine. *

(
  projectId,
  zone,
  instanceName,
  machineType = 'n1-standard-1',
  sourceImage = 'projects/debian-cloud/global/images/family/debian-11',
  networkName = 'global/networks/default'
)

Source from the content-addressed store, hash-verified

33 * For example: global/networks/default - if you want to use the default network.
34 */
35function main(
36 projectId,
37 zone,
38 instanceName,
39 machineType = 'n1-standard-1',
40 sourceImage = 'projects/debian-cloud/global/images/family/debian-11',
41 networkName = 'global/networks/default'
42) {
43 // [START compute_instances_create]
44 /**
45 * TODO(developer): Uncomment and replace these variables before running the sample.
46 */
47 // const projectId = 'YOUR_PROJECT_ID';
48 // const zone = 'europe-central2-b'
49 // const instanceName = 'YOUR_INSTANCE_NAME'
50 // const machineType = 'n1-standard-1';
51 // const sourceImage = 'projects/debian-cloud/global/images/family/debian-11';
52 // const networkName = 'global/networks/default';
53
54 const compute = require('@google-cloud/compute');
55
56 // Create a new instance with the values provided above in the specified project and zone.
57 async function createInstance() {
58 const instancesClient = new compute.InstancesClient();
59
60 console.log(`Creating the ${instanceName} instance in ${zone}...`);
61
62 const [response] = await instancesClient.insert({
63 instanceResource: {
64 name: instanceName,
65 disks: [
66 {
67 // Describe the size and source image of the boot disk to attach to the instance.
68 initializeParams: {
69 diskSizeGb: '10',
70 sourceImage,
71 },
72 autoDelete: true,
73 boot: true,
74 type: 'PERSISTENT',
75 },
76 ],
77 machineType: `zones/${zone}/machineTypes/${machineType}`,
78 networkInterfaces: [
79 {
80 // Use the network interface provided in the networkName argument.
81 name: networkName,
82 },
83 ],
84 },
85 project: projectId,
86 zone,
87 });
88 let operation = response.latestResponse;
89 const operationsClient = new compute.ZoneOperationsClient();
90
91 // Wait for the create operation to complete.
92 while (operation.status !== 'DONE') {

Callers 1

createInstance.jsFile · 0.70

Calls 1

createInstanceFunction · 0.70

Tested by

no test coverage detected