Skip to content

Instantly share code, notes, and snippets.

@mesmacosta
Last active July 30, 2020 07:55
Show Gist options
  • Save mesmacosta/5e6a2bf53395caa39c41cbf5be09dffa to your computer and use it in GitHub Desktop.
Save mesmacosta/5e6a2bf53395caa39c41cbf5be09dffa to your computer and use it in GitHub Desktop.
Example showing how to create a Compute Engine instance using Cloud Functions
const Buffer = require('safe-buffer').Buffer;
const Compute = require('@google-cloud/compute');
const compute = new Compute();
const zone = 'us-central1-a';
// We are going to add our vm configuration in the next step
// const vmConfig = ...
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