2

My ASP NET CORE Service is running on Google Cloud as App Engine Flexible. PostgreSQL database is running on SQL Cloud as well. Service connect to db and get data.

If I run service on localhost and test api from browser or Fidler it's works correct. When service is running in cloud but api response is mock it's also works correct. But if service need to connect do database it's get HTTP ERROR 500. No more additional information about error.

Maybe somone have any idea why it's works like in description?

Connection String looks like:

"ConnectionString": "Server=11.11.11.11;Port=1111;Database=namedb;;User Id=user;Password=pass;Keepalive=1;",

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Fijal
  • 43
  • 4
  • How is cloud sql configured ? remember that by default cloud sql denies connections from any host, you probably allowed your localhost for test/development purpose, right ? – Pievis Jan 02 '20 at 22:54
  • At the begin yes, to connect to SQL cloud from pgadmin. But to test I delete all allows host. From mobilephone it's works the same, no connect to database - correct, with connection to db http 500. – Fijal Jan 02 '20 at 23:35
  • well then, you have to whitelist your app engine app in order to perform authenticated calls to your cloud sql instance :) try to whitelist app engine ip range and see if it works, if that is the problem I would suggest you to improve your connection security with a certificate – Pievis Jan 02 '20 at 23:38
  • I think i tried IT. I get IP of working istance of service and add it to whitelist. It's correct way about your advice? – Fijal Jan 02 '20 at 23:42
  • App Engine does not currently provide a way to map static IP addresses to an application. In order to optimize the network path between an end user and an App Engine application, end users on different ISPs or geographic locations might use different IP addresses to access the same App Engine application. DNS might return different IP addresses to access App Engine over time or from different network locations. https://cloud.google.com/appengine/kb/ – Pievis Jan 03 '20 at 00:02

1 Answers1

0

To avoid whitelisting the IPs which is a titanic labor there are two ways to connect from App Engine Flex where you won't need to do this.

One way: is to use the beta settings and connect to the local address. This way is good if you are connecting to only one DB for this you need to add the following lines to the app.yaml

beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<PORT>

Where INSTANCE_CONNECTION_NAME is ::

for example:

beta_settings:
  cloud_sql_instances: my-project:my-region:my-instance=tcp:5432

The other way: is to connect by private IPs. For this you need to have the App Engine and the CloudSQL on the same region. and add the following lines to your app.yaml

   network:
       name: projects/<HOST_PROJECT_ID>/global/networks/<NETWORK_NAME>

for example

network:
  name: projects/my-project/global/networks/default
Soni Sol
  • 2,367
  • 3
  • 12
  • 23
  • INSTANCE_CONNECTION_NAME is like region:xxx:region or only xxx from this? Port in tcp: is 5432 for PostgreSQL? And where I can find HOST_PROJECT_ID and NETWORK_NAME from network: name: projects//global/networks/ – Fijal Jan 03 '20 at 15:46
  • are you trying the first or the second option? and could you share the error you are getting? – Soni Sol Jan 03 '20 at 18:20
  • Both. I shared it in answer. In my blind shot I set private addres IP in my SQL Cloud, it could be problem now? – Fijal Jan 03 '20 at 19:06
  • is your Cloud SQL internal IP in the same network as the network declared in the app.yaml? – Soni Sol Jan 03 '20 at 19:13
  • Yes, it is default network. In SQL Cloud Overview it is field named Associated networking, yes? – Fijal Jan 03 '20 at 19:33
  • could you verify if the database is accessible by internal IP with CloudShell? to do this you can follow https://cloud.google.com/sql/docs/postgres/connect-compute-engine – Soni Sol Jan 03 '20 at 20:04
  • Yes it is, I manage db from pgAdmin, create table and add data to it. (Maybe we can discuss in other communicator to faster communicate and then add solving answear here?) – Fijal Jan 03 '20 at 20:06
  • I have to use Public or Private Ip addres in ConnectionString in asp net core service? – Fijal Jan 03 '20 at 20:16
  • use Private IP when adding the network to your app.yaml – Soni Sol Jan 03 '20 at 21:37
  • I tried this and still same error. Network could be "default" or must be like addres?Could we talk for example on Skype to show you my screen or sth to faster find solution? – Fijal Jan 03 '20 at 21:39
  • José Soní Thank you very much! Your solution is 100% correct! I'm ashamed to admit it but I miss one letter in ConnectionString, but very importatnt :( – Fijal Jan 03 '20 at 22:35