Database Instance
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 usegcp.sql.User
to define a custom user with a restricted host and strong password. Note: On newer versions of the provider, you must explicitly setdeletion_protection=false
(and runpulumi 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
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()
.databaseVersion("POSTGRES_15")
.region("us-central1")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-f1-micro")
.build())
.build());
}
}
Private IP Instance
NOTE: For private IP instance setup, note that the
gcp.sql.DatabaseInstance
does not actually interpolate values fromgcp.servicenetworking.Connection
. You must explicitly add adepends_on
reference as shown below.
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.Empty, CustomResourceOptions.builder()
.provider(google_beta)
.build());
var privateIpAddress = new GlobalAddress("privateIpAddress", GlobalAddressArgs.builder()
.purpose("VPC_PEERING")
.addressType("INTERNAL")
.prefixLength(16)
.network(privateNetwork.id())
.build(), CustomResourceOptions.builder()
.provider(google_beta)
.build());
var privateVpcConnection = new Connection("privateVpcConnection", ConnectionArgs.builder()
.network(privateNetwork.id())
.service("servicenetworking.googleapis.com")
.reservedPeeringRanges(privateIpAddress.name())
.build(), CustomResourceOptions.builder()
.provider(google_beta)
.build());
var dbNameSuffix = new RandomId("dbNameSuffix", RandomIdArgs.builder()
.byteLength(4)
.build());
var instance = new DatabaseInstance("instance", DatabaseInstanceArgs.builder()
.region("us-central1")
.databaseVersion("MYSQL_5_7")
.settings(DatabaseInstanceSettingsArgs.builder()
.tier("db-f1-micro")
.ipConfiguration(DatabaseInstanceSettingsIpConfigurationArgs.builder()
.ipv4Enabled(false)
.privateNetwork(privateNetwork.id())
.enablePrivatePathForGoogleCloudServices(true)
.build())
.build())
.build(), CustomResourceOptions.builder()
.provider(google_beta)
.dependsOn(privateVpcConnection)
.build());
}
}
ENTERPRISE_PLUS Instance with data_cache_config
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()
.databaseVersion("MYSQL_8_0_31")
.settings(DatabaseInstanceSettingsArgs.builder()
.dataCacheConfig(DatabaseInstanceSettingsDataCacheConfigArgs.builder()
.dataCacheEnabled(true)
.build())
.edition("ENTERPRISE_PLUS")
.tier("db-perf-optimized-N-2")
.build())
.build());
}
}
Cloud SQL Instance with PSC connectivity
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.DatabaseInstanceSettingsBackupConfigurationArgs;
import com.pulumi.gcp.sql.inputs.DatabaseInstanceSettingsIpConfigurationArgs;
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()
.databaseVersion("MYSQL_8_0")
.settings(DatabaseInstanceSettingsArgs.builder()
.availabilityType("REGIONAL")
.backupConfiguration(DatabaseInstanceSettingsBackupConfigurationArgs.builder()
.binaryLogEnabled(true)
.enabled(true)
.build())
.ipConfiguration(DatabaseInstanceSettingsIpConfigurationArgs.builder()
.ipv4Enabled(false)
.pscConfig(%!v(PANIC=Format method: runtime error: invalid memory address or nil pointer dereference))
.build())
.tier("db-f1-micro")
.build())
.build());
}
}
Import
Database instances can be imported using one of any of these accepted formats
$ pulumi import gcp:sql/databaseInstance:DatabaseInstance main projects/{{project}}/instances/{{name}}
$ pulumi import gcp:sql/databaseInstance:DatabaseInstance main {{project}}/{{name}}
$ pulumi import gcp:sql/databaseInstance:DatabaseInstance main {{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
The list of all maintenance versions applicable on the instance.
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.
The connection name of the instance to be used in connection strings. For example, when connecting with Cloud SQL Proxy.
The MySQL, PostgreSQL or SQL Server version to use. Supported values include MYSQL_5_6
, MYSQL_5_7
, MYSQL_8_0
, POSTGRES_9_6
,POSTGRES_10
, POSTGRES_11
, POSTGRES_12
, POSTGRES_13
, POSTGRES_14
, POSTGRES_15
, 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.
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
.
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.
The first IPv4 address of any type assigned.
The type of the instance. The supported values are SQL_INSTANCE_TYPE_UNSPECIFIED
, CLOUD_SQL_INSTANCE
, ON_PREMISES_INSTANCE
and READ_REPLICA_INSTANCE
.
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.
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.
The first private (PRIVATE
) IPv4 address assigned.
The link to service attachment of PSC instance.
The first public (PRIMARY
) IPv4 address assigned.
The configuration for replication. The configuration is detailed below. Valid only for MySQL instances.
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.
Initial root password. Can be updated. Required for MS SQL Server.
The service account email address assigned to the instance.
The settings to use for the database. The configuration is detailed below. Required if clone
is not set.