Using Memorystore with Cloud Run

Neil Kolban
Jul 21, 2020 · 3 min read
Image for post
Image for post

Google’s Memorystore service provides a fully managed Redis environment. Cloud Run provides a fully managed container hosting environment with automatic scaling. This opens up the opportunity to design solutions hosted by Cloud Run that leverage the services of Memorystore. In this article we will examine what is involved to set that up.

Whenever we create a Memorystore instance, it will be given a private IP address that will be visible to a single VPC network. Further, Memorystore only permits connections from resources that are contained within the same region. This provides immediate access to Memorystore from Compute Engines in the same region/VPC using VPC peering:

Image for post
Image for post

To access Memorystore from Cloud Run requires some additional steps. This is because Cloud Run is a managed serverless environment and isn’t associated with any specific VPCs such as the VPC with which Memorystore is peered. The solution to this is to use serverless VPC access.

We create a Serverless VPC Access connector in the same region as both Cloud Run and Memorstore and associated with the same VPC as Memorystore. With this in place, we can then instruct Cloud Run to leverage that connector to access Memorystore.

Image for post
Image for post

Let us now work through a sample that illustrates this story in action. First, we create an instance of Memorystore:

gcloud redis instances create redis1 --region us-central1

The instance will be called “redis1” and exist in the “us-central1” region. This command will take several minutes to complete. When the instance has been created, we will want to make note of the IP address that was allocated for us.

gcloud redis instances describe redis1 --region us-central1

Within the information presented, we will find an entry with the label “host”. Make note of the IP address as this is what we will use to connect with the Redis instance. Now we can create the VPC access connector:

gcloud compute networks vpc-access connectors \
create my-vpc-connector \
--network default \
--region us-central1 \
--range 10.8.0.0/28

This will create an access connector called “my-vpc-connector” associated with the default VPC network and the us-central1 region. The IP addresses for the connector will be taken from the CIDR range 10.8.0.0/28.

We can now create a Cloud Run hosted application that will leverage Redis. Here is an example:

const express = require('express');
const cors = require('cors')
const redis = require("redis");

const app = express();

const client = redis.createClient({
"host": process.env.REDIS_IP
});
client.on("error", function(error) {
console.error(error);
});

app.use(cors());

app.get('/', (req, res) => {
res.set('Cache-Control', 'no-store');
client.set("key", "value!", redis.print);
client.get("key", (err, reply) => {
res.send(`
<html>
<head>
</head>
<body>
<p>Connecting to Redis at: ${process.env.REDIS_IP}</p>
<p>Value of key just read: ${reply}</p>
</body>
</html>
`);
});
});

const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});

Notice the use of an environment variable called REDIS_IP that is expected to be provided. This will contain the IP address of the instance of Memorystore to be used.

We then build a container image (not shown). Once the container image has been built, we can deploy to Cloud Run:

gcloud run deploy cloud-run-app \
--image gcr.io/[PROJECT_ID]/cloud-run-app \
--max-instances 1 \
--platform managed \
--region us-central1 \
--vpc-connector my-vpc-connector \
--allow-unauthenticated \
--set-env-vars "REDIS_IP=[IP_ADDRESS]"

Note that we set the environment variable called REDIS_IP to be the IP address of the Redis instance. Note also that we supply the VPC connector we created earlier to provide the linkage between Cloud Run and Memorystore.

A GitHub repository is available here that contains the complete example just shown.

The following video shows a further illustration of the steps.

References:

Neil Kolban

Written by

IT specialist with 30+ years industry experience. I am also a Google Customer Engineer assisting users to get the most out of Google Cloud Platform.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Neil Kolban

Written by

IT specialist with 30+ years industry experience. I am also a Google Customer Engineer assisting users to get the most out of Google Cloud Platform.

Google Cloud - Community

A collection of technical articles and blogs published or curated by Google Cloud Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store