Start a Google Cloud VM instance through a website

Basically the title - I want to create a simple website with a button, which, when pressed, starts a google cloud vm instance. The goal is to not need any kind of authorization from the client, so logging in to a google account linked to the gcloud project is not going to work.

Something tells me this should be an easy job, but I could not find anything related to this online, and I'm lost in gclouds many options, so I don't know where to start.

Upvotes: 0

Views: 214

Answers (1)

DazWilkin
DazWilkin

Reputation: 40416

There are various ways to solve this problem but please be very careful about permitting (arbitrary) users of a website to click a button and start VMs (that you're going to be billed for). Each VM will cost you $$$.

Since you don't want the website (client) to authenticate, you'll have to delegate that authentication (because GCP requires authentication) to a proxy server|service. This proxy would provide some API endpoint, let's keep it simple and say it's /billmeforanothervm.

You could run this proxy on any of Google's compute services (Cloud Run|Functions, App Engine, Compute Engine etc.).

When it runs on any compute service on GCP, the proxy will itself be authenticated as a service account. You could empower this service account by granting it the IAM role to create VMs or better, create a new service account that's only used by the proxy and only then for creating VMs.

Then, depending on your programming language preference, the proxy would run Compute Engine's instances.insert method:

https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert#examples

The IAM permissions are documented above the example:

https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert#iam-permissions

Once working, every time the endpoint is invoked, or a user clicks the button on the website a new VM will be created. You'll then need additional functionality to make this VM accessible to your users.

You'll be paying for every VM that is created this way for the lifetime of the VM. And, since you don't mention authentication, anyone on the planet could click the button and bill you for creating VMs. Please be very careful with this approach.

Upvotes: 1

Related Questions