Search code examples
google-cloud-platformgoogle-cloud-functionsgoogle-compute-enginegoogle-cloud-monitoring

Compute Engine VM Creation Notification


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.


Solution

  • 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.