运行 GCP 实例删除操作的脚本,列出使用 gcloud 对实例执行的操作
Run script on GCP instance delete action, list actions taken on instance using gcloud
寻找在 GCP 实例上执行关闭脚本的解决方案,仅当其删除操作时。此外,是否有区分使用 gcloud
对实例采取的操作?
关闭实例时执行关闭脚本。删除实例(当它是 运行 时)会触发关闭请求。因此,只有在无法删除实例时才执行关闭脚本。
在 API 的 documentation, where the shutdown-script is triggered when instance shuts down due to an instances.delete
request or an instances.stop
请求中也有描述。
编辑: 您可以使用命令 gcloud logging read
通过 CLI 查看在 VM 上执行的操作列表(即 start/stop/delete)。例如,此命令将在 table 上显示一天的日志,其中包含时间戳、资源名称和方法名称的列:
gcloud logging read \
"resource.type=gce_instance \
resource.labels.instance_id=INSTANCE_ID \
logName=projects/PROJECT_ID/logs/cloudaudit.googleapis.com%2Factivity" \
--format="table(timestamp,protoPayload.resourceName,protoPayload.methodName)" \
--project=PROJECT_ID
注意:将大写变量替换为您的 VM 的实例 ID 和您的项目 ID。
有关该命令如何工作的更多信息,请参阅以下资源:
寻找在 GCP 实例上执行关闭脚本的解决方案,仅当其删除操作时。此外,是否有区分使用 gcloud
对实例采取的操作?
关闭实例时执行关闭脚本。删除实例(当它是 运行 时)会触发关闭请求。因此,只有在无法删除实例时才执行关闭脚本。
在 API 的 documentation, where the shutdown-script is triggered when instance shuts down due to an instances.delete
request or an instances.stop
请求中也有描述。
编辑: 您可以使用命令 gcloud logging read
通过 CLI 查看在 VM 上执行的操作列表(即 start/stop/delete)。例如,此命令将在 table 上显示一天的日志,其中包含时间戳、资源名称和方法名称的列:
gcloud logging read \
"resource.type=gce_instance \
resource.labels.instance_id=INSTANCE_ID \
logName=projects/PROJECT_ID/logs/cloudaudit.googleapis.com%2Factivity" \
--format="table(timestamp,protoPayload.resourceName,protoPayload.methodName)" \
--project=PROJECT_ID
注意:将大写变量替换为您的 VM 的实例 ID 和您的项目 ID。
有关该命令如何工作的更多信息,请参阅以下资源: