Can I change a Cloud SQL instance's number of CPUs and memory via API or programmatically?

Viewed 1484

I would like to edit a PostgreSQL instance's number of CPUs and Memory to custom values such as 2 vCPUs and 5 GB of memory each, via the API, but haven't found a way to do so.

The Instance settings page shows Cores and Memory as options, but when I try setting a simple JSON with the curl example given here,

{
  "settings": {
    "cores": 2,
    "memory": 5
  }
}

nothing happens.

I found a way to get existing settings, via curl -X GET -H "Authorization: Bearer "$(gcloud auth print-access-token) -H "Content-Type: application/json; charset=utf-8" "https://sqladmin.googleapis.com/v1/projects/MYPROJECT/instances/MYINSTANCE"

The returned JSON has dataDiskSizeGb, but nothing related to CPUs or memory that is obvious to me.

{
  "kind": "sql#instance",
  "state": "RUNNABLE",
  "databaseVersion": "POSTGRES_12",
  "settings": {
    "authorizedGaeApplications": [],
    "tier": "db-custom-1-3840",
    "kind": "sql#settings",
    "availabilityType": "ZONAL",
    "pricingPlan": "PER_USE",
    "replicationType": "SYNCHRONOUS",
    "activationPolicy": "ALWAYS",
    "ipConfiguration": {
      "privateNetwork": "projects/MYPROJECT/global/networks/default",
      "authorizedNetworks": [],
      "ipv4Enabled": true
    },
    "locationPreference": {
      "zone": "southamerica-east1-c",
      "kind": "sql#locationPreference"
    },
    "dataDiskType": "PD_SSD",
    "maintenanceWindow": {
      "kind": "sql#maintenanceWindow",
      "hour": 0,
      "day": 0
    },
    "backupConfiguration": {
      "startTime": "08:00",
      "kind": "sql#backupConfiguration",
      "location": "us",
      "backupRetentionSettings": {
        "retentionUnit": "COUNT",
        "retainedBackups": 7
      },
      "enabled": true,
      "replicationLogArchivingEnabled": false,
      "pointInTimeRecoveryEnabled": false,
      "transactionLogRetentionDays": 7
    },
    "settingsVersion": "4",
    "storageAutoResizeLimit": "0",
    "storageAutoResize": false,
    "dataDiskSizeGb": "10"
  },
  "etag": "079...039",
  "ipAddresses": [
    {
      "type": "PRIMARY",
      "ipAddress": "xx.xxx.x.xxx"
    },
    {
      "type": "OUTGOING",
      "ipAddress": "xx.xx.xxx.xx"
    },
    {
      "type": "PRIVATE",
      "ipAddress": "xx.xx.xxx.xx"
    }
  ],
  "serverCaCert": {
    "kind": "sql#sslCert",
    "certSerialNumber": "0",
    "cert": "-----BEGIN CERTIFICATE-----\nMII......c=\n-----END CERTIFICATE-----",
    "commonName": "C=US,O=Google\\, Inc,CN=Google Cloud SQL Server CA,dnQualifier=9f7...e0c",
    "sha1Fingerprint": "fff...8fb",
    "instance": "MYINSTANCE",
    "createTime": "2021-10-05T17:59:18.971Z",
    "expirationTime": "2031-10-03T18:00:18.971Z"
  },
  "instanceType": "CLOUD_SQL_INSTANCE",
  "project": "MYPROJECT",
  "serviceAccountEmailAddress": "abc...@gcp-sa-cloud-sql.iam.gserviceaccount.com",
  "backendType": "SECOND_GEN",
  "selfLink": "https://sqladmin.googleapis.com/v1/projects/MYPROJECT/instances/MYINSTANCE",
  "connectionName": "MYPROJECT:southamerica-east1:MYINSTANCE",
  "name": "MYINSTANCE",
  "region": "southamerica-east1",
  "gceZone": "southamerica-east1-c",
  "createTime": "2021-10-05T17:57:47.539Z"
}
1 Answers

To update the number of CPUs and Memory, on the "settings" REST reference there is a field "tier" that represents the CPU and Memory of the instance. In your example it is "db-custom-1-3840", the values represents the CPU and Memory (db-custom-[CPU]-[Memory]) this means that it has 1 CPU and 3840 memory. To change the machine to 2vCPU and and 5GB memory "tier" should have a value of "db-custom-2-5120".

For testing purposes I initially created a 4 vCPU with 26 GB memory. For reference see initial instance configuration:

enter image description here

To change CPU and memory see steps below:

request.json:

{
  "settings": {
    "tier":  "db-custom-2-5120"
  }
}

NOTE: The value of the memory should be a mutiple of 256MB thus the value 5120.

Curl command:

curl -X PATCH \
-H "Authorization: Bearer "$(gcloud auth print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/your-project-name/instances/your-instance-name"

This will return a long running operation:

enter image description here

When I run GET curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) https://sqladmin.googleapis.com/v1/projects/your-project-name/instances/your-instance-name the change is reflected.

See GET response snippet:

enter image description here

See new instance information at Cloud Console > SQL > Edit:

enter image description here

Related