You can connect to a Redis instance from Cloud Functions by using Serverless VPC Access. Your functions must be in the same region as the Redis instance.
Setup
If you have already installed the Cloud SDK and have created a Redis instance, you can skip these steps.
Install the Cloud SDK and initialize:
gcloud init
Follow the Quickstart Guide to create a Redis instance. Take note of the zone, IP address, and port of the Redis instance.
Configuring Serverless VPC Access
To connect from your Cloud Functions to your Redis instance's authorized VPC network, you must set up Serverless VPC Access.
Find your Redis instance's authorized network by running the command:
gcloud beta redis instances describe [INSTANCE_ID] --region [REGION]
Follow the instructions at Creating a connector to create a Serverless VPC Access connector. Make sure you create the connector in the same region as your function and your Redis instance, and make sure the connector is attached to the Redis instance's authorized VPC network. Remember the name of the connector.
Sample function
This sample function establishes a connection to a Redis instance from Cloud Functions.
Clone the repository for your desired programming language and navigate to the folder that contains the sample code:
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples
cd golang-samples/functions/memorystore/redis
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/functions/memorystore/redis
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/functions/memorystore/redis
The sample code increments a Redis counter every time the function is triggered.
Go
This function uses the
github.com/gomodule/redigo/redis
client.
Node.js
This function uses the redis
module.
Python
This function uses the redis-py
package.
Deploying the sample to Cloud Functions
Deploy the function using the gcloud
command-line tool:
Go
gcloud functions deploy VisitCount \ --runtime go111 \ --trigger-http \ --region [REGION] \ --vpc-connector projects/[PROJECT_ID]/locations/[REGION]/connectors/[CONNECTOR_NAME] \ --set-env-vars REDISHOST=[REDIS_IP],REDISPORT=[REDIS_PORT]
Node.js
gcloud functions deploy visitCount \ --runtime nodejs10 \ --trigger-http \ --region [REGION] \ --vpc-connector projects/[PROJECT_ID]/locations/[REGION]/connectors/[CONNECTOR_NAME] \ --set-env-vars REDISHOST=[REDIS_IP],REDISPORT=[REDIS_PORT]
Python
gcloud functions deploy visit_count \ --runtime python37 \ --trigger-http \ --region [REGION] \ --vpc-connector projects/[PROJECT_ID]/locations/[REGION]/connectors/[CONNECTOR_NAME] \ --set-env-vars REDISHOST=[REDIS_IP],REDISPORT=[REDIS_PORT]
where:
[PROJECT_ID]
is your Google Cloud project's ID.[REGION]
is the same region where your Serverless VPC Access connector and your Redis instance are located.[CONNECTOR_NAME]
is the name of your connector.[REDIS_IP]
and[REDIS_PORT]
are the IP address and port number of your Redis instance.
After the function deployment finishes, see the visit count increase by
sending a GET
request to your function's URL:
Go
https://[REGION]-[PROJECT_ID].cloudfunctions.net/VisitCount
Node.js
https://[REGION]-[PROJECT_ID].cloudfunctions.net/visitCount
Python
https://[REGION]-[PROJECT_ID].cloudfunctions.net/visit_count