So we all know that early in 2019, it was not impossible but hard to have a static IP for an application running on cloud run because it was all about the dynamic pool of IP addresses so if you need a static IP address to whitelist for connection to a database, it will be a workaround to do it which means maybe another point of failure but welcome to the announce, of what we called VPC connector 🎉, let‘s go straight to the command line
⚠️Don't forget to connect and set your GCP project⚠️
Let's first create a network ( you can use the default one if you want )
gcloud compute --project=hopauto-all networks create projectname_vpc --description="MY CRUN VPC" --subnet-mode=custom
Rq: I advise you guys to use names like project_servicename_env
Check if we created our network with this command
gcloud compute networks list
Let’s create the subnet
gcloud compute --project=projectname networks subnets create projectname-sub --network projectname_vpc --region=europe-west1 --range=10.20.0.0/28
For region and range, it is up to you and your work zone or app zone
let’s now go and create our famous VPC connector
gcloud compute networks vpc-access connectors create project-connector --region=europe-west1 --subnet-project=projectID --subnet=projectname-sub
And now the magic part, let’s just configure a NAT so we can route outbound requests to external endpoints through a static IP
1- Create a router for the NAT
gcloud compute routers create projectname-router --network peojectname-vpc --region europe-west1
2- Reserve a static IP
gcloud compute addresses create projectname-ip --region europe-west1
3- Create Nat’s configuration
cloud compute routers nats create projectname-nat --router projectname-router --region europe-west1 --nat-custom-subnet-ip-ranges=projectname-sub --nat-external-ip-pool=rojectname-ip
What else guys just deploy your cloudRun service adding the flags
--vpc-connector=project-connector \
--vpc-egress=all-traffic
Peace from Tunisia 🇹🇳✌️