How to Connect Google Cloud Memorystore with App Engine Standard
By: Hasham Ali
Tags: Redis, Golang, App Engine, Google Cloud Platform

Last year, Google launched Memorystore as their managed Redis solution for the Google Cloud suite of products. Initially, connecting Memorystore to App Engine was only available via flexible environments. But just a few months ago, Google announced support for connecting any internal resource (e.g. Memorystore) to App Engine standard environments. The setup is quite straight forward but there were a few gotcha’s that I thought I’d highlight.

Getting Started

Before we begin, let’s set up the various resources:

Make sure your Memorystore and App Engine instances are in the same region. That region MUST be either us-central1 or europe-west1.

Gathering Details

For this demo, I’ll be using a Go 1.11 runtime. To begin with, my example app.yaml looks like this:

runtime: go111 handlers:

- url: /.*

script: auto

Next, let’s gather some important details on our Memorystore instance. Using the gcloud utility, I can get this information with the following command:

gcloud redis instances list --region YOUR_REGION

the key pieces of information from from the above command

The three key pieces of information to note are: host, port, and network.

Creating a VPC Access Connector

To allow App Engine to access our newly created Memorystore instance, we need to create a VPC Access Connector.

Go to your GCloud console and access the network connectors page. Click “Create connector” button at the top. Give your connector a descriptive name. Select the region your App Engine and Memorystore instances reside in (again, these can ONLY be us-central1 or europe-west1). Select the network you noted from your Memorystore instance above (in our case, this is “default”). Put in an IP range that isn’t taken in your network. For us, something like 10.9.0.0 works fine. Optionally put in a throughput min/max for your connector.

our newly generated connector

Now that our connector is generated, we need to let App Engine know that the connector is available. This is done by adding another entry into our app.yaml. We want to add the field “vpc_access_connector” which has one subfield: “name”. This subfield expects a value in the following format:

projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME

Our updated app.yaml now looks like this (replace PROJECT_ID with your own project ID):

runtime: go111 vpc_access_connector:

name: projects/PROJECT_ID/locations/us-central1/connectors/test-connector handlers:

- url: /.*

script: auto

Deployment

For the changes to take effect, the last step is to deploy our new app.yaml. But before we do that, let’s also set our Memorystore host and port (as noted in the Gathering Details section) as environment variables for our application code to access. Our final app.yaml then ends up like this:

runtime: go111 vpc_access_connector:

name: projects/PROJECT_ID/locations/us-central1/connectors/test-connector env_variables:

REDIS_HOST: "10.0.0.3"

REDIS_PORT: "6379" handlers:

- url: /.*

script: auto

To deploy, use the beta deploy command.

Note that you must use beta deploy command. The regular deploy command will ignore the access connector configuration.

gcloud beta app deploy

And that’s it! Your App Engine standard environment now has access to the Memorystore instance.

This setup has worked well for me so far, I’m hoping Google will open this up to other regions in the near future.

For more articles like this, consider following my personal website.