簡體   English   中英

通過 HTTP 請求打開/關閉 GCP Compute Engine 實例

[英]Turn on/off GCP Compute Engine Instance via HTTP request

如何使用外部 HTTP 請求(例如,通過 Slack)打開/關閉(或檢查狀態)Google Compute Engine VM 實例?

這在 AWS 上是可能的,甚至有針對 AWS 實例的 Slack 集成,但是對於 Google Cloud Platform 呢? 我想這涉及編寫和部署 Google Cloud Function,並且在 GCP nodeJS 示例repo中有 startInstance/stopInstance 函數。

注意:這不是自動調度程序的東西,大多數關於啟動 VM 的文檔/文章都使用 Google Cloud Scheduler a la this 但是我不確定/不認為 Pub/Sub 可以在此設置中與 Slack 等外部應用程序一起使用。

這篇文章還沒有答案。 我會編輯這個。 我在這里寫信是因為我沒有足夠的聲譽來添加評論。 為此表示歉意。 :/

提到的 Computed Engine Management API 不能解決這種情況,因為您不能在 Slack 等應用程序中使用必需的身份驗證。 您需要使用密鑰進行身份驗證。

我認為使用 Cloud Functions 啟動/停止實例(通過使用問題中提到的repo創建)是一種適用的方式。 但默認情況下它們是公共端點,我們必須保護它們。 我找到了兩種保護 Cloud Function 的方法:

  1. 在 function 中實施身份驗證檢查。這是一個解決方案: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/functions/scheduleinstance/index.js

  2. 將端點放在 Cloud Function 前面,以便能夠限制訪問並使用 API 密鑰(特定於您的項目)進行身份驗證。 https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-functions

第二種選擇對我來說更好看。 我們可以使用本教程保護雲 Function。 唯一的額外步驟是修改教程中的 openapi-functions.yaml 文件(以啟用 API 密鑰安全)。 下面的內容完成了這項工作。

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 集成實現整個系統並更新這篇文章。 如果這篇文章打擾了您,再次抱歉,因為它還不是一個合法的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM