Its not blank
Its not blank

Reputation: 3095

Compute Engine VM Creation Notification

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

But do storage like triggers available for Compute.

Also if there is any other solution.

Upvotes: 2

Views: 1186

Answers (3)

Its not blank
Its not blank

Reputation: 3095

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.

Upvotes: 0

guillaume blaquiere
guillaume blaquiere

Reputation: 75810

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!

Upvotes: 3

LundinCast
LundinCast

Reputation: 9810

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.

Upvotes: 2

Related Questions