Last active
August 15, 2019 15:14
-
-
Save mesmacosta/f27447cca62a22c6cfbdbae932908e55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Buffer = require('safe-buffer').Buffer; | |
| const Compute = require('@google-cloud/compute'); | |
| const compute = new Compute(); | |
| // Change this const value to your project | |
| const projectId = 'your-project'; | |
| const zone = 'us-central1-a'; | |
| const vmConfig = { | |
| kind: 'compute#instance', | |
| zone: `projects/${projectId}/zones/${zone}`, | |
| machineType: `projects/${projectId}/zones/${zone}/machineTypes/f1-micro`, | |
| displayDevice: { | |
| enableDisplay: false | |
| }, | |
| metadata: { | |
| kind: 'compute#metadata', | |
| items: [ | |
| { | |
| key: 'startup-script', | |
| value: 'gcloud logging write batch-execution \'Hello world from $(hostname).\'\ngcp_zone=$(curl -H Metadata-Flavor:Google http://metadata.google.internal/computeMetadata/v1/instance/zone -s | cut -d/ -f4)\ngcloud compute instances delete $(hostname) --zone ${gcp_zone}' | |
| } | |
| ] | |
| }, | |
| tags: { | |
| items: [] | |
| }, | |
| disks: [ | |
| { | |
| kind: 'compute#attachedDisk', | |
| type: 'PERSISTENT', | |
| boot: true, | |
| mode: 'READ_WRITE', | |
| autoDelete: true, | |
| deviceName: 'instance-1', | |
| initializeParams: { | |
| sourceImage: `projects/debian-cloud/global/images/debian-9-stretch-v20190729`, | |
| diskType: `projects/${projectId}/zones/${zone}/diskTypes/pd-standard`, | |
| diskSizeGb: '10' | |
| }, | |
| diskEncryptionKey: {} | |
| } | |
| ], | |
| canIpForward: false, | |
| networkInterfaces: [ | |
| { | |
| kind: 'compute#networkInterface', | |
| subnetwork: `projects/${projectId}/regions/us-central1/subnetworks/default`, | |
| accessConfigs: [ | |
| { | |
| kind: 'compute#accessConfig', | |
| name: 'External NAT', | |
| type: 'ONE_TO_ONE_NAT', | |
| networkTier: 'PREMIUM' | |
| } | |
| ], | |
| aliasIpRanges: [] | |
| } | |
| ], | |
| description: '', | |
| labels: {}, | |
| scheduling: { | |
| preemptible: false, | |
| onHostMaintenance: 'MIGRATE', | |
| automaticRestart: true, | |
| nodeAffinities: [] | |
| }, | |
| deletionProtection: false, | |
| reservationAffinity: { | |
| consumeReservationType: 'ANY_RESERVATION' | |
| }, | |
| serviceAccounts: [ | |
| { | |
| email: `compute-execute-batch-job@${projectId}.iam.gserviceaccount.com`, | |
| scopes: [ | |
| 'https://www.googleapis.com/auth/cloud-platform' | |
| ] | |
| } | |
| ] | |
| } | |
| exports.createInstance = (event, context) => { | |
| const vmName = 'batch-job-executor' + Date.now(); | |
| try { | |
| compute.zone(zone) | |
| .createVM(vmName, vmConfig) | |
| .then(data => { | |
| // Operation pending. | |
| const vm = data[0]; | |
| const operation = data[1]; | |
| console.log(`VM being created: ${vm.id}`); | |
| console.log(`Operation info: ${operation.id}`); | |
| return operation.promise(); | |
| }) | |
| .then(() => { | |
| const message = 'VM created with success, Cloud Function finished execution.'; | |
| console.log(message); | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| }); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment