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

Function main

compute/createInstanceFromTemplate.js:27–72  ·  view source on GitHub ↗

* Creates a Compute Engine VM instance from an instance template. * * @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 instance. * @param

(projectId, zone, instanceName, instanceTemplateUrl)

Source from the content-addressed store, hash-verified

25 * - global/instanceTemplates/example-instance-template
26 */
27function main(projectId, zone, instanceName, instanceTemplateUrl) {
28 // [START compute_instances_create_from_template]
29 /**
30 * TODO(developer): Uncomment and replace these variables before running the sample.
31 */
32 // const projectId = 'YOUR_PROJECT_ID';
33 // const zone = 'europe-central2-b'
34 // const instanceName = 'YOUR_INSTANCE_NAME'
35 // const instanceTemplateUrl = 'YOUR_INSTANCE_TEMPLATE_URL'
36
37 const compute = require('@google-cloud/compute');
38
39 // Create a new instance from template in the specified project and zone.
40 async function createInstanceFromTemplate() {
41 const instancesClient = new compute.InstancesClient();
42
43 console.log(
44 `Creating the ${instanceName} instance in ${zone} from template ${instanceTemplateUrl}...`
45 );
46
47 const [response] = await instancesClient.insert({
48 project: projectId,
49 zone,
50 instanceResource: {
51 name: instanceName,
52 },
53 sourceInstanceTemplate: instanceTemplateUrl,
54 });
55 let operation = response.latestResponse;
56 const operationsClient = new compute.ZoneOperationsClient();
57
58 // Wait for the create operation to complete.
59 while (operation.status !== 'DONE') {
60 [operation] = await operationsClient.wait({
61 operation: operation.name,
62 project: projectId,
63 zone: operation.zone.split('/').pop(),
64 });
65 }
66
67 console.log('Instance created.');
68 }
69
70 createInstanceFromTemplate();
71 // [END compute_instances_create_from_template]
72}
73
74main(...process.argv.slice(2));

Callers 1

Calls 1

Tested by

no test coverage detected