Skip to content

Terraform module for creating database and associated user on an existing CloudSQL instance

License

Notifications You must be signed in to change notification settings

sparkfabrik/terraform-google-gcp-mysql-db-and-user-creation-helper

Repository files navigation

Terraform module for creating database and associated user on an existing Google CloudSQL instance

This module creates database and users on an existing CloudSQL instance. The structure of the input variable is designed so that the database/user ratio is 1:1, so the module not only takes care of creating the database and its user, but also sets permissions on the user so that it has access to only the database for which it is responsible.

To enforce permissions, the module executes SQL commands with the mysql cli, which is therefore a prerequisite (it must be present in the filesystem where terraform apply is executed).

In addition, the script must be able to connect to the CloudSQL instance. In case this is not easily accessible from the terraform cli, the module is able to:

  1. Start an instance of CloudSQL Auth Proxy, for this purpose two null resources will be created for each user added to the database, enabling this option requires the presence of the proxy executable in the filesystem where terraform apply is executed.
  2. Connect from a CloudSQL Auth Proxy instance not present in the filesystem.

IMPORTANT

CloudSQL Auth Proxy needs the CloudSQL instance to expose a public IP address in order to connect to the instance itself.

Providers

Name Version
google >= 4.47.0
null >= 3.2.1
random >= 3.4.3

Requirements

Name Version
terraform >= 1.2
google >= 4.47.0
null >= 3.2.1
random >= 3.4.3

Inputs

Name Description Type Default Required
cloudsql_instance_name The name of the existing Google CloudSQL Instance name. Actually only a MySQL 5.7 or 8 instance is supported. string n/a yes
cloudsql_privileged_user_name The name of the privileged user of the Cloud SQL instance string n/a yes
cloudsql_privileged_user_password The password of the privileged user of the Cloud SQL instance string n/a yes
cloudsql_proxy_host The host of the Cloud SQL Auth Proxy; if a value other than localhost or 127.0.0.1 (default) is entered, it is assumed that there is a CloudSQL Auth Proxy instance defined and already configured outside this module, and therefore the proxy will not be launched. string "127.0.0.1" no
cloudsql_proxy_port Port of the Cloud SQL Auth Proxy string "1234" no
database_and_user_list The list with all the databases and the relative user. Please not that you can assign only a database to a single user, the same user cannot be assigned to multiple databases. user_host is optional, has a default value of '%' to allow the user to connect from any host, or you can specify it for the given user for a more restrictive access.
list(object({
user = string
user_host = optional(string, "%")
database = string
}))
n/a yes
project_id The ID of the project in which the resource belongs. string n/a yes
region The region in which the resource belongs. string n/a yes
terraform_start_cloud_sql_proxy If true terraform will automatically start the Cloud SQL Proxy instance present in the filesystem at the condition that cloudsql_proxy_host is set to a supported value. If false you have to start the Cloud SQL Proxy manually. This variable is used to prevent the creation of a Cloud SQL Proxy instance even if cloudsql_proxy_host has a supported value. bool true no

Outputs

Name Description
sql_users_creds The list of the created databases and the relative user username and password. You can use this output to connect to the relative database.

Resources

Name Type
google_sql_database.sql_database resource
google_sql_user.sql_user resource
null_resource.execute_cloud_sql_proxy resource
null_resource.kill_cloud_sql_proxy resource
random_password.sql_user_password resource
google_sql_database_instance.cloudsql_instance data source

Modules

No modules.

About

Terraform module for creating database and associated user on an existing CloudSQL instance

Resources

License

Stars

Watchers

Forks

Packages

No packages published