Connect Firebase App Hosting to a VPC network Stay organized with collections Save and categorize content based on your preferences.
YourFirebase App Hosting backend can connect to aVirtual Private Cloud(VPC) network. This allows yourFirebase App Hosting backend to access backend services not accessible usingpublic IP addresses, such as Cloud SQL, Spanner, Cloud Memorystore,Compute Engine, or Kubernetes internal microservices.
VPC access is only available at runtime (from yourCloud Runcontainer), not at build time (Cloud Build).
Choose how to connect to a VPC network
- Direct VPCEgress:Simpler, faster, and less expensive. Uses one IP address per container.Recommended for most use cases.
- ServerlessConnectors:Pools IP addresses for larger applications. Requires payment for theunderlying VM. See "Serverless VPC Access" in theVPC pricing pagefor pricing details.
Configure inapphosting.yaml
Use thevpcAccess mapping in yourapphosting.yaml file to configure access.Use either a fully qualified network/connector name or an ID. Using IDs allowsfor portability between staging and production environments with differentconnectors/networks.
Direct VPC Egress Configuration (apphosting.yaml):
runConfig:vpcAccess:egress:PRIVATE_RANGES_ONLY# Default valuenetworkInterfaces:# Specify at least one of network and/or subnetwork-network:my-network-idsubnetwork:my-subnetwork-idServerless Connector Configuration (apphosting.yaml):
runConfig:vpcAccess:egress:ALL_TRAFFICconnector:connector-idExample: connect to Memorystore for Redis from a Next.js app
Caching systems like Redis or Memcached are commonly used to build a fast datacaching layer for an app. This example shows you how to set upMemorystore for Redisin the sameGoogle Cloud project as yourFirebase App Hosting backend andconnect to it usingDirect VPC egress.
Step 0: Create a Memorystore for Redis instance
Note: you may also be prompted to create aservice connection policyas part of this setup.- Go to theMemorystore for Redis page in theGoogle Cloud console.
- Make sure the same project you're using forFirebase App Hosting isselected.
- If you can't access this page, make sure billing is enabled for yourproject and that you've enabled theMemorystore API.
- SelectCreate Instance.
- Configure the new instance with your preferred settings. Here are someexample values you can use:
- Enter
my-redis-cacheunderInstance ID. - Enter
Redis cacheunderDisplay name. - ChooseBasic under the tier selector. Basic tier designates astandalone Redis node, as opposed to standard tier, which uses areplica node to backup your data.
- Choose yourApp Hosting backend's region from theRegionselector.Be sure to set this value to match the region of your backend.
- Chooseany from the zone selector.
- Enter
5underCapacity. This sets your instance capacity to 5 GB. - Select
5.0underVersion (recommended). - Choosedefault from theAuthorized network selector.
- Enter
Step 1: Updateapphosting.yaml with your VPC network ID
- Visit theVPC networks page in theGoogle Cloud console.
- Find the VPC network ID for your Memorystore for Redis instance (it willoften be
default). Set direct VPC egress configuration in
apphosting.yamlusing the VPCnetwork ID:runConfig:vpcAccess:egress:PRIVATE_RANGES_ONLY# Default valuenetworkInterfaces:-network:my-network-id
Step 2: Add environment variables that direct your app to Redis
- Find connection information (host and port) in the "Connections" tab of yourMemorystore for Redis instance in theGoogle Cloud console.
Connect to Redis with
REDISPORTandREDISHOSTenvironment variables. Setthese inapphosting.yamlusing the host and port values from theGoogle Cloud console:env:# Sample only. Use actual values provided by Memorystore-variable:REDISPORTvalue:6379-variable:REDISHOSTvalue:10.127.16.3
Step 3: Use redis from your app
Install theredis npm package:
npm install redis@latestAccess your redis cache from your code. Use the environment variablesconfigured in the previous step. For example, here's how you might read froma cache in a Next.js route handler:
src/lib/redis.jsimport{createClient}from"redis";// Set these environment variables in apphosting.yamlconstREDISHOST=process.env.REDISHOST;constREDISPORT=process.env.REDISPORT;letredisClient;exportasyncfunctiongetClient(req,res){// Only connect if a connection isn't already availableif(!redisClient){redisClient=awaitcreateClient(REDISPORT,REDISHOST).on("error",(err)=>console.error("Redis Client Error",err)).connect();}returnredisClient;}src/app/counter/route.jsimport{getClient}from"@/lib/redis.js";exportasyncfunctionGET(request){constredisClient=awaitgetClient();constcount=awaitredisClient.get("counter");returnResponse.json({count});}exportasyncfunctionPOST(request){constredisClient=awaitgetClient();constcount=awaitredisClient.incr("counter");returnResponse.json({count});}
Step 4 (optional): Configure your app for local development
TheFirebase App Hosting emulator can override values usingapphosting.emulator.yaml. Here, you can change the value ofREDISHOST topoint to the localhost so that you can develop locally using a localinstallation of Redis.
- Install Redis on your local machine
Create or edit
apphosting.emulators.yamlto reference your local instance:env:-variable:REDISHOSTvalue:127.0.0.1
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-02-05 UTC.