Skip to content

Repository files navigation

cloud-sql-proxy

TFSec Security Checksterraform-docsauto-release

Overview

A Terraform module that builds out a Compute Engine VM with the Cloud SQL Proxy installed as well as the required IAP configs to allow a developer to access a Cloud SQL database from their workstation 🤖

Diagram of infrastructure deployed via this module to support remote Cloud SQL connections

To connect to the Cloud SQL instance once the infrastructure has been applied you need to open a tunnel to the VM using the following example command:

gcloud compute start-iap-tunnel cloudsqlproxy-695b 3306 --local-host-port=localhost:3306 --zone=australia-southeast1-c

Replace the VM name and both ports with the relevant ports for your environment.

The above example creates an IAP tunnel to the VM named cloudsqlproxy-695b on port 3306 using the local host port 3306. The Cloud SQL proxy listens on whichever port is relevant to the database you're connecting to (in this example we're connecting to a MySQL Cloud SQL instance, so the Cloud SQL Proxy automatically listens on port 3306).

Once the IAP tunnel has been brought up you are able to connect to the Cloud SQL instance on localhost:3306. Keep in mind that the terminal session where the tunnel is open must remain open so you'll need to open another terminal session to run your SQL queries.

For more information, please refer to the Cloud SQL Proxy documentation here.

Assumptions

  • You have a Cloud SQL instance already created
  • You have a VPC network and subnetwork already created

Troubleshooting

If you are having issues connecting to the Cloud SQL instance via the proxy, check the startup script logs on the VM to see if there are any errors. You can do this by running the following command on the instance:

sudo journalctl -u google-startup-scripts.service

Example

module"proxy" {
source="git::https://github.com/withriley/cloud-sql-proxy.git"project="gcp-project"region="australia-southeast1"db_name="mysql-instance"network="vpc-network"subnetwork="vpc-subnet"create_firewall_rule=trueadditional_ports=["3306"]
members=["user:peter.griffin@withriley.com"]
}

Resources

NameType
google_compute_firewall.defaultresource
google_compute_instance.defaultresource
google_iap_tunnel_instance_iam_binding.enable_iapresource
google_project_iam_member.projectresource
google_project_service.defaultresource
google_service_account.defaultresource
random_id.defaultresource
random_shuffle.defaultresource
google_compute_subnetwork.defaultdata source
google_compute_zones.availabledata source
google_sql_database_instance.defaultdata source

Modules

No modules.

Inputs

NameDescriptionTypeDefaultRequired
additional_portsAdditional ports to open for IAP tunnelling - only used if create_firewall_rule is truelist(string)[]no
cloud_sql_proxy_versionVersion of the Cloud SQL Proxy to downloadstring"v2.7.2"no
create_firewall_ruleBoolean to enable/disable firewall rule creation for IAP tunnellingboolfalseno
db_nameCloud SQL instance database namestringn/ayes
host_projectProject where the firewall rule + VM will be created if using Shared VPC - if not using Shared VPC leave blankstring""no
membersList of the members to grant IAM permissions for IAP tunnelling. Format: user:, group:, etc.list(string)n/ayes
networkName of the networkstringn/ayes
projectProject name where the resources are deployedstringn/ayes
psc_connectionSet this to true to connect to your Cloud SQL instance via Private Service Connect. default will connect to SQL via private IP.boolfalseno
regionRegion to deploy the resources tostringn/ayes
subnetworkName of the subnetworkstringn/ayes

Outputs

No outputs.

About

No description, website, or topics provided.

Resources

Code of conduct

Security policy

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages