You can connect to a Redis instance from the App Engine standard environment by using Serverless VPC Access.
Setup
If you have already installed the Google Cloud CLI and have created a Redis instance, you can skip these steps.
Install the gcloud CLI 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 App Engine app 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 app, and make sure the connector is attached to the Redis instance's authorized VPC network. Remember the name of the connector.
Sample application
This sample HTTP server application establishes a connection to a Redis instance from an App Engine standard environment app.
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/memorystore/redis
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/memorystore/redis
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/memorystore/redis
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/memorystore/redis
This sample application increments a Redis counter every time the /
endpoint
is accessed.
Go
This application uses the
github.com/gomodule/redigo/redis
client. Install it by running the following command:
go get github.com/gomodule/redigo/redis
Java
This application is Jetty 3.1 servlet-based.
It uses the Jedis library:
The AppServletContextListener
class is used to create a long-lived
Redis connection pool:
The VisitCounterServlet
class is a web servlet that increments
a Redis counter:
Node.js
This application uses the redis
module.
Python
This application uses Flask
for web serving and the redis-py
package to communicate with the Redis instance.
Preparing the application for deployment
To access the Redis instance, the App Engine app must be configured to use your Serverless VPC Access connector, and you must provide your Redis instance's connection details.
If you don't already have one, create an App Engine application.
Update the app's configuration to specify your Serverless VPC Access connector and the IP address and port of your Redis instance:
Go
Update the
gae_standard_deployment/app.yaml
file:See app.yaml Configuration File for more details.
Java
Update the
gae_standard_deployment/appengine-web.xml
file to specify your Serverless VPC Access connector:And update the
src/main/resources/application.properties
file with your Redis instance's IP address and port:For more information about configuring your app, see appengine-web.xml Reference.
Node.js
Update the
gae_standard_deployment/app.yaml
file:See app.yaml Configuration File for more details.
Python
Update the
gae_standard_deployment/app.yaml
file:See app.yaml Configuration File for more details.
Deploying the application to the App Engine standard environment
To deploy the application:
Copy necessary configuration files into the source directory:
Go
Copy the
app.yaml
andgo.mod
files into the source directory:cp gae_standard_deployment/{app.yaml,go.mod} .
Java
Copy the
appengine-web.xml
file into the source directory:mkdir -p src/main/webapp/WEB-INF cp gae_standard_deployment/appengine-web.xml src/main/webapp/WEB-INF/
Node.js
Copy the
app.yaml
file into the source directory:cp gae_standard_deployment/app.yaml .
Python
Copy the
app.yaml
file into the source directory:cp gae_standard_deployment/app.yaml .
Run the deploy command:
Go
gcloud app deploy
Java
mvn package appengine:stage gcloud app deploy target/appengine-staging/app.yaml
Node.js
gcloud app deploy
Python
gcloud app deploy
After the deployment is complete, the command will output the URL where you can visit your app. If you visit this URL, you will see the count on your Redis instance increase each time the page is loaded.