google_sql_database_instance
Creates a new Google SQL Database Instance. For more information, see the official documentation, or the JSON API.
NOTE on
google_sql_database_instance
: - Second-generation instances include a default 'root'@'%' user with no password. This user will be deleted by Terraform on instance creation. You should usegoogle_sql_user
to define a custom user with a restricted host and strong password.
Example Usage
SQL First Generation
resource "google_sql_database_instance" "master" { name = "master-instance" database_version = "MYSQL_5_6" # First-generation instance regions are not the conventional # Google Compute Engine regions. See argument reference below. region = "us-central" settings { tier = "D0" } }
SQL Second generation
resource "google_sql_database_instance" "master" { name = "master-instance" database_version = "POSTGRES_9_6" region = "us-central1" settings { # Second-generation instance tiers are based on the machine # type. See argument reference below. tier = "db-f1-micro" } }
Argument Reference
The following arguments are supported:
-
region
- (Required) The region the instance will sit in. Note, first-generation Cloud SQL instance regions do not line up with the Google Compute Engine (GCE) regions, and Cloud SQL is not available in all regions - choose from one of the options listed here. A valid region must be provided to use this resource. If a region is not provided in the resource definition, the provider region will be used instead, but this will be an apply-time error for all first-generation instances and for second-generation instances if the provider region is not supported with Cloud SQL. If you choose not to provide theregion
argument for this resource, make sure you understand this. -
settings
- (Required) The settings to use for the database. The configuration is detailed below.
-
database_version
- (Optional, Default:MYSQL_5_6
) The MySQL version to use. Can beMYSQL_5_6
,MYSQL_5_7
orPOSTGRES_9_6
for second-generation instances, orMYSQL_5_5
orMYSQL_5_6
for first-generation instances. See Second Generation Capabilities for more information.POSTGRES_9_6
support is in Beta. -
name
- (Optional, Computed) The name of the instance. If the name is left blank, Terraform 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. -
master_instance_name
- (Optional) The name of the instance that will act as the master in the replication setup. Note, this requires the master to havebinary_log_enabled
set, as well as existing backups. -
project
- (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. -
replica_configuration
- (Optional) The configuration for replication. The configuration is detailed below.
The required settings
block supports:
-
tier
- (Required) The machine tier (First Generation) or type (Second Generation) to use. See tiers for more details and supported versions. Postgres supports only shared-core machine types such asdb-f1-micro
, and custom machine types such asdb-custom-2-13312
. See the Custom Machine Type Documentation to learn about specifying custom machine types. -
activation_policy
- (Optional) This specifies when the instance should be active. Can be eitherALWAYS
,NEVER
orON_DEMAND
. -
authorized_gae_applications
- (Optional) A list of Google App Engine (GAE) project names that are allowed to access this instance. -
availability_type
- (Optional) This specifies whether a PostgreSQL instance should be set up for high availability (REGIONAL
) or single zone (ZONAL
). -
crash_safe_replication
- (Optional) Specific to read instances, indicates when crash-safe replication flags are enabled. -
disk_autoresize
- (Optional, Second Generation, Default:true
) Configuration to increase storage size automatically. -
disk_size
- (Optional, Second Generation, Default:10
) The size of data disk, in GB. Size of a running instance cannot be reduced but can be increased. -
disk_type
- (Optional, Second Generation, Default:PD_SSD
) The type of data disk: PD_SSD or PD_HDD. -
pricing_plan
- (Optional, First Generation) Pricing plan for this instance, can be one ofPER_USE
orPACKAGE
. -
replication_type
- (Optional) Replication type for this instance, can be one ofASYNCHRONOUS
orSYNCHRONOUS
. -
user_labels
- (Optional) A set of key/value user label pairs to assign to the instance.
The optional settings.database_flags
sublist supports:
The optional settings.backup_configuration
subblock supports:
-
binary_log_enabled
- (Optional) True if binary logging is enabled. Iflogging
is false, this must be as well. -
enabled
- (Optional) True if backup configuration is enabled. -
start_time
- (Optional)HH:MM
format time indicating when backup configuration starts.
The optional settings.ip_configuration
subblock supports:
-
ipv4_enabled
- (Optional) True if the instance should be assigned an IP address. The IPv4 address cannot be disabled for Second Generation instances. -
require_ssl
- (Optional) True if mysqld should default toREQUIRE X509
for users connecting over IP.
The optional settings.ip_configuration.authorized_networks[]
sublist supports:
-
expiration_time
- (Optional) The RFC 3339 formatted date time string indicating when this whitelist expires. -
name
- (Optional) A name for this whitelist entry. -
value
- (Optional) A CIDR notation IPv4 or IPv6 address that is allowed to access this instance. Must be set even if other two attributes are not for the whitelist to become active.
The optional settings.location_preference
subblock supports:
-
follow_gae_application
- (Optional) A GAE application whose zone to remain in. Must be in the same region as this instance.
The optional settings.maintenance_window
subblock for Second Generation instances declares a one-hour maintenance window when an Instance can automatically restart to apply updates. The maintenance window is specified in UTC time. It supports:
-
day
- (Optional) Day of week (1-7
), starting on Monday -
hour
- (Optional) Hour of day (0-23
), ignored ifday
not set -
update_track
- (Optional) Receive updates earlier (canary
) or later (stable
)
The optional replica_configuration
block must have master_instance_name
set to work, cannot be updated, and supports:
-
ca_certificate
- (Optional) PEM representation of the trusted CA's x509 certificate. -
client_certificate
- (Optional) PEM representation of the slave's x509 certificate. -
client_key
- (Optional) PEM representation of the slave's private key. The corresponding public key in encoded in theclient_certificate
. -
connect_retry_interval
- (Optional, Default: 60) The number of seconds between connect retries. -
dump_file_path
- (Optional) Path to a SQL file in GCS from which slave instances are created. Format isgs://bucket/filename
. -
failover_target
- (Optional) Specifies if the replica is the failover target. If the field is set to true the replica will be designated as a failover replica. If the master instance fails, the replica instance will be promoted as the new master instance. -
master_heartbeat_period
- (Optional) Time in ms between replication heartbeats. -
password
- (Optional) Password for the replication connection. -
sslCipher
- (Optional) Permissible ciphers for use in SSL encryption. -
username
- (Optional) Username for replication connection. -
verify_server_certificate
- (Optional) True if the master's common name value is checked during the SSL handshake.
Attributes Reference
In addition to the arguments listed above, the following computed attributes are exported:
-
first_ip_address
- The first IPv4 address of the addresses assigned. This is is to support accessing the first address in the list in a terraform output when the resource is configured with acount
. -
connection_name
- The connection name of the instance to be used in connection strings. -
ip_address.0.ip_address
- The IPv4 address assigned. -
ip_address.0.time_to_retire
- The time this IP address will be retired, in RFC 3339 format. -
self_link
- The URI of the created resource. -
settings.version
- Used to make sure changes to thesettings
block are atomic. -
server_ca_cert.0.cert
- The CA Certificate used to connect to the SQL Instance via SSL. -
server_ca_cert.0.common_name
- The CN valid for the CA Cert. -
server_ca_cert.0.create_time
- Creation time of the CA Cert. -
server_ca_cert.0.expiration_time
- Expiration time of the CA Cert. -
server_ca_cert.0.sha1_fingerprint
- SHA Fingerprint of the CA Cert.
Timeouts
google_sql_database_instance
provides the following Timeouts configuration options:
Import
Database instances can be imported using the name
, e.g.
$ terraform import google_sql_database_instance.master master-instance
© 2018 HashiCorpLicensed under the MPL 2.0 License.
https://www.terraform.io/docs/providers/google/r/sql_database_instance.html