UNPKG

@pulumi/gcp

Version:

A Pulumi package for creating and managing Google Cloud Platform resources.

520 lines • 22.6 kB
"use strict"; // *** WARNING: this file was generated by pulumi-language-nodejs. *** // *** Do not edit by hand unless you're certain you know what you are doing! *** var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseInstance = void 0; const pulumi = __importStar(require("@pulumi/pulumi")); const utilities = __importStar(require("../utilities")); /** * Creates a new Google SQL Database Instance. For more information, see the [official documentation](https://cloud.google.com/sql/docs/mysql/create-instance), * or the [JSON API](https://cloud.google.com/sql/docs/admin-api/v1beta4/instances). * * > **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 * * ```typescript * 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", * }, * }); * ``` * * ### Granular restriction of network access * * ```typescript * import * as pulumi from "@pulumi/pulumi"; * import * as gcp from "@pulumi/gcp"; * import * as random from "@pulumi/random"; * * const apps: gcp.compute.Instance[] = []; * for (const range = {value: 0}; range.value < 8; range.value++) { * apps.push(new gcp.compute.Instance(`apps-${range.value}`, { * networkInterfaces: [{ * accessConfigs: [{}], * network: "default", * }], * name: `apps-${range.value + 1}`, * machineType: "f1-micro", * bootDisk: { * initializeParams: { * image: "ubuntu-os-cloud/ubuntu-1804-lts", * }, * }, * })); * } * const dbNameSuffix = new random.index.Id("db_name_suffix", {byteLength: 4}); * const onprem = [ * "192.168.1.2", * "192.168.2.3", * ]; * const postgres = new gcp.sql.DatabaseInstance("postgres", { * name: `postgres-instance-${dbNameSuffix.hex}`, * databaseVersion: "POSTGRES_15", * settings: { * tier: "db-f1-micro", * ipConfiguration: { * authorizedNetworks: Object.entries(apps).sort().map(([k, v]) => ({key: k, value: v})).apply(entries => entries.map(entry => ({ * name: entry.value.name, * value: entry.value.networkInterface[0].accessConfig[0].natIp, * }))), * authorizedNetworks: onprem.map((v, k) => ({key: k, value: v})).map(entry2 => ({ * name: `onprem-${entry2.key}`, * value: entry2.value, * })), * }, * }, * }); * ``` * * ### 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 `dependsOn`reference as shown below. * * ```typescript * 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.index.Id("db_name_suffix", {byteLength: 4}); * const instance = new gcp.sql.DatabaseInstance("instance", { * 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, * }, * }, * }, { * dependsOn: [privateVpcConnection], * }); * ``` * * ### ENTERPRISE_PLUS Instance with dataCacheConfig * * ```typescript * 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, * }, * }, * }); * ``` * * ### Cloud SQL Instance with Managed Connection Pooling * ```typescript * import * as pulumi from "@pulumi/pulumi"; * import * as gcp from "@pulumi/gcp"; * * const instance = new gcp.sql.DatabaseInstance("instance", { * name: "mcp-enabled-main-instance", * region: "us-central1", * databaseVersion: "POSTGRES_16", * settings: { * tier: "db-perf-optimized-N-2", * edition: "ENTERPRISE_PLUS", * connectionPoolConfigs: [{ * connectionPoolingEnabled: true, * flags: [{ * name: "max_client_connections", * value: "1980", * }], * }], * }, * }); * ``` * * ### Cloud SQL Instance with PSC connectivity * * ```typescript * 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", * }, * }); * ``` * * ### Cloud SQL Instance with PSC auto connections * * ```typescript * 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", * }, * }); * ``` * * ### Cloud SQL Instance with PSC outbound * * ```typescript * 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"], * networkAttachmentUri: "network-attachment-uri", * }], * ipv4Enabled: false, * }, * backupConfiguration: { * enabled: true, * binaryLogEnabled: true, * }, * availabilityType: "REGIONAL", * }, * }); * ``` * * ### Cloud SQL Instance created with backupdrBackup * > **NOTE:** For restoring from a backupdr_backup, note that the backup must be in active state. List down the backups using `gcp.backupdisasterrecovery.getBackup`. Replace `backupdrBackupFullPath` with the backup name. * * ```typescript * import * as pulumi from "@pulumi/pulumi"; * import * as gcp from "@pulumi/gcp"; * * const instance = new gcp.sql.DatabaseInstance("instance", { * name: "main-instance", * databaseVersion: "MYSQL_8_0", * settings: { * tier: "db-f1-micro", * backupConfiguration: { * enabled: true, * binaryLogEnabled: true, * }, * }, * backupdrBackup: "backupdr_backup_full_path", * }); * ``` * * ### Cloud SQL Instance created using pointInTimeRestore * > **NOTE:** Replace `backupdrDatasource` with the full datasource path, `timeStamp` should be in the format of `YYYY-MM-DDTHH:MM:SSZ`. The `targetInstance` is required field and must match the name of the resource. * * ```typescript * import * as pulumi from "@pulumi/pulumi"; * import * as gcp from "@pulumi/gcp"; * * const instance = new gcp.sql.DatabaseInstance("instance", { * name: "main-instance", * databaseVersion: "MYSQL_8_0", * settings: { * tier: "db-f1-micro", * backupConfiguration: { * enabled: true, * binaryLogEnabled: true, * }, * }, * pointInTimeRestoreContext: { * datasource: "backupdr_datasource", * targetInstance: "main-instance", * pointInTime: "time_stamp", * }, * }); * ``` * * ### Cloud SQL Instance created using pointInTimeRestore using multiregion datasource * > **NOTE:** Replace `backupdrDatasource` with the full datasource path, `timeStamp` should be in the format of `YYYY-MM-DDTHH:MM:SSZ` and `region` with the target instance region. The `targetInstance` is required field and must match the name of the resource. * * ```typescript * import * as pulumi from "@pulumi/pulumi"; * import * as gcp from "@pulumi/gcp"; * * const instance = new gcp.sql.DatabaseInstance("instance", { * name: "main-instance", * databaseVersion: "MYSQL_8_0", * settings: { * tier: "db-f1-micro", * backupConfiguration: { * enabled: true, * binaryLogEnabled: true, * }, * }, * pointInTimeRestoreContext: { * datasource: "backupdr_datasource", * targetInstance: "main-instance", * pointInTime: "time_stamp", * region: "region", * }, * }); * ``` * * ## 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 (`cascadableReplica` is set to true in `replicaConfiguration`) * * #### Invoking switchover in the replica resource: * 1. Change instanceType from `READ_REPLICA_INSTANCE` to `CLOUD_SQL_INSTANCE` * 2. Remove `masterInstanceName` * 3. (SQL Server) Remove `replicaConfiguration` * 4. Add current primary's name to the replica's `replicaNames` list * 5. (MySQL/PostgreSQL) Add current primary's name to the replica's `replication_cluster.failover_dr_replica_name`. * 6. (MySQL/PostgreSQL) Adjust `backupConfiguration`. See Switchover Guide for details. * * #### Updating the primary resource: * 1. Change `instanceType` from `CLOUD_SQL_INSTANCE` to `READ_REPLICA_INSTANCE` * 2. Set `masterInstanceName` to the original replica (which will be primary after switchover) * 3. (SQL Server) Set `replicaConfiguration` and set `cascadableReplica` to `true` * 4. Remove original replica from `replicaNames` * * **NOTE**: Do **not** delete the replicaNames field, even if it has no replicas remaining. Set replicaNames = [ ] to indicate it having no replicas. * 5. (MySQL/PostgreSQL) Set `replication_cluster.failover_dr_replica_name` as the empty string. * 6. (MySQL/PostgreSQL) Adjust `backupConfiguration`. 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 `deletionProtection` 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: * * ```sh * $ 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}} * ``` * * > **NOTE:** Some fields (such as `replicaConfiguration`) won't show a diff if they are unset in * 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. */ class DatabaseInstance extends pulumi.CustomResource { /** * Get an existing DatabaseInstance resource's state with the given name, ID, and optional extra * properties used to qualify the lookup. * * @param name The _unique_ name of the resulting resource. * @param id The _unique_ provider ID of the resource to lookup. * @param state Any extra arguments used during the lookup. * @param opts Optional settings to control the behavior of the CustomResource. */ static get(name, id, state, opts) { return new DatabaseInstance(name, state, { ...opts, id: id }); } /** @internal */ static __pulumiType = 'gcp:sql/databaseInstance:DatabaseInstance'; /** * Returns true if the given object is an instance of DatabaseInstance. This is designed to work even * when multiple copies of the Pulumi SDK have been loaded into the same process. */ static isInstance(obj) { if (obj === undefined || obj === null) { return false; } return obj['__pulumiType'] === DatabaseInstance.__pulumiType; } constructor(name, argsOrState, opts) { let resourceInputs = {}; opts = opts || {}; if (opts.id) { const state = argsOrState; resourceInputs["availableMaintenanceVersions"] = state?.availableMaintenanceVersions; resourceInputs["backupdrBackup"] = state?.backupdrBackup; resourceInputs["clone"] = state?.clone; resourceInputs["connectionName"] = state?.connectionName; resourceInputs["databaseVersion"] = state?.databaseVersion; resourceInputs["deletionPolicy"] = state?.deletionPolicy; resourceInputs["deletionProtection"] = state?.deletionProtection; resourceInputs["dnsName"] = state?.dnsName; resourceInputs["dnsNames"] = state?.dnsNames; resourceInputs["encryptionKeyName"] = state?.encryptionKeyName; resourceInputs["finalBackupDescription"] = state?.finalBackupDescription; resourceInputs["firstIpAddress"] = state?.firstIpAddress; resourceInputs["instanceType"] = state?.instanceType; resourceInputs["ipAddresses"] = state?.ipAddresses; resourceInputs["maintenanceVersion"] = state?.maintenanceVersion; resourceInputs["masterInstanceName"] = state?.masterInstanceName; resourceInputs["name"] = state?.name; resourceInputs["nodeCount"] = state?.nodeCount; resourceInputs["pointInTimeRestoreContext"] = state?.pointInTimeRestoreContext; resourceInputs["privateIpAddress"] = state?.privateIpAddress; resourceInputs["project"] = state?.project; resourceInputs["pscServiceAttachmentLink"] = state?.pscServiceAttachmentLink; resourceInputs["publicIpAddress"] = state?.publicIpAddress; resourceInputs["region"] = state?.region; resourceInputs["replicaConfiguration"] = state?.replicaConfiguration; resourceInputs["replicaNames"] = state?.replicaNames; resourceInputs["replicationCluster"] = state?.replicationCluster; resourceInputs["restoreBackupContext"] = state?.restoreBackupContext; resourceInputs["rootPassword"] = state?.rootPassword; resourceInputs["rootPasswordWo"] = state?.rootPasswordWo; resourceInputs["rootPasswordWoVersion"] = state?.rootPasswordWoVersion; resourceInputs["selfLink"] = state?.selfLink; resourceInputs["serverCaCerts"] = state?.serverCaCerts; resourceInputs["serviceAccountEmailAddress"] = state?.serviceAccountEmailAddress; resourceInputs["settings"] = state?.settings; } else { const args = argsOrState; if (args?.databaseVersion === undefined && !opts.urn) { throw new Error("Missing required property 'databaseVersion'"); } resourceInputs["backupdrBackup"] = args?.backupdrBackup; resourceInputs["clone"] = args?.clone; resourceInputs["databaseVersion"] = args?.databaseVersion; resourceInputs["deletionPolicy"] = args?.deletionPolicy; resourceInputs["deletionProtection"] = args?.deletionProtection; resourceInputs["encryptionKeyName"] = args?.encryptionKeyName; resourceInputs["finalBackupDescription"] = args?.finalBackupDescription; resourceInputs["instanceType"] = args?.instanceType; resourceInputs["maintenanceVersion"] = args?.maintenanceVersion; resourceInputs["masterInstanceName"] = args?.masterInstanceName; resourceInputs["name"] = args?.name; resourceInputs["nodeCount"] = args?.nodeCount; resourceInputs["pointInTimeRestoreContext"] = args?.pointInTimeRestoreContext; resourceInputs["project"] = args?.project; resourceInputs["region"] = args?.region; resourceInputs["replicaConfiguration"] = args?.replicaConfiguration ? pulumi.secret(args.replicaConfiguration) : undefined; resourceInputs["replicaNames"] = args?.replicaNames; resourceInputs["replicationCluster"] = args?.replicationCluster; resourceInputs["restoreBackupContext"] = args?.restoreBackupContext; resourceInputs["rootPassword"] = args?.rootPassword ? pulumi.secret(args.rootPassword) : undefined; resourceInputs["rootPasswordWo"] = args?.rootPasswordWo ? pulumi.secret(args.rootPasswordWo) : undefined; resourceInputs["rootPasswordWoVersion"] = args?.rootPasswordWoVersion; resourceInputs["settings"] = args?.settings; resourceInputs["availableMaintenanceVersions"] = undefined /*out*/; resourceInputs["connectionName"] = undefined /*out*/; resourceInputs["dnsName"] = undefined /*out*/; resourceInputs["dnsNames"] = undefined /*out*/; resourceInputs["firstIpAddress"] = undefined /*out*/; resourceInputs["ipAddresses"] = undefined /*out*/; resourceInputs["privateIpAddress"] = undefined /*out*/; resourceInputs["pscServiceAttachmentLink"] = undefined /*out*/; resourceInputs["publicIpAddress"] = undefined /*out*/; resourceInputs["selfLink"] = undefined /*out*/; resourceInputs["serverCaCerts"] = undefined /*out*/; resourceInputs["serviceAccountEmailAddress"] = undefined /*out*/; } opts = pulumi.mergeOptions(utilities.resourceOptsDefaults(), opts); const secretOpts = { additionalSecretOutputs: ["replicaConfiguration", "rootPassword", "rootPasswordWo", "serverCaCerts"] }; opts = pulumi.mergeOptions(opts, secretOpts); super(DatabaseInstance.__pulumiType, name, resourceInputs, opts); } } exports.DatabaseInstance = DatabaseInstance; //# sourceMappingURL=databaseInstance.js.map