2
votes

I'm trying to connect my app engine flex service to the cloud sql database that is in a shared project.

I have added the app engine default and app engine flex user as SQL admin to the shared project.

Now I'm trying to connect the app engine container to the cloud sql instance with the following connection string:

"Server=cloudsql;Database=cliche;User Id=user;Password=password;"

However, the connection always fails with the given connection string.

[13:57:37 ERR] An error occurred using the connection to database 'cliche' on server 'cloudsql'.
[13:57:37 ERR] An exception occurred while iterating over the results of a query for context type 'IKL.Data.ApplicationDbContext'.
 Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)

As requested I provide the app.yaml here:

runtime: aspnetcore
env: flex
service: platform
manual_scaling:
  instances: 1
resources:
  cpu: 2
  memory_gb: 1.5
  disk_size_gb: 10
beta_settings:
  cloud_sql_instances: shared-resources-291914:europe-west3:edutec-mssql=tcp:1433  
2
How are you configuring the Cloud SQL connexion with App Engine Flex? In the app.yaml? Can you share your app.yaml?guillaume blaquiere
I am using the app yaml for configuration, apologies for the lack of detail.Louis Campbell
Does the database have a public IP? Or is it only deployed on private IP?guillaume blaquiere
It is deployed on public ipLouis Campbell
Can you also share the piece of code where you configure and connect your database?guillaume blaquiere

2 Answers

3
votes

I found your issue! Your connexion string "Server=cloudsql;Database=cliche;User Id=user;Password=password;" isn't correct.

Cloud SQL proxy open a tunnel. one side of the tunnel is the Cloud SQL instance, the other is your local instance. Therefore you have to connect on the local host

"Server=127.0.0.1;Database=cliche;User Id=user;Password=password;"
0
votes

If you are using VPC Peering between your App Engine Flex project and your Cloud SQL project, then I must inform you that this is not possible to do yet, when you use an internal IP on your Cloud SQL, it will automatically use a VPC peering (different from the one you are probably using), and traffic between 3 VPC peerings is not posible.

You can use a VM in your Cloud SQL project to serve as a NAT server to route traffic onto your Cloud SQL there in order to workaround this, or you can use a Cloud VPN between your App Engine and Cloud SQL project, if possible please make sure that the NAT VM is in the same region as your Cloud SQL server or that the VPN on your SQL project is on the same region as your SQL server.

Also, please keep in mind that you would need to have the appropriate GCP firewall rules on all the involved GCP projects.