<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cloud SQL &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/cloud-sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Thu, 08 Aug 2024 22:05:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Cloud SQL &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Cloud Run: Connecting to Cloud SQL with Private IP and SSL</title>
		<link>https://myshittycode.com/2024/08/08/cloud-run-connecting-to-cloud-sql-with-private-ip-and-ssl/</link>
					<comments>https://myshittycode.com/2024/08/08/cloud-run-connecting-to-cloud-sql-with-private-ip-and-ssl/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 08 Aug 2024 22:05:43 +0000</pubDate>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Cloud Run]]></category>
		<category><![CDATA[Cloud SQL]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Google Cloud Platform]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=2642</guid>

					<description><![CDATA[<p>There are various ways for a Cloud Run instance to connect to a Cloud SQL instance. Regardless of the solutions, if both resources reside in the same GCP organization, connecting using a Cloud SQL&#8217;s private IP with SSL/TLS encryption is highly recommended to prevent network traffic from going out to the internet. It is possible [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/08/08/cloud-run-connecting-to-cloud-sql-with-private-ip-and-ssl/">Cloud Run: Connecting to Cloud SQL with Private IP and SSL</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#cloud-sql-configuration">Cloud SQL Configuration</a></li><li><a href="#options">Options</a><ul><li><a href="#option-a-cloud-sql-language-connectors">Option A: Cloud SQL Language Connectors</a></li><li><a href="#option-b-managed-cloud-sql-proxy-in-cloud-run">Option B: Managed Cloud SQL Proxy in Cloud Run</a></li><li><a href="#option-c-manually-configure-cloud-sql-proxy">Option C: Manually Configure Cloud SQL Proxy</a></li><li><a href="#option-d-use-cloud-sq-ls-private-ip-and-client-certificate">Option D: Use Cloud SQL&#8217;s Private IP and Client Certificate</a></li></ul></li><li><a href="#recommendations">Recommendations</a><ul><li><a href="#best-solution-option-a">Best Solution &#8211; Option A</a></li><li><a href="#distance-next-best-solution-option-d">Distance Next Best Solution &#8211; Option D</a></li><li><a href="#least-favorite-solution-option-b-c">Least Favorite Solution &#8211; Option B &amp; C</a></li></ul></li></ul></nav></div>



<p>There are various ways for a Cloud Run instance to connect to a Cloud SQL instance. Regardless of the solutions, if both resources reside in the same GCP organization, connecting using a Cloud SQL&#8217;s private IP with SSL/TLS encryption is highly recommended to prevent network traffic from going out to the internet.</p>



<p>It is possible to have a Cloud SQL instance with both private and public IPs enabled, and there are legitimate reasons for doing so. For example, it allows the developers to connect to the Cloud SQL instance outside the GCP environment. However, utmost care is needed to ensure the private IP is always used when connecting from the Cloud Run instance.</p>



<p>This article evaluates the pros and cons of each solution and provides some recommendations.</p>



<h2 class="wp-block-heading" id="cloud-sql-configuration">Cloud SQL Configuration</h2>



<p>For consistency&#8217;s sake, SSL/TLS encryption is enabled on the Cloud SQL instance using client certificates.</p>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" width="1024" height="742" src="https://myshittycode.com/wp-content/uploads/2024/08/ssl-mode-1-1024x742.png?x45560" alt="Cloud SQL" class="wp-image-2651" style="width:800px" srcset="https://myshittycode.com/wp-content/uploads/2024/08/ssl-mode-1-1024x742.png 1024w, https://myshittycode.com/wp-content/uploads/2024/08/ssl-mode-1-300x217.png 300w, https://myshittycode.com/wp-content/uploads/2024/08/ssl-mode-1-768x557.png 768w, https://myshittycode.com/wp-content/uploads/2024/08/ssl-mode-1.png 1104w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>A client certificate is also created. </p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="1024" height="417" src="https://myshittycode.com/wp-content/uploads/2024/08/client-certificate-1-1024x417.png?x45560" alt="" class="wp-image-2650" style="width:800px" srcset="https://myshittycode.com/wp-content/uploads/2024/08/client-certificate-1-1024x417.png 1024w, https://myshittycode.com/wp-content/uploads/2024/08/client-certificate-1-300x122.png 300w, https://myshittycode.com/wp-content/uploads/2024/08/client-certificate-1-768x313.png 768w, https://myshittycode.com/wp-content/uploads/2024/08/client-certificate-1.png 1096w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong>Note:</strong> Some solutions, such as Cloud SQL Proxy and Cloud SQL Language Connectors, do not need the client certificate. So, these steps can be safely skipped. From the <a href="https://cloud.google.com/sql/docs/mysql/configure-ssl-instance#enforcing-ssl" target="_blank" rel="noopener">documentation</a>:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>With Cloud SQL Auth Proxy and Cloud SQL Connectors, client and server identities are also automatically verified regardless of the SSL mode setting.</p>
</blockquote>



<h2 class="wp-block-heading" id="options">Options</h2>



<h3 class="wp-block-heading" id="option-a-cloud-sql-language-connectors">Option A: Cloud SQL Language Connectors</h3>



<p>This solution uses <a href="https://cloud.google.com/sql/docs/mysql/connect-connectors" target="_blank" rel="noopener">Cloud SQL Language Connectors</a>, a programming language library maintained by Google.</p>



<p>The following configurations are <strong>NOT</strong> required:</p>



<ul class="wp-block-list">
<li>SSL certificates in the Cloud SQL instance</li>



<li>Cloud SQL volume mounting in the Cloud Run instance.</li>
</ul>



<p><strong>Python App</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import os

from google.cloud.sql.connector import Connector, IPTypes

# ... simplified for brevity

connector = Connector(IPTypes.PRIVATE)  # use Private IP

connector.connect(
  os.getenv(&#039;DB_INSTANCE_CONNECTION_NAME&#039;),  # project:region:instance
  &#039;pymysql&#039;,
  user=os.getenv(&#039;DB_USER&#039;),
  password=os.getenv(&#039;DB_PASS&#039;),
  db=os.getenv(&#039;DB_NAME&#039;),
)
</pre></div>


<p><strong>Pros</strong></p>



<ul class="wp-block-list">
<li>Very concise configuration in both IaC and application code since there&#8217;s no need to manage client certificates.</li>
</ul>



<p><strong>Cons</strong></p>



<ul class="wp-block-list">
<li>Only supports Python, Java, Go, and Node.js.</li>
</ul>



<h3 class="wp-block-heading" id="option-b-managed-cloud-sql-proxy-in-cloud-run">Option B: Managed Cloud SQL Proxy in Cloud Run</h3>



<p>This solution uses a <a href="https://cloud.google.com/sql/docs/mysql/connect-run#configure" target="_blank" rel="noopener">managed Cloud SQL Proxy</a> that is configurable directly in the Cloud Run instance. Once configured, a Cloud SQL volume mount is created in the Cloud Run instance. </p>



<p><strong>Python App</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import os

import pymysql

# ... simplified for brevity

pymysql.connect(
  database=os.getenv(&#039;DB_NAME&#039;),
  user=os.getenv(&#039;DB_USER&#039;),
  password=os.getenv(&#039;DB_PASS&#039;),
  unix_socket=f&#039;/cloudsql/{os.getenv(&quot;DB_INSTANCE_CONNECTION_NAME&quot;)}&#039;,  # /cloudsql/project:region:instance
)
</pre></div>


<p><strong>Pros</strong></p>



<ul class="wp-block-list">
<li>Very concise configuration in both IaC and application code since there&#8217;s no need to manage SSL certificates.</li>
</ul>



<p><strong>Cons</strong></p>



<ul class="wp-block-list">
<li><strong>It doesn&#8217;t work if the Cloud SQL instance has both public IP and private IP enabled</strong>. There&#8217;s no way to specify which IP to use, and Cloud SQL Proxy automatically uses the public IP if it exists.
<ul class="wp-block-list">
<li><strong>Note:</strong> If you know how to pull this off, please drop a comment below.</li>
</ul>
</li>



<li><a href="https://cloud.google.com/sql/docs/mysql/sql-proxy#production-environment" target="_blank" rel="noopener">Additional configurations</a> must be considered when running in production, which introduces many points of failure.</li>



<li>It may exceed the default <a href="https://cloud.google.com/sql/docs/mysql/connect-run#api-quota-limits" target="_blank" rel="noopener">Cloud SQL Admin API quota limit</a>.</li>
</ul>



<h3 class="wp-block-heading" id="option-c-manually-configure-cloud-sql-proxy">Option C: Manually Configure Cloud SQL Proxy</h3>



<p>This solution <a href="https://cloud.google.com/sql/docs/mysql/connect-auth-proxy" target="_blank" rel="noopener">manually configures Cloud SQL Proxy</a> via Dockerfile instead of relying on the managed Cloud SQL volume mount from Cloud Run.</p>



<p><strong>Dockerfile</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [13,14,16]; title: ; notranslate">
FROM python:3.12-alpine

RUN apk update &amp;amp;&amp;amp; apk add curl

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8080

RUN curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.12.0/cloud-sql-proxy.linux.amd64
RUN chmod +x cloud-sql-proxy

CMD &#x5B;&quot;sh&quot;, &quot;-c&quot;, &quot;./cloud-sql-proxy --private-ip --port 3306 ${DB_INSTANCE_CONNECTION_NAME} &amp;amp; sleep 5 &amp;amp;&amp;amp; python app.py&quot;]
</pre></div>


<p><strong>Python App</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import os

import pymysql

# ... simplified for brevity

pymysql.connect(
  host=os.getenv(&#039;DB_HOST&#039;),  # 127.0.0.1
  database=os.getenv(&#039;DB_NAME&#039;),
  user=os.getenv(&#039;DB_USER&#039;),
  password=os.getenv(&#039;DB_PASS&#039;),
)
</pre></div>


<p><strong>Pros</strong></p>



<ul class="wp-block-list">
<li>Very concise configuration in both IaC and application code since there&#8217;s no need to manage client certificates.</li>



<li>Unlike the managed Cloud SQL Proxy volume mount solution, this solution will work even if the Cloud SQL instance has both private and public IPs.</li>
</ul>



<p><strong>Cons</strong></p>



<ul class="wp-block-list">
<li>Need to manage the Cloud SQL Proxy installation and configuration in the Dockerfile.</li>



<li>All the cons described in the managed Cloud SQL Proxy solution above still apply here.</li>
</ul>



<h3 class="wp-block-heading" id="option-d-use-cloud-sq-ls-private-ip-and-client-certificate">Option D: Use Cloud SQL&#8217;s Private IP and Client Certificate</h3>



<p>Instead of relying on external tools/libraries, the application code connects directly to the Cloud SQL instance using the private IP where the client certificate is also provided.</p>



<p><strong>Python App</strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: python; title: ; notranslate">
import os

import pymysql

# ... simplified for brevity

pymysql.connect(
  host=os.getenv(&#039;DB_HOST&#039;), # Cloud SQL&#039;s Private IP
  database=os.getenv(&#039;DB_NAME&#039;),
  user=os.getenv(&#039;DB_USER&#039;),
  password=os.getenv(&#039;DB_PASS&#039;),
  ssl={
    &#039;ca&#039;: os.getenv(&#039;DB_SSL_SERVER_CA&#039;),     # /app/certs/ca/server-ca.pem
    &#039;cert&#039;: os.getenv(&#039;DB_SSL_CLIENT_CERT&#039;), # /app/certs/cert/client-cert.pem
    &#039;key&#039;: os.getenv(&#039;DB_SSL_CLIENT_KEY&#039;),   # /app/certs/key/client-key.pem
    &#039;check_hostname&#039;: False,
  }
)
</pre></div>


<p><strong>Note:</strong> The hostname checking must be disabled because the database host (Cloud SQL&#8217;s Private IP) will never<br>match the common name specified in the certificate. If the hostname checking is not disabled, it will produce the following error:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Error connecting to database: (2003, &#8220;Can&#8217;t connect to MySQL server on &#8217;10.x.x.x&#8217; ([SSL: CERTIFICATE_VERIFY_FAILED]<br>certificate verify failed: IP address mismatch, certificate is not valid for &#8217;10.x.x.x&#8217;. (_ssl.c:1000))&#8221;)</p>
</blockquote>



<p><strong>Pros</strong></p>



<ul class="wp-block-list">
<li>No additional wrappers like Cloud SQL Proxy or Cloud SQL Language Connectors are required.</li>
</ul>



<p><strong>Cons</strong></p>



<ul class="wp-block-list">
<li>Additional configurations and complexity on the IaC and application code are needed to manage and ingest the client certificate securely.</li>
</ul>



<h2 class="wp-block-heading" id="recommendations">Recommendations</h2>



<h3 class="wp-block-heading" id="best-solution-option-a">Best Solution &#8211; Option A</h3>



<p>Use Cloud SQL Language Connectors if your app uses one of the supported programming languages (Python, Java, Go, or Node.js). </p>



<p>It eliminates many unnecessary configurations on the IaC code and application code. In other words, there&#8217;s no need to do the following:</p>



<ul class="wp-block-list">
<li>Create a client certificate for Cloud SQL.</li>



<li>Store server certificate, client certificate, and client key in Secret Manager securely.</li>



<li>Mount files containing the server certificate, client certificate, and client key as volumes in the Cloud Run instance.</li>



<li>Pass certificate information to the application code via environment variables. </li>
</ul>



<p>None of the above is needed. </p>



<p>Less codebase = Less chance of errors.</p>



<h3 class="wp-block-heading" id="distance-next-best-solution-option-d">Distance Next Best Solution &#8211; Option D</h3>



<p>Connect directly using the Cloud SQL instance&#8217;s Private IP and client certificate.</p>



<p>It eliminates the need to rely on additional cloud configuration or external library (i.e., fewer points of failure). </p>



<p>However, it introduces additional configurations in IaC and application code to maintain and ingest the certificate described above.</p>



<h3 class="wp-block-heading" id="least-favorite-solution-option-b-c">Least Favorite Solution &#8211; Option B &amp; C</h3>



<p>While both Cloud SQL Proxy solutions (volume mount in the Cloud Run instance or installing it directly via Dockerfile) don&#8217;t require a client certificate, they come with <a href="https://cloud.google.com/sql/docs/mysql/sql-proxy#production-environment" target="_blank" rel="noopener">additional considerations and complexities when running in production</a>. </p>



<p>Furthermore, the volume mount solution won&#8217;t work if the Cloud SQL instance has both private and public IPs. </p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/08/08/cloud-run-connecting-to-cloud-sql-with-private-ip-and-ssl/">Cloud Run: Connecting to Cloud SQL with Private IP and SSL</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2024/08/08/cloud-run-connecting-to-cloud-sql-with-private-ip-and-ssl/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2642</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk (Request-wide modification query)

Served from: myshittycode.com @ 2026-02-19 23:01:46 by W3 Total Cache
-->