1
votes

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.

1
The Google Cloud API does not have an equivalent to withMaxCount. The Google Cloud API is very easy to use. You can code a loop to create as many instances as your quota allows. The Google Cloud Console will show you the CLI command to create an instance. Use a shell script or you can add the command line option --log-http to can see the REST API to duplicate that command. Tip: look into using Terraform for deployments and skip using the API. - John Hanley
@JohnHanley, Thanks for the info - smootherbug
You are welcome. You have numerous tools, resources and options to build your infrastructure. - John Hanley
Do you want to create N GCE VM instances or GCE Managed Instance Group within REST API? - Serhii Rohoza
n GCE VM instances only - smootherbug

1 Answers

0
votes

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.