Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mesmacosta/cd1f970c8db3550f3c13086f1dfe68fd to your computer and use it in GitHub Desktop.

Select an option

Save mesmacosta/cd1f970c8db3550f3c13086f1dfe68fd to your computer and use it in GitHub Desktop.
create_compute_vm_with_container_image
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 = 'my-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: 'gce-container-declaration',
value: `spec:\n containers:\n - name: instance-1\n image: "gcr.io/${projectId}/alpha_vantage_bdd:latest"\n stdin: false\n tty: false\n restartPolicy: Always\n\n# This container declaration format is not public API and may change without notice. Please\n# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine.`
},
{
key: 'google-logging-enabled',
value: 'true'
}
]
},
tags: {
items: []
},
disks: [
{
kind: 'compute#attachedDisk',
type: 'PERSISTENT',
boot: true,
mode: 'READ_WRITE',
autoDelete: true,
deviceName: 'instance-1',
initializeParams: {
sourceImage: 'projects/cos-cloud/global/images/cos-stable-76-12239-60-0',
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: '',
scheduling: {
preemptible: false,
onHostMaintenance: 'MIGRATE',
automaticRestart: true,
nodeAffinities: []
},
deletionProtection: false,
reservationAffinity: {
consumeReservationType: 'ANY_RESERVATION'
},
serviceAccounts: [
{
email: '520162182440-compute@developer.gserviceaccount.com',
scopes: [
'https://www.googleapis.com/auth/devstorage.read_only',
'https://www.googleapis.com/auth/logging.write',
'https://www.googleapis.com/auth/monitoring.write',
'https://www.googleapis.com/auth/servicecontrol',
'https://www.googleapis.com/auth/service.management.readonly',
'https://www.googleapis.com/auth/trace.append'
]
}
]
}
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