使用 Rest api 创建 N 个 GCP 计算实例

Create N number of GCP compute instances using Rest api

如何使用 rest 在 GCP 中创建 n 个实例 api。
在 AWS java SDK 中,有一个方法 withMaxCount 我们可以在其中指定 ec2 实例的数量。
同样有任何用于 GCP 计算的东西。

您可以在循环中使用 REST API 来创建实例。
示例请求将如下所示:

{
  "kind": "compute#instance",
  "name": "INSTANCE-NAME",
  "zone": "projects/PROJECT-NAME/zones/us-central1-a",
  "machineType": "projects/PROJECT-NAME/zones/us-central1-a/machineTypes/e2-medium",
  "displayDevice": {
    "enableDisplay": false
  },
  "metadata": {
    "kind": "compute#metadata",
    "items": []
  },
  "tags": {
    "items": []
  },
  "disks": [
    {
      "kind": "compute#attachedDisk",
      "type": "PERSISTENT",
      "boot": true,
      "mode": "READ_WRITE",
      "autoDelete": true,
      "deviceName": "INSTANCE-NAME",
      "initializeParams": {
        "sourceImage": "projects/debian-cloud/global/images/debian-10-buster-v20210122",
        "diskType": "projects/PROJECT-NAME/zones/us-central1-a/diskTypes/pd-standard",
        "diskSizeGb": "10",
        "labels": {}
      },
      "diskEncryptionKey": {}
    }
  ],
  "canIpForward": false,
  "networkInterfaces": [
    {
      "kind": "compute#networkInterface",
      "subnetwork": "regions/us-central1/subnetworks/default",
      "accessConfigs": [
        {
          "kind": "compute#accessConfig",
          "name": "External NAT",
          "type": "ONE_TO_ONE_NAT",
          "networkTier": "PREMIUM"
        }
      ],
      "aliasIpRanges": []
    }
  ],
  "description": "",
  "labels": {},
  "scheduling": {
    "preemptible": false,
    "onHostMaintenance": "MIGRATE",
    "automaticRestart": true,
    "nodeAffinities": []
  },
  "deletionProtection": false,
  "reservationAffinity": {
    "consumeReservationType": "ANY_RESERVATION"
  },
  "serviceAccounts": [
    {
      "email": "111111111111-compute@developer.gserviceaccount.com",
      "scopes": [
        "https://www.googleapis.com/auth/devstorage.read_only",
        "https://www.googleapis.com/auth/logging.write",
        "https://www.googleapis.com/auth/monitoring.write",
        "https://www.googleapis.com/auth/servicecontrol",
        "https://www.googleapis.com/auth/service.management.readonly",
        "https://www.googleapis.com/auth/trace.append"
      ]
    }
  ],
  "shieldedInstanceConfig": {
    "enableSecureBoot": false,
    "enableVtpm": true,
    "enableIntegrityMonitoring": true
  },
  "confidentialInstanceConfig": {
    "enableConfidentialCompute": false
  }
}

替换成你自己的项目名和服务账号,可以测试一下here