DatabaseInstance

class DatabaseInstance : KotlinCustomResource

Creates a new Google SQL Database Instance. For more information, see the official documentation, or the JSON API.

NOTE on gcp.sql.DatabaseInstance: - Second-generation instances include a default 'root'@'%' user with no password. This user will be deleted by the provider on instance creation. You should use gcp.sql.User to define a custom user with a restricted host and strong password. Note: On newer versions of the provider, you must explicitly set deletion_protection=false (and run pulumi update to write the field to state) in order to destroy an instance. It is recommended to not set this field (or set it to true) until you're ready to destroy the instance and its databases.

Example Usage

SQL Second Generation Instance

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const main = new gcp.sql.DatabaseInstance("main", {
name: "main-instance",
databaseVersion: "POSTGRES_15",
region: "us-central1",
settings: {
tier: "db-f1-micro",
},
});
import pulumi
import pulumi_gcp as gcp
main = gcp.sql.DatabaseInstance("main",
name="main-instance",
database_version="POSTGRES_15",
region="us-central1",
settings={
"tier": "db-f1-micro",
})
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;
return await Deployment.RunAsync(() =>
{
var main = new Gcp.Sql.DatabaseInstance("main", new()
{
Name = "main-instance",
DatabaseVersion = "POSTGRES_15",
Region = "us-central1",
Settings = new Gcp.Sql.Inputs.DatabaseInstanceSettingsArgs
{
Tier = "db-f1-micro",
},
});
});
package main
import (
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/sql"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := sql.NewDatabaseInstance(ctx, "main", &sql.DatabaseInstanceArgs{
Name: pulumi.String("main-instance"),
DatabaseVersion: pulumi.String("POSTGRES_15"),
Region: pulumi.String("us-central1"),
Settings: &sql.DatabaseInstanceSettingsArgs{
Tier: pulumi.String("db-f1-micro"),
},
})
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.sql.DatabaseInstance;
import com.pulumi.gcp.sql.DatabaseInstanceArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var main = new DatabaseInstance("main", DatabaseInstanceArgs.builder()
.name("main-instance")
.databaseVersion("POSTGRES_15")
.region("us-central1")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-f1-micro")
.build())
.build());
}
}
resources:
main:
type: gcp:sql:DatabaseInstance
properties:
name: main-instance
databaseVersion: POSTGRES_15
region: us-central1
settings:
tier: db-f1-micro

Private IP Instance

NOTE: For private IP instance setup, note that the gcp.sql.DatabaseInstance does not actually interpolate values from gcp.servicenetworking.Connection. You must explicitly add a depends_onreference as shown below.

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
import * as random from "@pulumi/random";
const privateNetwork = new gcp.compute.Network("private_network", {name: "private-network"});
const privateIpAddress = new gcp.compute.GlobalAddress("private_ip_address", {
name: "private-ip-address",
purpose: "VPC_PEERING",
addressType: "INTERNAL",
prefixLength: 16,
network: privateNetwork.id,
});
const privateVpcConnection = new gcp.servicenetworking.Connection("private_vpc_connection", {
network: privateNetwork.id,
service: "servicenetworking.googleapis.com",
reservedPeeringRanges: [privateIpAddress.name],
});
const dbNameSuffix = new random.RandomId("db_name_suffix", {byteLength: 4});
const instance = new gcp.sql.DatabaseInstance("instance", {
name: pulumi.interpolate`private-instance-${dbNameSuffix.hex}`,
region: "us-central1",
databaseVersion: "MYSQL_5_7",
settings: {
tier: "db-f1-micro",
ipConfiguration: {
ipv4Enabled: false,
privateNetwork: privateNetwork.selfLink,
enablePrivatePathForGoogleCloudServices: true,
},
},
}, {
dependsOn: [privateVpcConnection],
});
import pulumi
import pulumi_gcp as gcp
import pulumi_random as random
private_network = gcp.compute.Network("private_network", name="private-network")
private_ip_address = gcp.compute.GlobalAddress("private_ip_address",
name="private-ip-address",
purpose="VPC_PEERING",
address_type="INTERNAL",
prefix_length=16,
network=private_network.id)
private_vpc_connection = gcp.servicenetworking.Connection("private_vpc_connection",
network=private_network.id,
service="servicenetworking.googleapis.com",
reserved_peering_ranges=[private_ip_address.name])
db_name_suffix = random.RandomId("db_name_suffix", byte_length=4)
instance = gcp.sql.DatabaseInstance("instance",
name=db_name_suffix.hex.apply(lambda hex: f"private-instance-{hex}"),
region="us-central1",
database_version="MYSQL_5_7",
settings={
"tier": "db-f1-micro",
"ip_configuration": {
"ipv4_enabled": False,
"private_network": private_network.self_link,
"enable_private_path_for_google_cloud_services": True,
},
},
opts = pulumi.ResourceOptions(depends_on=[private_vpc_connection]))
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;
using Random = Pulumi.Random;
return await Deployment.RunAsync(() =>
{
var privateNetwork = new Gcp.Compute.Network("private_network", new()
{
Name = "private-network",
});
var privateIpAddress = new Gcp.Compute.GlobalAddress("private_ip_address", new()
{
Name = "private-ip-address",
Purpose = "VPC_PEERING",
AddressType = "INTERNAL",
PrefixLength = 16,
Network = privateNetwork.Id,
});
var privateVpcConnection = new Gcp.ServiceNetworking.Connection("private_vpc_connection", new()
{
Network = privateNetwork.Id,
Service = "servicenetworking.googleapis.com",
ReservedPeeringRanges = new[]
{
privateIpAddress.Name,
},
});
var dbNameSuffix = new Random.RandomId("db_name_suffix", new()
{
ByteLength = 4,
});
var instance = new Gcp.Sql.DatabaseInstance("instance", new()
{
Name = dbNameSuffix.Hex.Apply(hex => $"private-instance-{hex}"),
Region = "us-central1",
DatabaseVersion = "MYSQL_5_7",
Settings = new Gcp.Sql.Inputs.DatabaseInstanceSettingsArgs
{
Tier = "db-f1-micro",
IpConfiguration = new Gcp.Sql.Inputs.DatabaseInstanceSettingsIpConfigurationArgs
{
Ipv4Enabled = false,
PrivateNetwork = privateNetwork.SelfLink,
EnablePrivatePathForGoogleCloudServices = true,
},
},
}, new CustomResourceOptions
{
DependsOn =
{
privateVpcConnection,
},
});
});
package main
import (
"fmt"
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/compute"
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/servicenetworking"
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/sql"
"github.com/pulumi/pulumi-random/sdk/v4/go/random"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
privateNetwork, err := compute.NewNetwork(ctx, "private_network", &compute.NetworkArgs{
Name: pulumi.String("private-network"),
})
if err != nil {
return err
}
privateIpAddress, err := compute.NewGlobalAddress(ctx, "private_ip_address", &compute.GlobalAddressArgs{
Name: pulumi.String("private-ip-address"),
Purpose: pulumi.String("VPC_PEERING"),
AddressType: pulumi.String("INTERNAL"),
PrefixLength: pulumi.Int(16),
Network: privateNetwork.ID(),
})
if err != nil {
return err
}
privateVpcConnection, err := servicenetworking.NewConnection(ctx, "private_vpc_connection", &servicenetworking.ConnectionArgs{
Network: privateNetwork.ID(),
Service: pulumi.String("servicenetworking.googleapis.com"),
ReservedPeeringRanges: pulumi.StringArray{
privateIpAddress.Name,
},
})
if err != nil {
return err
}
dbNameSuffix, err := random.NewRandomId(ctx, "db_name_suffix", &random.RandomIdArgs{
ByteLength: pulumi.Int(4),
})
if err != nil {
return err
}
_, err = sql.NewDatabaseInstance(ctx, "instance", &sql.DatabaseInstanceArgs{
Name: dbNameSuffix.Hex.ApplyT(func(hex string) (string, error) {
return fmt.Sprintf("private-instance-%v", hex), nil
}).(pulumi.StringOutput),
Region: pulumi.String("us-central1"),
DatabaseVersion: pulumi.String("MYSQL_5_7"),
Settings: &sql.DatabaseInstanceSettingsArgs{
Tier: pulumi.String("db-f1-micro"),
IpConfiguration: &sql.DatabaseInstanceSettingsIpConfigurationArgs{
Ipv4Enabled: pulumi.Bool(false),
PrivateNetwork: privateNetwork.SelfLink,
EnablePrivatePathForGoogleCloudServices: pulumi.Bool(true),
},
},
}, pulumi.DependsOn([]pulumi.Resource{
privateVpcConnection,
}))
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.compute.Network;
import com.pulumi.gcp.compute.NetworkArgs;
import com.pulumi.gcp.compute.GlobalAddress;
import com.pulumi.gcp.compute.GlobalAddressArgs;
import com.pulumi.gcp.servicenetworking.Connection;
import com.pulumi.gcp.servicenetworking.ConnectionArgs;
import com.pulumi.random.RandomId;
import com.pulumi.random.RandomIdArgs;
import com.pulumi.gcp.sql.DatabaseInstance;
import com.pulumi.gcp.sql.DatabaseInstanceArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsIpConfigurationArgs;
import com.pulumi.resources.CustomResourceOptions;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var privateNetwork = new Network("privateNetwork", NetworkArgs.builder()
.name("private-network")
.build());
var privateIpAddress = new GlobalAddress("privateIpAddress", GlobalAddressArgs.builder()
.name("private-ip-address")
.purpose("VPC_PEERING")
.addressType("INTERNAL")
.prefixLength(16)
.network(privateNetwork.id())
.build());
var privateVpcConnection = new Connection("privateVpcConnection", ConnectionArgs.builder()
.network(privateNetwork.id())
.service("servicenetworking.googleapis.com")
.reservedPeeringRanges(privateIpAddress.name())
.build());
var dbNameSuffix = new RandomId("dbNameSuffix", RandomIdArgs.builder()
.byteLength(4)
.build());
var instance = new DatabaseInstance("instance", DatabaseInstanceArgs.builder()
.name(dbNameSuffix.hex().applyValue(_hex -> String.format("private-instance-%s", _hex)))
.region("us-central1")
.databaseVersion("MYSQL_5_7")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-f1-micro")
.ipConfiguration(DatabaseInstanceSettingsIpConfigurationArgs.builder()
.ipv4Enabled(false)
.privateNetwork(privateNetwork.selfLink())
.enablePrivatePathForGoogleCloudServices(true)
.build())
.build())
.build(), CustomResourceOptions.builder()
.dependsOn(privateVpcConnection)
.build());
}
}
resources:
privateNetwork:
type: gcp:compute:Network
name: private_network
properties:
name: private-network
privateIpAddress:
type: gcp:compute:GlobalAddress
name: private_ip_address
properties:
name: private-ip-address
purpose: VPC_PEERING
addressType: INTERNAL
prefixLength: 16
network: ${privateNetwork.id}
privateVpcConnection:
type: gcp:servicenetworking:Connection
name: private_vpc_connection
properties:
network: ${privateNetwork.id}
service: servicenetworking.googleapis.com
reservedPeeringRanges:
- ${privateIpAddress.name}
dbNameSuffix:
type: random:RandomId
name: db_name_suffix
properties:
byteLength: 4
instance:
type: gcp:sql:DatabaseInstance
properties:
name: private-instance-${dbNameSuffix.hex}
region: us-central1
databaseVersion: MYSQL_5_7
settings:
tier: db-f1-micro
ipConfiguration:
ipv4Enabled: false
privateNetwork: ${privateNetwork.selfLink}
enablePrivatePathForGoogleCloudServices: true
options:
dependsOn:
- ${privateVpcConnection}

ENTERPRISE_PLUS Instance with data_cache_config

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const main = new gcp.sql.DatabaseInstance("main", {
name: "enterprise-plus-main-instance",
databaseVersion: "MYSQL_8_0_31",
settings: {
tier: "db-perf-optimized-N-2",
edition: "ENTERPRISE_PLUS",
dataCacheConfig: {
dataCacheEnabled: true,
},
},
});
import pulumi
import pulumi_gcp as gcp
main = gcp.sql.DatabaseInstance("main",
name="enterprise-plus-main-instance",
database_version="MYSQL_8_0_31",
settings={
"tier": "db-perf-optimized-N-2",
"edition": "ENTERPRISE_PLUS",
"data_cache_config": {
"data_cache_enabled": True,
},
})
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;
return await Deployment.RunAsync(() =>
{
var main = new Gcp.Sql.DatabaseInstance("main", new()
{
Name = "enterprise-plus-main-instance",
DatabaseVersion = "MYSQL_8_0_31",
Settings = new Gcp.Sql.Inputs.DatabaseInstanceSettingsArgs
{
Tier = "db-perf-optimized-N-2",
Edition = "ENTERPRISE_PLUS",
DataCacheConfig = new Gcp.Sql.Inputs.DatabaseInstanceSettingsDataCacheConfigArgs
{
DataCacheEnabled = true,
},
},
});
});
package main
import (
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/sql"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := sql.NewDatabaseInstance(ctx, "main", &sql.DatabaseInstanceArgs{
Name: pulumi.String("enterprise-plus-main-instance"),
DatabaseVersion: pulumi.String("MYSQL_8_0_31"),
Settings: &sql.DatabaseInstanceSettingsArgs{
Tier: pulumi.String("db-perf-optimized-N-2"),
Edition: pulumi.String("ENTERPRISE_PLUS"),
DataCacheConfig: &sql.DatabaseInstanceSettingsDataCacheConfigArgs{
DataCacheEnabled: pulumi.Bool(true),
},
},
})
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.sql.DatabaseInstance;
import com.pulumi.gcp.sql.DatabaseInstanceArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsDataCacheConfigArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var main = new DatabaseInstance("main", DatabaseInstanceArgs.builder()
.name("enterprise-plus-main-instance")
.databaseVersion("MYSQL_8_0_31")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-perf-optimized-N-2")
.edition("ENTERPRISE_PLUS")
.dataCacheConfig(DatabaseInstanceSettingsDataCacheConfigArgs.builder()
.dataCacheEnabled(true)
.build())
.build())
.build());
}
}
resources:
main:
type: gcp:sql:DatabaseInstance
properties:
name: enterprise-plus-main-instance
databaseVersion: MYSQL_8_0_31
settings:
tier: db-perf-optimized-N-2
edition: ENTERPRISE_PLUS
dataCacheConfig:
dataCacheEnabled: true

Cloud SQL Instance with PSC connectivity

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const main = new gcp.sql.DatabaseInstance("main", {
name: "psc-enabled-main-instance",
databaseVersion: "MYSQL_8_0",
settings: {
tier: "db-f1-micro",
ipConfiguration: {
pscConfigs: [{
pscEnabled: true,
allowedConsumerProjects: ["allowed-consumer-project-name"],
}],
ipv4Enabled: false,
},
backupConfiguration: {
enabled: true,
binaryLogEnabled: true,
},
availabilityType: "REGIONAL",
},
});
import pulumi
import pulumi_gcp as gcp
main = gcp.sql.DatabaseInstance("main",
name="psc-enabled-main-instance",
database_version="MYSQL_8_0",
settings={
"tier": "db-f1-micro",
"ip_configuration": {
"psc_configs": [{
"psc_enabled": True,
"allowed_consumer_projects": ["allowed-consumer-project-name"],
}],
"ipv4_enabled": False,
},
"backup_configuration": {
"enabled": True,
"binary_log_enabled": True,
},
"availability_type": "REGIONAL",
})
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;
return await Deployment.RunAsync(() =>
{
var main = new Gcp.Sql.DatabaseInstance("main", new()
{
Name = "psc-enabled-main-instance",
DatabaseVersion = "MYSQL_8_0",
Settings = new Gcp.Sql.Inputs.DatabaseInstanceSettingsArgs
{
Tier = "db-f1-micro",
IpConfiguration = new Gcp.Sql.Inputs.DatabaseInstanceSettingsIpConfigurationArgs
{
PscConfigs = new[]
{
new Gcp.Sql.Inputs.DatabaseInstanceSettingsIpConfigurationPscConfigArgs
{
PscEnabled = true,
AllowedConsumerProjects = new[]
{
"allowed-consumer-project-name",
},
},
},
Ipv4Enabled = false,
},
BackupConfiguration = new Gcp.Sql.Inputs.DatabaseInstanceSettingsBackupConfigurationArgs
{
Enabled = true,
BinaryLogEnabled = true,
},
AvailabilityType = "REGIONAL",
},
});
});
package main
import (
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/sql"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := sql.NewDatabaseInstance(ctx, "main", &sql.DatabaseInstanceArgs{
Name: pulumi.String("psc-enabled-main-instance"),
DatabaseVersion: pulumi.String("MYSQL_8_0"),
Settings: &sql.DatabaseInstanceSettingsArgs{
Tier: pulumi.String("db-f1-micro"),
IpConfiguration: &sql.DatabaseInstanceSettingsIpConfigurationArgs{
PscConfigs: sql.DatabaseInstanceSettingsIpConfigurationPscConfigArray{
&sql.DatabaseInstanceSettingsIpConfigurationPscConfigArgs{
PscEnabled: pulumi.Bool(true),
AllowedConsumerProjects: pulumi.StringArray{
pulumi.String("allowed-consumer-project-name"),
},
},
},
Ipv4Enabled: pulumi.Bool(false),
},
BackupConfiguration: &sql.DatabaseInstanceSettingsBackupConfigurationArgs{
Enabled: pulumi.Bool(true),
BinaryLogEnabled: pulumi.Bool(true),
},
AvailabilityType: pulumi.String("REGIONAL"),
},
})
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.sql.DatabaseInstance;
import com.pulumi.gcp.sql.DatabaseInstanceArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsIpConfigurationArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsBackupConfigurationArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var main = new DatabaseInstance("main", DatabaseInstanceArgs.builder()
.name("psc-enabled-main-instance")
.databaseVersion("MYSQL_8_0")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-f1-micro")
.ipConfiguration(DatabaseInstanceSettingsIpConfigurationArgs.builder()
.pscConfigs(DatabaseInstanceSettingsIpConfigurationPscConfigArgs.builder()
.pscEnabled(true)
.allowedConsumerProjects("allowed-consumer-project-name")
.build())
.ipv4Enabled(false)
.build())
.backupConfiguration(DatabaseInstanceSettingsBackupConfigurationArgs.builder()
.enabled(true)
.binaryLogEnabled(true)
.build())
.availabilityType("REGIONAL")
.build())
.build());
}
}
resources:
main:
type: gcp:sql:DatabaseInstance
properties:
name: psc-enabled-main-instance
databaseVersion: MYSQL_8_0
settings:
tier: db-f1-micro
ipConfiguration:
pscConfigs:
- pscEnabled: true
allowedConsumerProjects:
- allowed-consumer-project-name
ipv4Enabled: false
backupConfiguration:
enabled: true
binaryLogEnabled: true
availabilityType: REGIONAL

Cloud SQL Instance with PSC auto connections

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
const main = new gcp.sql.DatabaseInstance("main", {
name: "psc-enabled-main-instance",
databaseVersion: "MYSQL_8_0",
settings: {
tier: "db-f1-micro",
ipConfiguration: {
pscConfigs: [{
pscEnabled: true,
allowedConsumerProjects: ["allowed-consumer-project-name"],
pscAutoConnections: [{
consumerNetwork: "network-name",
consumerServiceProjectId: "project-id",
}],
}],
ipv4Enabled: false,
},
backupConfiguration: {
enabled: true,
binaryLogEnabled: true,
},
availabilityType: "REGIONAL",
},
});
import pulumi
import pulumi_gcp as gcp
main = gcp.sql.DatabaseInstance("main",
name="psc-enabled-main-instance",
database_version="MYSQL_8_0",
settings={
"tier": "db-f1-micro",
"ip_configuration": {
"psc_configs": [{
"psc_enabled": True,
"allowed_consumer_projects": ["allowed-consumer-project-name"],
"psc_auto_connections": [{
"consumer_network": "network-name",
"consumer_service_project_id": "project-id",
}],
}],
"ipv4_enabled": False,
},
"backup_configuration": {
"enabled": True,
"binary_log_enabled": True,
},
"availability_type": "REGIONAL",
})
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Gcp = Pulumi.Gcp;
return await Deployment.RunAsync(() =>
{
var main = new Gcp.Sql.DatabaseInstance("main", new()
{
Name = "psc-enabled-main-instance",
DatabaseVersion = "MYSQL_8_0",
Settings = new Gcp.Sql.Inputs.DatabaseInstanceSettingsArgs
{
Tier = "db-f1-micro",
IpConfiguration = new Gcp.Sql.Inputs.DatabaseInstanceSettingsIpConfigurationArgs
{
PscConfigs = new[]
{
new Gcp.Sql.Inputs.DatabaseInstanceSettingsIpConfigurationPscConfigArgs
{
PscEnabled = true,
AllowedConsumerProjects = new[]
{
"allowed-consumer-project-name",
},
PscAutoConnections = new[]
{
new Gcp.Sql.Inputs.DatabaseInstanceSettingsIpConfigurationPscConfigPscAutoConnectionArgs
{
ConsumerNetwork = "network-name",
ConsumerServiceProjectId = "project-id",
},
},
},
},
Ipv4Enabled = false,
},
BackupConfiguration = new Gcp.Sql.Inputs.DatabaseInstanceSettingsBackupConfigurationArgs
{
Enabled = true,
BinaryLogEnabled = true,
},
AvailabilityType = "REGIONAL",
},
});
});
package main
import (
"github.com/pulumi/pulumi-gcp/sdk/v8/go/gcp/sql"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := sql.NewDatabaseInstance(ctx, "main", &sql.DatabaseInstanceArgs{
Name: pulumi.String("psc-enabled-main-instance"),
DatabaseVersion: pulumi.String("MYSQL_8_0"),
Settings: &sql.DatabaseInstanceSettingsArgs{
Tier: pulumi.String("db-f1-micro"),
IpConfiguration: &sql.DatabaseInstanceSettingsIpConfigurationArgs{
PscConfigs: sql.DatabaseInstanceSettingsIpConfigurationPscConfigArray{
&sql.DatabaseInstanceSettingsIpConfigurationPscConfigArgs{
PscEnabled: pulumi.Bool(true),
AllowedConsumerProjects: pulumi.StringArray{
pulumi.String("allowed-consumer-project-name"),
},
PscAutoConnections: sql.DatabaseInstanceSettingsIpConfigurationPscConfigPscAutoConnectionArray{
&sql.DatabaseInstanceSettingsIpConfigurationPscConfigPscAutoConnectionArgs{
ConsumerNetwork: pulumi.String("network-name"),
ConsumerServiceProjectId: pulumi.String("project-id"),
},
},
},
},
Ipv4Enabled: pulumi.Bool(false),
},
BackupConfiguration: &sql.DatabaseInstanceSettingsBackupConfigurationArgs{
Enabled: pulumi.Bool(true),
BinaryLogEnabled: pulumi.Bool(true),
},
AvailabilityType: pulumi.String("REGIONAL"),
},
})
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.gcp.sql.DatabaseInstance;
import com.pulumi.gcp.sql.DatabaseInstanceArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsIpConfigurationArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsBackupConfigurationArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
var main = new DatabaseInstance("main", DatabaseInstanceArgs.builder()
.name("psc-enabled-main-instance")
.databaseVersion("MYSQL_8_0")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-f1-micro")
.ipConfiguration(DatabaseInstanceSettingsIpConfigurationArgs.builder()
.pscConfigs(DatabaseInstanceSettingsIpConfigurationPscConfigArgs.builder()
.pscEnabled(true)
.allowedConsumerProjects("allowed-consumer-project-name")
.pscAutoConnections(DatabaseInstanceSettingsIpConfigurationPscConfigPscAutoConnectionArgs.builder()
.consumerNetwork("network-name")
.consumerServiceProjectId("project-id")
.build())
.build())
.ipv4Enabled(false)
.build())
.backupConfiguration(DatabaseInstanceSettingsBackupConfigurationArgs.builder()
.enabled(true)
.binaryLogEnabled(true)
.build())
.availabilityType("REGIONAL")
.build())
.build());
}
}
resources:
main:
type: gcp:sql:DatabaseInstance
properties:
name: psc-enabled-main-instance
databaseVersion: MYSQL_8_0
settings:
tier: db-f1-micro
ipConfiguration:
pscConfigs:
- pscEnabled: true
allowedConsumerProjects:
- allowed-consumer-project-name
pscAutoConnections:
- consumerNetwork: network-name
consumerServiceProjectId: project-id
ipv4Enabled: false
backupConfiguration:
enabled: true
binaryLogEnabled: true
availabilityType: REGIONAL

Switchover

Users can perform a switchover on a replica by following the steps below. ~>WARNING: Failure to follow these steps can lead to data loss (You will be warned during plan stage). To prevent data loss during a switchover, please verify your plan with the checklist below. For a more in-depth walkthrough with example code, see the Switchover Guide

Steps to Invoke Switchover

MySQL/PostgreSQL: Create a cross-region, Enterprise Plus edition primary and replica pair, then set the value of primary's replication_cluster.failover_dr_replica_name as the replica. SQL Server: Create a cascadable replica in a different region from the primary (cascadable_replica is set to true in replica_configuration)

Invoking switchover in the replica resource:

  1. Change instance_type from READ_REPLICA_INSTANCE to CLOUD_SQL_INSTANCE

  2. Remove master_instance_name

  3. (SQL Server) Remove replica_configuration

  4. Add current primary's name to the replica's replica_names list

  5. (MySQL/PostgreSQL) Add current primary's name to the replica's replication_cluster.failover_dr_replica_name.

  6. (MySQL/PostgreSQL) Adjust backup_configuration. See Switchover Guide for details.

Updating the primary resource:

  1. Change instance_type from CLOUD_SQL_INSTANCE to READ_REPLICA_INSTANCE

  2. Set master_instance_name to the original replica (which will be primary after switchover)

  3. (SQL Server) Set replica_configuration and set cascadable_replica to true

  4. Remove original replica from replica_names

    • NOTE: Do not delete the replica_names field, even if it has no replicas remaining. Set replica_names = [ ] to indicate it having no replicas.

  5. (MySQL/PostgreSQL) Set replication_cluster.failover_dr_replica_name as the empty string.

  6. (MySQL/PostgreSQL) Adjust backup_configuration. See Switchover Guide for details.

Plan and verify that:

  • pulumi preview outputs "0 to add, 0 to destroy"

  • pulumi preview does not say "must be replaced" for any resource

  • Every resource "will be updated in-place"

  • Only the 2 instances involved in switchover have planned changes

  • (Recommended) Use deletion_protection on instances as a safety measure

Import

Database instances can be imported using one of any of these accepted formats:

  • projects/{{project}}/instances/{{name}}

  • {{project}}/{{name}}

  • {{name}} When using the pulumi import command, Database instances can be imported using one of the formats above. For example:

$ pulumi import gcp:sql/databaseInstance:DatabaseInstance default projects/{{project}}/instances/{{name}}
$ pulumi import gcp:sql/databaseInstance:DatabaseInstance default {{project}}/{{name}}
$ pulumi import gcp:sql/databaseInstance:DatabaseInstance default {{name}}

config and set on the server. When importing, double-check that your config has all the fields set that you expect- just seeing no diff isn't sufficient to know that your config could reproduce the imported resource.

Properties

Link copied to clipboard

The list of all maintenance versions applicable on the instance.

Link copied to clipboard

The context needed to create this instance as a clone of another instance. When this field is set during resource creation, this provider will attempt to clone another instance as indicated in the context. The configuration is detailed below.

Link copied to clipboard
val connectionName: Output<String>

The connection name of the instance to be used in connection strings. For example, when connecting with Cloud SQL Proxy.

Link copied to clipboard
val databaseVersion: Output<String>

The MySQL, PostgreSQL or SQL Server version to use. Supported values include MYSQL_5_6, MYSQL_5_7, MYSQL_8_0, MYSQL_8_4, POSTGRES_9_6,POSTGRES_10, POSTGRES_11, POSTGRES_12, POSTGRES_13, POSTGRES_14, POSTGRES_15, POSTGRES_16, POSTGRES_17, SQLSERVER_2017_STANDARD, SQLSERVER_2017_ENTERPRISE, SQLSERVER_2017_EXPRESS, SQLSERVER_2017_WEB. SQLSERVER_2019_STANDARD, SQLSERVER_2019_ENTERPRISE, SQLSERVER_2019_EXPRESS, SQLSERVER_2019_WEB. Database Version Policies includes an up-to-date reference of supported versions.

Link copied to clipboard

Whether or not to allow the provider to destroy the instance. Unless this field is set to false in state, a destroy or update command that deletes the instance will fail. Defaults to true.

Link copied to clipboard
val dnsName: Output<String>

The DNS name of the instance. See Connect to an instance using Private Service Connect for more details.

Link copied to clipboard

The full path to the encryption key used for the CMEK disk encryption. Setting up disk encryption currently requires manual steps outside of this provider. The provided key must be in the same region as the SQL instance. In order to use this feature, a special kind of service account must be created and granted permission on this key. This step can currently only be done manually, please see this step. That service account needs the Cloud KMS > Cloud KMS CryptoKey Encrypter/Decrypter role on your key - please see this step.

Link copied to clipboard
val firstIpAddress: Output<String>

The first IPv4 address of any type assigned.

Link copied to clipboard
val id: Output<String>
Link copied to clipboard
val instanceType: Output<String>

The type of the instance. The supported values are SQL_INSTANCE_TYPE_UNSPECIFIED, CLOUD_SQL_INSTANCE, ON_PREMISES_INSTANCE and READ_REPLICA_INSTANCE.

Link copied to clipboard
Link copied to clipboard

The current software version on the instance. This attribute can not be set during creation. Refer to available_maintenance_versions attribute to see what maintenance_version are available for upgrade. When this attribute gets updated, it will cause an instance restart. Setting a maintenance_version value that is older than the current one on the instance will be ignored.

Link copied to clipboard

The name of the existing instance that will act as the master in the replication setup. Note, this requires the master to have binary_log_enabled set, as well as existing backups.

Link copied to clipboard
val name: Output<String>

The name of the instance. If the name is left blank, the provider will randomly generate one when the instance is first created. This is done because after a name is used, it cannot be reused for up to one week.

Link copied to clipboard

The first private (PRIVATE) IPv4 address assigned.

Link copied to clipboard
val project: Output<String>

The ID of the project in which the resource belongs. If it is not provided, the provider project is used.

Link copied to clipboard

the URI that points to the service attachment of the instance.

Link copied to clipboard
val publicIpAddress: Output<String>

The first public (PRIMARY) IPv4 address assigned.

Link copied to clipboard
val pulumiChildResources: Set<KotlinResource>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val region: Output<String>

The region the instance will sit in. If a region is not provided in the resource definition, the provider region will be used instead.

Link copied to clipboard

The configuration for replication. The configuration is detailed below.

Link copied to clipboard
val replicaNames: Output<List<String>>

List of replica names. Can be updated.

Link copied to clipboard

A primary instance and disaster recovery replica pair. Applicable to MySQL and PostgreSQL. This field can be set only after both the primary and replica are created.

Link copied to clipboard

The context needed to restore the database to a backup run. This field will cause the provider to trigger the database to restore from the backup run indicated. The configuration is detailed below. NOTE: Restoring from a backup is an imperative action and not recommended via this provider. Adding or modifying this block during resource creation/update will trigger the restore action after the resource is created/updated.

Link copied to clipboard
val rootPassword: Output<String>?

Initial root password. Can be updated. Required for MS SQL Server.

Link copied to clipboard
val selfLink: Output<String>

The URI of the created resource.

Link copied to clipboard
Link copied to clipboard

The service account email address assigned to the instance.

Link copied to clipboard

The settings to use for the database. The configuration is detailed below. Required if clone is not set.

Link copied to clipboard
val urn: Output<String>