Search code examples
javagoogle-cloud-platformgoogle-compute-enginegoogle-cloud-sdk

Create N number of GCP compute instances using Rest api


How to create n number of instances in GCP using rest api.
in AWS java SDK, there is a method withMaxCount where we specify number of ec2 instances.
Similarly is there anything for GCP compute.


Solution

  • You can use REST API in loop to create instances.
    Example request will look something like this:

    {
      "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": "[email protected]",
          "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
      }
    }
    

    Replace it with your own project name and service account, and you can test it here.