Skip to content

Instantly share code, notes, and snippets.

@danthegoodman1
Last active October 2, 2019 18:25
Show Gist options
  • Save danthegoodman1/bd9eb476d67816d5c2e72a225c553183 to your computer and use it in GitHub Desktop.
Save danthegoodman1/bd9eb476d67816d5c2e72a225c553183 to your computer and use it in GitHub Desktop.
Project 1 code snippets
const Compute = require('@google-cloud/compute')
const compute = new Compute({
projectId: '[PROJECT NAME]',
keyFilename: './keyFile.json'
})
const zone = compute.zone('us-east1-b')
const config = {
http: true,
https: true,
machineType: 'n1-standard-1',
tags: [
'http-server',
'https-server'
],
// Here is that code I gave you before:
'disks': [
{
'kind': 'compute#attachedDisk',
'type': 'PERSISTENT',
'boot': true,
'mode': 'READ_WRITE',
'autoDelete': true,
'deviceName': [VM NAME],
'initializeParams': {
'sourceImage': 'projects/[PROJECT NAME]/global/images/[IMAGE NAME]',
'diskType': 'projects/[PROJECT NAME]/zones/us-east1-b/diskTypes/pd-standard',
'diskSizeGb': 30
}
}
],
}
zone.createVM(name, config)
.then(() => {
console.log('created')
return zone.vm(name).getMetadata()
})
.then((data) => {
console.log('get meta')
const pubIP = data[0].networkInterfaces[0].accessConfigs[0].natIP // Public IP to access VM
console.log(`Launching VM with ip: ${pubIP}, wait 20 seconds before sshing...`)
const setMeta = {
'ssh-keys': 'dan:ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC3ICEOuo94gsWGokqS1kwdmZkIkpNPwcTILyawNb5HtWamfmrd+AFQwqrE60U7NOSWiP6ZtFD5IoxN9dABK/fC9CkmJlPdDJswGMe5JWK/vg7EPaOBYGKddD0E2eoANfYON2caO3l7X6FX5YZ0yVUqVbPcMPu+fb3Q3LVizJ6cT9CXgobVPK5mC2kom/V3MtNGt0TjrVIYjs1N9PpGVqEsu8aoR7iRYENfxyjjco0+jMdtZAu+TJ66LvQ/nVM0Gclpe1tVTGx0fwv3MokNmvEVnewjVys7WYpyvgnxqtvyzFtKmM96LqyQuvrSqrldFZFyOK6at/aFj+zVXO5FT+kWSfek9tfU2j9mRKpGqu/eX00r3OemBUGLY1Sr30/Ni3UAVyPIXZ9woCHeTFY2NR12njkJM5A2bTZxBWrL+i2ll4zEPm5DbLh/oaQTPF6RAyv03l3oOHjRLqh+u2y+ADRi9Mt1BeaI9eaNOqlQUuMm+e9IMmYThpq/rlPPvdtuRTOy/GZE8sExhzrSmo8rCPCKhM1rTTdXXWog824eG4WzUISj2/KyoeHXXz/Q2CSma7IOfFwDBM3GD963VldtzovnaQ+ZyNShidcP4ybgZDk35O6PtCqMJWB1S1uuwT95Gioa/aTotyuHGm4tzKaYiQe7/fm+gu/bsMZ9rD+WeD+KGQ== dan\n', // Example using my ssh public key
}
return zone.vm(name).setMetadata(setMeta) // Set the metadata (put your ssh key in)
})
.then(() => {
console.log('Launched VM!')
})
.catch((err) => {
console.log(err)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment