Disable public IP for Cloud SQL instance using Go SDK

I'm creating a new Cloud SQL instance using the Go SDK's Instances.Insert method. After setting DatabaseInstance.Settings.IpConfiguration.Ipv4Enabled to false, I'm still getting a public IP assigned to my instance. How can I disable the public IP?

0 4 337
4 REPLIES 4

To ensure that your Google Cloud SQL instance does not have a public IP address, you need to set DatabaseInstance.Settings.IpConfiguration.Ipv4Enabled to false. This action disables the assignment of a public IPv4 address. Additionally, to enforce the use of a private IP address, you should specify the Settings.IpConfiguration.PrivateNetwork with the resource link of your VPC network. This combination of settings will ensure that your Cloud SQL instance only uses a private IP and does not have a public IP assigned.

Here's how you can implement this in Go:

 
databaseInstance := &sqladmin.DatabaseInstance{
  // ... other settings ...
  Settings: &sqladmin.Settings{
    IpConfiguration: &sqladmin.IpConfiguration{
      Ipv4Enabled: false,
      PrivateNetwork: "projects/your-project/global/networks/your-vpc-network",
    },
  },
}

operation, err := sqladminService.Instances.Insert(projectId, databaseInstance).Do()
if err != nil {
  // handle error
}

// Assuming you have a function to wait for the operation to complete
err = waitForOperationCompletion(context.Background(), operation, waitTimeout)
if err != nil {
  // handle error
}

By setting Ipv4Enabled to false and providing a PrivateNetwork, your Cloud SQL instance will be configured to use only a private IP address, ensuring it does not have a public IP assigned. This setup is crucial for enhancing the security of your Cloud SQL instance by preventing direct access from the public internet.

Ok.Thank you staff google.now i need your team or staff google support me build and access ipv6 address and internet because all my partners haved ipv6.

Yes good,because you protect all your data and network

Hi. I tried to use same options, but my DB instance still allocate public IP.

import "google.golang.org/api/sqladmin/v1"
func CreateNewDB(instanceName string, engineVersion string, cfg *dsettings.DBSettings, networkName string) (string, error) {
	fmt.Printf("Network name will be assigned: %s\n", networkName)

	settings := sqladmin.Settings{
		Edition:                   "ENTERPRISE",
		Tier:                      cfg.Tier,
		DeletionProtectionEnabled: false,
		IpConfiguration: &sqladmin.IpConfiguration{
			Ipv4Enabled:    false,
			PrivateNetwork: "projects/dbtests/global/networks/qa-tests-qa-vpc-01",
		},
	}

	newDbInstance := sqladmin.DatabaseInstance{
		ConnectionName: instanceName,

		DatabaseVersion: engineVersion,
		InstanceType:    "CLOUD_SQL_INSTANCE",
		Name:            instanceName,
		Project:         cfg.ProjectID,
		Region:          cfg.Region,
		RootPassword:    "xxxxxxxxxxxxxxxxxx",
		Settings:        &settings,
	}
	somestuff, err := cfg.DBClient.Instances.Insert(cfg.ProjectID, &newDbInstance).Do()
	if err != nil {
		return "", err
	}