【问题标题】:Turn on/off GCP Compute Engine Instance via HTTP request通过 HTTP 请求打开/关闭 GCP Compute Engine 实例
【发布时间】:2020-09-01 15:37:32
【问题描述】:
如何使用外部 HTTP 请求(例如,通过 Slack)打开/关闭 Google Compute Engine VM 实例(或者检查其状态)?
这在 AWS 上是可能的,甚至还有针对 AWS 实例的 Slack 集成,但是 Google Cloud Platform 呢?我想这涉及到编写和部署一个谷歌云函数,在 GCP nodeJS 示例repo 中有 startInstance/stopInstance 函数。
注意:这不适用于自动调度程序,大多数有关启动 VM 的文档/文章都使用 Google Cloud Scheduler a la this。但是我不确定/不认为 Pub/Sub 可以在此设置中与 Slack 等外部应用程序一起使用。
【问题讨论】:
标签:
google-cloud-platform
google-cloud-functions
google-compute-engine
【解决方案1】:
这篇文章还不是答案。我会编辑这个。我写在这里是因为我没有足够的声誉来添加评论。为此事道歉。 :/
提到的 Computed Engine Management API 不能成为这种情况的解决方案,因为您无法在 Slack 等应用中使用所需的身份验证。您需要使用密钥进行身份验证。
我认为使用 Cloud Functions 启动/停止实例(使用问题中提到的 repo 创建)是一种适用的方式。但默认情况下它们是公共端点,我们必须保护它们。我发现了两种保护 Cloud Function 的方法:
在函数中实施您的身份验证检查。这是一个解决方案:https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/scheduleinstance/index.js
将端点放在云函数前面,以便能够限制访问并使用 API 密钥(特定于您的项目)进行身份验证。 https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-functions。
第二个选项对我来说看起来更好。我们可以使用这个tutorial 来保护云函数。唯一的额外步骤是修改教程中的 openapi-functions.yaml 文件(以启用 API Key 安全性)。下面的内容就可以了。
swagger: '2.0'
info:
title: Cloud Endpoints + GCF
description: Sample API on Cloud Endpoints with a Google Cloud Functions backend
version: 1.0.0
host: [HOST]
schemes:
- https
security: []
paths:
/hello:
post:
summary: Greet a user
operationId: hello
security:
- api_key: []
x-google-backend:
address: https://[REGION-FUNCTIONS_PROJECT_ID].cloudfunctions.net/[FUNCTION_NAME]
protocol: h2
responses:
'200':
description: A successful response
schema:
type: string
# [START securityDef]
securityDefinitions:
# This section configures basic authentication with an API key.
api_key:
type: "apiKey"
name: "key"
in: "query"
# [END securityDef]
我将使用 Slack 集成实现整个系统并更新这篇文章。如果这篇文章让您感到厌烦,再次抱歉,因为它还不是一个合法的答案。