Reputation: 1603
I have a system that adds dynamically created instances with ephemeral IP addresses to GCP cloud DNS when they start. However, I need to remove them from the DNS when they shutdown, whether through GCP's own APIs, or through some other method.
The best I can figure now is:
Is there another way to do this?
Upvotes: 3
Views: 1934
Reputation: 1992
Have you considered adding a shutdown script to the instances? You can have that script POST
to any URL you want. No need to involve Stackdriver or Pub/Sub.
Upvotes: 0
Reputation: 1603
There is two ways that I know of to do this. Both still require the Stackdriver Logging service, but it shouldn't require logging extraneous data like uptime monitoring would do and it should capture every GCE instance shutdown that happens in a given GCP project, no matter how it happens. I personally prefer the second method (Pub/Sub).
The first method is through the Stackdriver service. The steps below assume you have already setup a Stackdriver account and connect it to the project you want to monitor.
On the far right of the text field for the filter, select the advanced mode and enter the following (where my-project
is the name of your project):
resource.type="gce_instance"
logName="projects/my-
project/logs/compute.googleapis.com%2Factivity_log"
(jsonPayload.event_subtype:"compute.instances.stop" OR
jsonPayload.event_subtype:"compute.instances.guestTerminate")
jsonPayload.event_type:"GCE_OPERATION_DONE"
At the top of the page click Create Metric
, then give it a name and a description, and submit your new metric
Log-based Metrics
page. Under User-defined Metrics
you should now see your newly created metricCreate alert from metric
. This should take you to the Stackdriver alert panel with most of the proper information filled inTHRESHOLD
value to 0
, and the FOR
value to most recent value
. Then click Save Condition
.The downside for the above method is that you need to pay for a Stackdriver premium account to use features like webhooks. It is up to you whether the pricing on that is worth it. On the flip side, the method below (using GCP Pub/Sub) costs nothing from Stackdriver and works with both exempt and non-exempt logs. You do, of course, still need to pay any Pub/Sub charges you incur from using the service.
The second method is similar, but doesn't necessarily require a premium Stackdriver account (you should be fine to do all this on the Free Stackdriver tier). In this method, you use Google Pub/Sub. Much of the relevant documentation can be found here.
On the far right of the text field for the filter, select the advanced mode and enter the following (where my-project
is the name of your project):
resource.type="gce_instance"
logName="projects/my-
project/logs/compute.googleapis.com%2Factivity_log"
(jsonPayload.event_subtype:"compute.instances.stop" OR
jsonPayload.event_subtype:"compute.instances.guestTerminate")
jsonPayload.event_type:"GCE_OPERATION_DONE"
At the top of the page click Create Export
, give it a sink name, choose Cloud Pub/Sub
as the sink service, then choose or create a Pub/Sub topic
I personally prefer the Pub/Sub method. It seems more fit for purpose and is (at least theoretically) less expensive than the Stackdriver method.
There may be a way to do this all programmatically through the GCP APIs, but I haven't dug in that deeply. If I find any documentation for that, then I will update this answer with that information as well.
Upvotes: 3