-
Notifications
You must be signed in to change notification settings - Fork 0
Troubleshooting Google Cloud Run
- Connection issue with MongoDB Atlas (Dedicated Tier) and Google Cloud Run
- Connection issue with MongoDB Atlas (Serverless Tier and Free/Shared Tier) and Google Cloud Run
- Environment Variables defined within Google Cloud Run are not available
This only works for
Dedicated Tier plans
andfree tier
orshared tier
may add0.0.0.0/0
to theNetwork Access
, but this may pose the security issue and is not the recommended solution.
Issue
Google Cloud Run is unable to connect to MongoDB.
Example of the common Error Message:
MongoNetworkError: connection 1 to 22.199.200.204:27017 closed
Cause
MongoDB Atlas does not allow the Network Access to the database. The CloudRun as serverless service does not have static IP address, but only dynamic IP address unless static IP is assigned through another Load Balancing or VPC Connector.
Solution
VPC peering between Google Cloud Platform and MongoDB Atlas and whitelist IP on MongoDB Atlas
VPC peering in MongoDB Atlas is a feature that allows you to connect Virtual Private Cloud (VPC) within a single Region. This allows you to securely and privately access your MongoDB Atlas cluster from resources within another VPC.
Initiate Peering in MongoDB Atlas
- Login to MongoDB Atlas web console.
- Navigate to the Security tab and click on the
Network Access
option on the left panel. - Click
Peering
tab and clickADD PEERING CONNECTION
in the top-left corner. - Click
Google Cloud
andNext
. - Add
Project ID
, which is found on the Google Cloud Console. - Add VPC Name of Google Cloud Console, which is found on the
GCP
>VPC network
>VPC networks
. The default name of VPC Name isdefault
. - The current Atlas CIDR is autofilled, but it is likely
192.168.0.0/16
. Then, Clickinitiate Peering
. - You may wait or comeback to copy and save the
Atlas GCP Project ID
,Atlas VPC Name
, andAtlas CIDR
for next steps.
Initiate Peering in Google Cloud Platform
- Go to Google Cloud Console Network Peering or
Google Cloud
>VPC network
>VPC network peering
. - Click
CREATE CONNECTION
>CONTINUE
. - Name the peering connection on Name input, such as
mongodb-atlas-peering
, on theName
. - Select
default
on the dropdown ofYour VPC network
. - Select
In another project
onPeered VPC network
. - Enter
Project ID
withAtlas GCP Project ID
, which you have copied from 'Initial Peering in MongoDB Atlas'. - Enter
VPC network name
withAtlas VPC Name
, which is also copied from previous steps. - Click
CREATE
and wait.
Whitelist Private IP ranges
GCP networks generated in auto-mode use a CIDR range of 10.128.0.0/9
- Go to
IP Access List
tab and clickADD IP ADDRESS
. - Add
10.128.0.0/9
IP Address ranges thenConfirm
.
Note: This method causes the cost besides to the Google Cloud Run
Issue
Google Cloud Run is unable to connect to MongoDB.
Example of the common Error Message:
MongoNetworkError: connection 1 to 22.199.200.204:27017 closed
Cause
The Cause is still same as above that 3 MongoDB Atlas does not allow the network access.
Solution
More Info: Static Outbound IP to CloudRun
As VPC peering only works on Dedicated tier plan on MongoDB Atlas, the static IP must be assigned to CloudRun through VPC connector.
Assign Static IP to CloudRun
- Create a sub-network
#gcloud compute networks subnets create SUBNET_NAME \
#--range=RANGE --network=NETWORK_NAME --region=REGION
gcloud compute networks subnets create example-subnet \
--range=10.124.0.0/28 --network=default --region=us-central1
- Create a Serverless VPC Access connector
# gcloud compute networks vpc-access connectors create CONNECTOR_NAME \
# --region=REGION \
# --subnet-project=PROJECT_ID \
# --subnet=SUBNET_NAME
gcloud compute networks vpc-access connectors create example-connector \
--region=us-central1 \
--subnet-project=example-project-id \
--subnet=example-subnet
- Configuring network address translation (NAT)
- Create a new Cloud Router
# gcloud compute routers create ROUTER_NAME \
# --network=NETWORK_NAME \
# --region=REGION
gcloud compute routers create example-router \
--network=default \
--region=us-central1
- Reserve Static IP
# gcloud compute addresses create ORIGIN_IP_NAME --region=REGION
gcloud compute addresses create example-ip-static --region=us-central1
- Create Cloud NAT gateway configs
# gcloud compute routers nats create NAT_NAME \
# --router=ROUTER_NAME \
# --region=REGION \
# --nat-custom-subnet-ip-ranges=SUBNET_NAME \
# --nat-external-ip-pool=ORIGIN_IP_NAME
gcloud compute routers nats create example-nat \
--router=example-router \
--region=us-central1 \
--nat-custom-subnet-ip-ranges=example-subnet \
--nat-external-ip-pool=example-ip-static
- Routing Cloud Run traffic through VPC network
IMAGE_URL can be found on the
Google Cloud Platform
>Cloud Run
clickservice-name
>REVISIONS
tab underCONTAINERS
asimage URL
# gcloud run deploy SERVICE_NAME \ # service name you have deployed with
# --image=IMAGE_URL \
# --vpc-connector=CONNECTOR_NAME \
# --vpc-egress=all-traffic
gcloud run deploy example-service-name \
--image=docker.pkg.dev/cloudrun/container/example-app:lastest \
--vpc-connector=example-connector \
--region=us-central1 \
--vpc-egress=all-traffic
Assign IP address to MongoDB Atlas
- Go to MongoDB Atlas.
- Go to
Network Access
>IP Access List
> ClickADD IP ADDRESS
. - Add IP address you have assigned from Google Cloud Platform, which can be
found under
VPC network
>IP addresses
, and then clickConfirm
.
Issue
After deploying to Google Cloud Run, the environment variables that must be publicly available, or accessible within client, are returning undefined.
Cause
Environment variables defined within Google Cloud Run are only available during the runtime, but not build time. Next.js may require some values from environment variables during the build process. If it does, this may cause the undefined environment variables after deploying to Google Cloud Run because the value cannot be accessed during the build time.
Solution
This issue can be resolved by implementing build time substitution of environment variables.
- Go to
Dockerfile
. - Add the substitution.
Build time substitution environment variable holds the value during the build time then insert the actual environment variable values defined within Google Cloud Run during the deployment phase.
...
# Build time substitution
# add prefix BUILD_
ARG BUILD_IMAGE_DOMAIN
# IMAGE_DOMAIN is the actual environment variable that is required during the
# the build time
ENV IMAGE_DOMAIN $BUILD_IMAGE_DOMAIN
...
- Add the build flag on the docker build command.
# IMAGE_DOMAIN is the environment variable defined within Google Cloud Run
docker build --build-arg BUILD_IMAGE_DOMAIN=$IMAGE_DOMAIN -t example-project:latest
- [Optional] Semi-automate the process.
This process is automated with
deploy.sh
as long asGCR_
prefix is added.
Although It is still manual to define the build time environment variable within Dockerfile,
deploy.sh
will automatically pick up the environment variables with prefixGCR_
and add them into Google Cloud Run's environment variable, but also make them available during the build time
# ARG BUILD_IMAGE_DOMAIN
# ENV IMAGE_DOMAIN $BUILD_IMAGE_DOMAIN
ARG BUILD_IMAGE_DOMAIN
ENV GCR_IMAGE_DOMAIN $BUILD_IMAGE_DOMAIN