Context
I’m developing a NextJS 14 application using google services.
I also use Prisma as my ORM, which works by having a DATABASE_URL (postgresql://postgres:password*^@IPaddress/dbname?schema=public"
)
Let me walk through my configuration.
- I have a VPC network enabled, its called default and it has the basic configuration.
- My SQL instance has private and public IP enable. For the private IP it is connected to my previous default VPC. Below It is how the networking tab looks like in my postgres instance
- Im my cloud Run instance I have again basic config. I set the Cloud SQL connections, by selecting my instance in the dropdown.
I also have the correct env variables using the Private IP
(provided in my SQL instance overview page) in the DATABSE_URL
variable (needed by Prisma). Below is how my networking tab looks like .
- This is how the YAML in my trigger looks like. I had to pass here all my .env variables again because nextJS needs them for the build process. I’m also adding this line
'--vpc-connector=default'
steps:
- name: gcr.io/cloud-builders/docker
args:
- build
- '--no-cache'
- '-t'
- >-
$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
- .
- '-f'
- Dockerfile
- '--build-arg'
- NEXT_PUBLIC_URL=$_NEXT_PUBLIC_URL
- '--build-arg'
- DATABASE_URL=$_DATABASE_URL
- '--build-arg'
- RESEND_API_KEY=$_RESEND_API_KEY
- '--build-arg'
- JWT_SECRET_KEY=$_JWT_SECRET_KEY
- '--build-arg'
- GOOGLE_PROJECT_ID=$_GOOGLE_PROJECT_ID
- '--build-arg'
- BUCKET_NAME=$_BUCKET_NAME
- '--build-arg'
- GOOGLE_API_KEY=$_GOOGLE_API_KEY
- '--build-arg'
- GOOGLE_CLIENT_EMAIL=$_GOOGLE_CLIENT_EMAIL
- '--build-arg'
- NEXT_PUBLIC_RECAPTCHA_SITE_KEY=$_NEXT_PUBLIC_RECAPTCHA_SITE_KEY
- '--build-arg'
- RECAPTCHA_SECRET_KEY=$_RECAPTCHA_SECRET_KEY
- '--build-arg'
- GOOGLE_DB_INSTANCE=$_GOOGLE_DB_INSTANCE
- '--build-arg'
- GOOGLE_DB_PASSWORD=$_GOOGLE_DB_PASSWORD
- '--build-arg'
- GOOGLE_DB_CONNECTION_NAME=$_GOOGLE_DB_CONNECTION_NAME
- '--build-arg'
- GOOGLE_DB_NAME=$_GOOGLE_DB_NAME
- '--build-arg'
- GOOGLE_DB_USERNAME=$_GOOGLE_DB_USERNAME
id: Build
- name: gcr.io/cloud-builders/docker
args:
- push
- >-
$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
id: Push
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk:slim'
args:
- run
- services
- update
- $_SERVICE_NAME
- '--platform=managed'
- >-
--image=$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
- >-
--labels=managed-by=gcp-cloud-build-deploy-cloud-run,commit-sha=$COMMIT_SHA,gcb-build-id=$BUILD_ID,gcb-trigger-id=$_TRIGGER_ID
- '--region=$_DEPLOY_REGION'
- '--vpc-connector=default'
- '--quiet'
id: Deploy
entrypoint: gcloud
images:
- >-
$_AR_HOSTNAME/$PROJECT_ID/cloud-run-source-deploy/$REPO_NAME/$_SERVICE_NAME:$COMMIT_SHA
options:
substitutionOption: ALLOW_LOOSE
logging: CLOUD_LOGGING_ONLY
substitutions:
_AR_HOSTNAME: southamerica-west1-docker.pkg.dev
_PLATFORM: managed
_SERVICE_NAME: ******
_TRIGGER_ID: ******
REPO_NAME: ******
_DEPLOY_REGION: southamerica-west1
tags:
- gcp-cloud-build-deploy-cloud-run
- gcp-cloud-build-deploy-cloud-run-managed
- ******
The Problem
I get the error that It cannot communicate with the database
Can't reach database server at 172.18.160.4:5432
Which is the private IP and we are in cloud Run.
Hopefully, someone can help and figure out why I cannot reach my database
Below are the logs