2

I wanted to get notified if/when there is/are any VM creation in my infra on GCP.

  • I see a google library that can give me list of VM.
  • I can create a function to use this code (probably)
  • Schedule the above function. And check for difference.

But do storage like triggers available for Compute.

Also if there is any other solution.

LundinCast
  • 7,759
  • 4
  • 26
  • 40
Its not blank
  • 2,727
  • 18
  • 35

3 Answers3

3

You have a third solution. You can use Cloud Run instead of Cloud Functions (the migration is very easy, let me know if you have issues).

With Cloud Run, you can use the trigger (eventArc feature), a new feature (still in preview) based on the auditLog logs. It's very similar to the first solution proposed by LundinCast, but it's automatically set up by Cloud Run Trigger feature.

So, deploy your service on Cloud Run. Then configure a trigger on v1.compute.instancs.insert API, select your region or make the trigger global and that's all!! Your service will be triggered when a new instance will be created.

enter image description here

As you can see in my screenshot, you will be asked to activate the auditLog to be able to use this feature. Because it's built-in, it's done automatically for you!

guillaume blaquiere
  • 33,758
  • 2
  • 11
  • 37
2

Using Logging sink and a PubSub-triggered Cloud Function

First, export the relevant logs to a PubSub topic of your choice by creating a Logging sink. Include the logs created automatically during VM creation with the following log filter:

resource.type="gce_instance"
protoPayload.methodName="beta.compute.instances.insert"
protoPayload.methodName="compute.instances.insert"

Next, create a Cloud Function that'll trigger every time a new log is set to the PubSub topic. You can process this new message as per your needs.

Note that with this option you'll have to handle to notification yourself (for example, by sending an email). It is useful though if you want to send different notification based on some condition or if you want to perform additional actions apart from the notification.

Using a log-based metric and a Cloud Monitoring alert

You can use a Log-based metric filtering logs for Compute Engine VM creation and set an alert on that metric to get notified.

First create a counter log-based metric with a log filter similar to the one in the previous method, which will report a data point to Cloud monitoring every time a new VM instance is created.

Then go to Cloud Monitoring and create an alert based on that metric that trigger every time a metric is reported.

This option is the easiest to set up and supports various notification channels out-of-the-box.

LundinCast
  • 7,759
  • 4
  • 26
  • 40
0

Going along with LudninCast's answer.

Cloud Run -- Would have used it if it had not been zone issue for me. Though I conclude this from POC I did

  1. Easy setup.
  2. Containerised Apps. Probably more code to maintain.
  3. Public URL for app.
  4. Out of box support for the requirements like mine.

Cloud Function --

  1. Sink setups for triggers can be time consuming for first timer
  2. Easy coding and maintainance.
Its not blank
  • 2,727
  • 18
  • 35
  • Regarding trigger set-up, there was an old alpha on the cloud-functions-beta-testers group for GCE triggers for functions. [This is the thread](https://groups.google.com/g/cloud-functions-beta-testers/c/GdUkshWlrtc/m/0u-vOwD0DQAJ). You should be able to join the group with the instructions on [the docs](https://cloud.google.com/functions/docs/getting-support#beta_tester_group). That thread is really old, so I'm not sure if the alpha is still ongoing, and it's an alpha (meaning it's not production-ready), but I think it's worth a try. – Jofre Jan 05 '21 at 07:34