Reputation: 304
I'm using Google Cloud Composer v3 (Airflow 2) and need help with networking. I'm trying to connect Airflow DAGs running in Composer to a Cloud SQL PostgreSQL instance, which is accessed via Private Service Connect (PSC) in a shared VPC network.
Here's the setup:
I'm not sure how to establish a connection between the Airflow resources in the tenant project and Cloud SQL (via PSC) in the customer project.
What I’ve tried: I attempted to add the Composer environment to the same shared VPC in network config but seems create new network attachaments into private service connect, also I have tried without network in both case i got below error:
I wrote a Python DAG to connect to Cloud SQL using the Cloud SQL Python Connector but didn’t receive a response — it seems to hang indefinitely.
I tried to connect using the Cloud SQL Proxy with Airflow connexion with this script, I got the following error:
psycopg2.OperationalError: connection to server at "127.0.0.1", port 58149 failed: server closed the connection unexpectedly.
This probably means the server terminated abnormally before or while processing the request.
Any guidance or examples would be greatly appreciated! thank you.
Upvotes: 0
Views: 253
Reputation: 304
I share my solution:
I have used cloud sql proxy v2.13.0
If you’re working within an organization, you’ll need firewall rules for both ingress and egress, as follows:
direction = "INGRESS" allow { protocol = "tcp" ports = ["5432", "3307", "3306"] } source_ranges = ["YOUR_RANGE/16", "IP_ADDRESS_WORKER_COMPOSER_V3/10"]
direction = "EGRESS" allow { protocol = "tcp" ports = ["5432", "3307", "3306"] } destination_ranges= ["YOUR_RANGE/16", "IP_ADDRESS_WORKER_COMPOSER_V3/10"]
Additional Steps:
For those on free trial or without organization structure, this setup may differ slightly, but the above firewall rules are generally required into organization structure. You can find a detailed example in my GitHub repository: GitHub - Cloud SQL and Composer v3 with PSC. with free trial or without organization structure.
Please feel free to share any additional solutions or improvements. Your feedback is appreciated!
Upvotes: 0