This Terraform configuration sets up a below listed GCP Services:
-
CloudSQL - PostgreSQL: The configuration provisions a CloudSQL - PostgreSQL instance using the
google_sql_database_instanceresource. It sets up the instance with specified settings such as tier, edition, activation policy, availability type, deletion protection, encryption, backup configuration, IP configuration, insights configuration, and more. -
Additional Databases: The configuration creates additional databases within the CloudSQL instance using the
google_sql_databaseresource. These databases are defined in thevar.additional_databasesvariable. -
Additional Users: The configuration creates additional users within the CloudSQL instance using the
google_sql_userresource. These users are defined in thevar.additional_usersvariable. Passwords for these users are randomly generated using therandom_passwordresource. -
IAM Users: The configuration creates IAM users within the CloudSQL instance using the
google_sql_userresource. These users are defined in thevar.iam_usersvariable. The type of user (CLOUD_IAM_SERVICE_ACCOUNT or CLOUD_IAM_USER) is determined based on theis_account_saattribute. -
Replicas: The configuration sets up replicas of the CloudSQL instance using the
google_sql_database_instanceresource. Replicas are defined in thevar.read_replicasvariable. Each replica has its own settings and can be configured with different characteristics.
Default - Single Database:
module "single-database" {
source = "nurdsoft/cloudsql-database/google"
version = "0.1.0"
project_id = "zeus-404008"
name = "example-csql"
random_instance_name = false
database_version = "POSTGRES_14"
zone = "us-central1-c"
region = "us-central1"
edition = "ENTERPRISE"
tier = "db-f1-micro"
user_name = "master"
user_password = ""
data_cache_enabled = false
deletion_protection = false
create_additional_databases = false
labels = {
platform_name = "demo"
cloud = "gcp"
component = "project"
environment = "dev"
client = "ns"
}
iam_users = [{
id = "Hello",
email = "hello@nurdsoft.co"
}, {
id = "Test",
email = "test@nurdsoft.co"
},
]
database_flags = [{
name = "cloudsql.iam_authentication",
value = true
}]
ip_configuration = {
ipv4_enabled = true
private_network = null
require_ssl = true
allocated_ip_range = null
authorized_networks = [{
name = "ns-priavte-vpc-cidr"
value = "10.0.0.0/16"
}] }
}Multiple Databases:
module "multiple-databases" {
source = "nurdsoft/cloudsql-database/google"
version = "0.1.0"
project_id = "zeus-404008"
name = "example-csql"
random_instance_name = false
database_version = "POSTGRES_14"
zone = "us-central1-c"
region = "us-central1"
edition = "ENTERPRISE"
tier = "db-f1-micro"
user_name = "master"
user_password = ""
data_cache_enabled = false
deletion_protection = false
create_additional_databases = false
additional_databases = [
{
name = "internal-dev"
charset = "UTF8"
collation = "en_US.UTF8"
}
]
labels = {
platform_name = "demo"
cloud = "gcp"
component = "project"
environment = "dev"
client = "ns"
app = "portal"
}
iam_users = [{
id = "Hello",
email = "hello@nurdsoft.co"
}, {
id = "Test",
email = "test@nurdsoft.co"
},
]
database_flags = [{
name = "cloudsql.iam_authentication",
value = true
}]
ip_configuration = {
ipv4_enabled = true
private_network = null
require_ssl = false
ssl_mode = "ENCRYPTED_ONLY"
allocated_ip_range = null
authorized_networks = [{
name = "ns-priavte-vpc-cidr"
value = "0.0.0.0/0"
}] }
}The project assumes the following:
- A basic understanding of Git.
- Git version
>= 2.33.0. - An existing GCP IAM user or role with access to create/update/delete resources defined in main.tf.
- GCloud CLI
>= 465.0.0 - A basic understanding of Terraform.
- Terraform version
>= 1.6.0 - (Optional - for local testing) A basic understanding of Make.
- Make version
>= GNU Make 3.81. - Important Note: This project includes a Makefile to speed up local development in Terraform. The
maketargets act as a wrapper around Terraform commands. As such,makehas only been tested/verified on Linux/Mac OS. Though, it is possible to install make using Chocolatey, we do not guarantee this approach as it has not been tested/verified. You may use the commands in the Makefile as a guide to run each Terraform command locally on Windows.
- Make version
Important Note: This project includes a Makefile to speed up local development in Terraform. The make targets act as a wrapper around Terraform commands. As such, make has only been tested/verified on Linux/Mac OS. Though, it is possible to install make using Chocolatey, we do not guarantee this approach as it has not been tested/verified. You may use the commands in the Makefile as a guide to run each Terraform command locally on Windows.
gcloud init # https://cloud.google.com/docs/authentication/gcloud
gcloud auth
make plan
make apply
make destroyContributions are always welcome. As such, this project uses the main branch as the source of truth to track changes.
Step 1. Clone this project.
# Using Git
$ git clone git@github.com:pacenthink/terraform-gcp-modules-cloudsql.git
# Using HTTPS
$ git clone https://github.com/pacenthink/terraform-gcp-modules-cloudsql.gitStep 2. Checkout a feature branch: git checkout -b feature/abc.
Step 3. Validate the change/s locally by executing the steps defined under Test.
Step 4. If testing is successful, commit and push the new change/s to the remote.
$ git add file1 file2 ...
$ git commit -m "Adding some change"
$ git push --set-upstream origin feature/abcStep 5. Once pushed, create a PR and assign it to a member for review.
- Important Note: It can be helpful to attach the
terraform planoutput in the PR.
Step 6. A team member reviews/approves/merges the change/s.
Step 7. Once merged, deploy the required changes as needed.
Step 8. Once deployed, verify that the changes have been deployed.
- If possible, please add a
planoutput using the feature branch so the member reviewing the MR has better visibility in the changes.
| Name | Version |
|---|---|
| 5.16.0 | |
| google-beta | 5.16.0 |
| null | 3.2.2 |
| random | 3.6.0 |
| Name | Version |
|---|---|
| 5.16.0 | |
| google-beta | 5.16.0 |
| null | 3.2.2 |
| random | 3.6.0 |
| time | n/a |
| Name | Type |
|---|---|
| google-beta_google_sql_database_instance.sql_database_instance | resource |
| google-beta_google_sql_database_instance.sql_database_instance_replica | resource |
| google_compute_global_address.private_ip_alloc | resource |
| google_service_networking_connection.private_vpc_connection | resource |
| google_sql_database.additional_databases | resource |
| google_sql_database.sql_database | resource |
| google_sql_user.additional_users | resource |
| google_sql_user.iam_account | resource |
| google_sql_user.sql_user | resource |
| null_resource.module_depends_on | resource |
| random_id.suffix | resource |
| random_password.additional_passwords | resource |
| random_password.user_password | resource |
| time_sleep.wait_90_seconds | resource |
| google_compute_zones.available | data source |
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| activation_policy | The activation policy for the master instance.Can be either ALWAYS, NEVER or ON_DEMAND. |
string |
"ALWAYS" |
no |
| additional_databases | A list of databases to be created in your cluster | list(object({ |
[] |
no |
| additional_users | A list of users to be created in your cluster. A random password would be set for the user if the random_password variable is set. |
list(object({ |
[] |
no |
| availability_type | The availability type for the master instance.This is only used to set up high availability for the PostgreSQL instance. Can be either ZONAL or REGIONAL. |
string |
"ZONAL" |
no |
| backup_configuration | The backup_configuration settings subblock for the database setings | object({ |
{} |
no |
| connector_enforcement | Enforce that clients use the connector library | bool |
false |
no |
| create_additional_databases | Wether A list of databases to be created in your cluster | bool |
true |
no |
| create_database | Whether to create the CloudSQL database | bool |
true |
no |
| create_service_networking_connection | Whether to create service networking connection | bool |
true |
no |
| create_timeout | The optional timout that is applied to limit long database creates. | string |
"30m" |
no |
| data_cache_enabled | Whether data cache is enabled for the instance. Defaults to false. Feature is only available for ENTERPRISE_PLUS tier and supported database_versions | bool |
false |
no |
| database_deletion_policy | The deletion policy for the database. Setting ABANDON allows the resource to be abandoned rather than deleted. This is useful for Postgres, where databases cannot be deleted from the API if there are users other than cloudsqlsuperuser with access. Possible values are: "ABANDON". | string |
null |
no |
| database_flags | The database flags for the master instance. See more details | list(object({ |
[] |
no |
| database_version | The database version to use | string |
n/a | yes |
| db_charset | The charset for the default database | string |
"" |
no |
| db_collation | The collation for the default database. Example: 'en_US.UTF8' | string |
"" |
no |
| db_name | The name of the default database to create | string |
"default" |
no |
| delete_timeout | The optional timout that is applied to limit long database deletes. | string |
"30m" |
no |
| deletion_protection | Used to block Terraform from deleting a SQL Instance. | bool |
true |
no |
| deletion_protection_enabled | Enables protection of an instance from accidental deletion across all surfaces (API, gcloud, Cloud Console and Terraform). | bool |
false |
no |
| deny_maintenance_period | The Deny Maintenance Period fields to prevent automatic maintenance from occurring during a 90-day time period. List accepts only one value. See more details | list(object({ |
[] |
no |
| disk_autoresize | Configuration to increase storage size. | bool |
true |
no |
| disk_autoresize_limit | The maximum size to which storage can be auto increased. | number |
0 |
no |
| disk_size | The disk size for the master instance. | number |
10 |
no |
| disk_type | The disk type for the master instance. | string |
"PD_SSD" |
no |
| edition | The edition of the instance, can be ENTERPRISE or ENTERPRISE_PLUS. | string |
null |
no |
| enable_default_db | Enable or disable the creation of the default database | bool |
true |
no |
| enable_default_user | Enable or disable the creation of the default user | bool |
true |
no |
| enable_random_password_special | Enable special characters in generated random passwords. | bool |
false |
no |
| encryption_key_name | The full path to the encryption key used for the CMEK disk encryption | string |
null |
no |
| follow_gae_application | A Google App Engine application whose zone to remain in. Must be in the same region as this instance. | string |
null |
no |
| iam_users | A list of IAM users to be created in your CloudSQL instance | list(object({ |
[] |
no |
| insights_config | The insights_config settings for the database. | object({ |
null |
no |
| internal_database_ip | Whether to create private IP allocation for Cloud SQL | bool |
true |
no |
| ip_configuration | The ip configuration for the master instances. | object({ |
{} |
no |
| labels | Map of labels for project | map(string) |
{} |
no |
| maintenance_window_day | The day of week (1-7) for the master instance maintenance. | number |
1 |
no |
| maintenance_window_hour | The hour of day (0-23) maintenance window for the master instance maintenance. | number |
23 |
no |
| maintenance_window_update_track | The update track of maintenance window for the master instance maintenance.Can be either canary or stable. |
string |
"canary" |
no |
| module_depends_on | List of modules or resources this module depends on. | list(any) |
[] |
no |
| name | The name of the Cloud SQL resources | string |
"" |
no |
| password_validation_policy_config | The password validation policy settings for the database instance. | object({ |
null |
no |
| pricing_plan | The pricing plan for the master instance. | string |
"PER_USE" |
no |
| private_ip_address | Private IP address for allocation | string |
null |
no |
| private_ip_prefix_length | Prefix length for private IP allocation | number |
20 |
no |
| project_id | The project ID to manage the Cloud SQL resources | string |
n/a | yes |
| random_instance_name | Sets random suffix at the end of the Cloud SQL resource name | bool |
false |
no |
| read_replica_deletion_protection | Used to block Terraform from deleting replica SQL Instances. | bool |
false |
no |
| read_replica_deletion_protection_enabled | Enables protection of replica instance from accidental deletion across all surfaces (API, gcloud, Cloud Console and Terraform). | bool |
false |
no |
| read_replica_name_suffix | The optional suffix to add to the read instance name | string |
"" |
no |
| read_replicas | List of read replicas to create. Encryption key is required for replica in different region. For replica in same region as master set encryption_key_name = null | list(object({ |
[] |
no |
| region | The region of the Cloud SQL resources | string |
null |
no |
| root_password | Initial root password during creation | string |
null |
no |
| secondary_zone | The preferred zone for the secondary/failover instance, it should be something like: us-central1-a, us-east1-c. |
string |
null |
no |
| tier | The tier for the master instance. | string |
null |
no |
| update_timeout | The optional timout that is applied to limit long database updates. | string |
"30m" |
no |
| user_deletion_policy | The deletion policy for the user. Setting ABANDON allows the resource to be abandoned rather than deleted. This is useful for Postgres, where users cannot be deleted from the API if they have been granted SQL roles. Possible values are: "ABANDON". | string |
null |
no |
| user_labels | The key/value labels for the master instances. | map(string) |
{} |
no |
| user_name | The name of the default user | string |
null |
no |
| user_password | The password for the default user. If not set, a random one will be generated and available in the generated_user_password output variable. | string |
"" |
no |
| zone | The zone for the master instance, it should be something like: us-central1-a, us-east1-c. |
string |
null |
no |
| Name | Description |
|---|---|
| additional_users | List of maps of additional users and passwords |
| dns_name | DNS name of the instance endpoint |
| generated_user_password | The auto generated default user password if not input password was provided |
| iam_users | The list of the IAM users with access to the CloudSQL instance |
| instance_connection_name | The connection name of the master instance to be used in connection strings |
| instance_first_ip_address | The first IPv4 address of the addresses assigned. |
| instance_ip_address | The IPv4 address assigned for the master instance |
| instance_name | The instance name for the master instance |
| instance_psc_attachment | The psc_service_attachment_link created for the master instance |
| instance_self_link | The URI of the master instance |
| instance_server_ca_cert | The CA certificate information used to connect to the SQL instance via SSL |
| instance_service_account_email_address | The service account email address assigned to the master instance |
| instances | A list of all google_sql_database_instance resources we've created |
| primary | The google_sql_database_instance resource representing the primary instance |
| private_ip_address | The first private (PRIVATE) IPv4 address assigned for the master instance |
| public_ip_address | Primay IPv4 address assigned for the master instance |
| read_replica_instance_names | The instance names for the read replica instances |
| replicas | A list of google_sql_database_instance resources representing the replicas |
| replicas_instance_connection_names | The connection names of the replica instances to be used in connection strings |
| replicas_instance_first_ip_addresses | The first IPv4 addresses of the addresses assigned for the replica instances |
| replicas_instance_self_links | The URIs of the replica instances |
| replicas_instance_server_ca_certs | The CA certificates information used to connect to the replica instances via SSL |
| replicas_instance_service_account_email_addresses | The service account email addresses assigned to the replica instances |
Module is maintained by Nurdsoft.
Apache 2 Licensed. See LICENSE for full details.