| 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') { |
| 93 | [operation] = await operationsClient.wait({ |
| 94 | operation: operation.name, |
| 95 | project: projectId, |
| 96 | zone: operation.zone.split('/').pop(), |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | console.log('Instance created.'); |
| 101 | } |
| 102 | |
| 103 | createInstance(); |
| 104 | // [END compute_instances_create] |