To set up a distributed in-memory data cache for your Cloud Run service, you can use Memorystore for Redis. With Memorystore for Redis, you use an in-memory Redis data store to store application cache with scale, security, and high availability. You can connect to a Redis instance directly from your Cloud Run service, but this require additional steps of setting up a Serverless VPC Access connector.
By using the Cloud Run integrations (Preview) described on this page, you can quickly connect your Cloud Run service to a new Redis cache using Memorystore without having to set up a Serverless VPC Access connector. If you want to connect your Cloud Run service to an existing redis instance, refer to Connecting to a Redis instance from a Cloud Run service.
Before you begin
- Make sure the Cloud Run service you are integrating with is already deployed.
- Review the Memorystore pricing page. You are billed for individual resources used in the integration.
Required roles
To use the Cloud Run integrations, you or your administrator must grant IAM roles on two different principals.
Click to view required roles for your Google Account
To get the permissions that you need to use Cloud Run integrations, ask your administrator to grant your Google Account the following IAM roles on your project:
- Cloud Run Developer (
roles/run.developer
) - Compute Viewer (
roles/compute.viewer
) - Serverless Integrations Developer (
roles/runapps.developer
) - Serverless Integrations Operator (
roles/runapps.operator
) - Service Account User (
roles/iam.serviceAccountUser
)
Click to view the required roles for the service account
To deploy the Cloud Run service, you can either use the auto-created Compute Engine default service account, or specify a user-managed service account. The service account must have the following roles:
- Cloud Run Developer (
roles/run.developer
) - Logs Bucket Writer (
roles/logging.bucketWriter
) - Service Account User (
roles/iam.serviceAccountUser
) - Storage Admin (
roles/storage.admin
) - Cloud Memorystore Redis Admin (
roles/redis.admin
) - Compute Viewer (
roles/compute.viewer
) - Serverless VPC Access Admin (
roles/vpcaccess.admin
)
Connect a service to a new Redis cache
Any configuration change leads to the creation of a new revision. Subsequent revisions will also automatically get this configuration setting unless you make explicit updates to change it.
You can connect a Cloud Run service to a Redis cache using the console or the command line. If you don't specify a memory size, a 1 gigabyte cache is used.
Console
In the Google Cloud console, go to the Cloud Run page.
Click the service in the services list that you want to use in this integration.
Click the Integrations tab.
Click Add integration.
Click Redis - Google Cloud MemoryStore.
Accept the defaults or change the name and cache size to values you want to use.
If prompted to enable a list of APIs, click Enable and wait for the APIs to be enabled.
Under Resources, note the new resources that will be created as a result of this integration.
Click Submit and wait for the integration and resources to be created. It can take up to 15 minutes.
gcloud
Update to the latest Google Cloud CLI:
gcloud components update
Create the integration:
gcloud beta run integrations create \ --type=redis \ --service=SERVICE \ --parameters=memory-size-gb=MEMORY-SIZE
Replace:
- SERVICE with your Cloud Run service name.
- MEMORY-SIZE with the size that you want in gigabytes of the cache. The default is 1 GB.
Optionally, include the
--service-account=SERVICE_ACCOUNT_EMAIL
flag.Replace SERVICE_ACCOUNT_EMAIL with the user-managed service account email address (
PROJECT_NUMBER-compute@developer.gserviceaccount.com
) that is used to deploy the service. Omit this flag if you want to use the default compute service account (SERVICE_ACCOUNT_ID@PROJECT_ID.iam.gserviceaccount.com
).To continue using the specified service account when deploying Cloud Run integrations, run:
gcloud config set runapps/deployment-service-account
Wait up to 15 minutes, during which time a fully configured Redis cache is created and connected. In addition, a new Cloud Run revision is created, including networking configuration and environment variables needed for the Redis cache. When the process is complete, the following message is shown:
[redis] integration [redis-xxx] has been updated successfully. To connect to the Redis instance, utilize the environment variables REDISHOST and REDISPORT. These have been added to the Cloud Run service for you.
You can check the status using
gcloud beta run integrations describe
Update Redis cache integrations
Updating an integration updates all of the Google Cloud resources associated with that integration. To update a Redis cache integration from your Cloud Run service:
Console
In the Google Cloud console, go to the Cloud Run page.
Click the service you want to update.
Click the Integrations tab.
Locate the Redis cache integration and click Edit.
When you are finished with updating the fields, click Update.
gcloud
Use the following command to get a list of available integrations:
gcloud beta run integrations list
Run the command to update the integration:
gcloud beta run integrations update INTEGRATION_NAME
Replace:
- INTEGRATION_NAME with the name of your Redis cache integration.
Optionally, you can use the following flag options:
Option Description --parameters
The memory size in gigabytes for the cache. --service-account
The user-managed service account email address to specify when updating the service. This service account overrides the previous service account that was used at deployment.
View Redis cache integrations
To view the current status of Redis cache integrations for your Cloud Run service:
Console
In the Google Cloud console, go to the Cloud Run page.
Click the service in the services list that you want to use in this integration.
Click the Integrations tab.
Locate the Redis cache integration you are interested in and click View details.
gcloud
Use the following command to get a list of available integrations:
gcloud beta run integrations list
Using a name from the returned list, display the details for it:
gcloud beta run integrations describe INTEGRATION_NAME
Replace INTEGRATION_NAME with the name of your Redis cache integration.
Delete Redis cache integrations
Deleting an integration will also delete all of the Google Cloud resources associated with that integration, but it will not delete your Cloud Run service.
To delete a Redis cache integration from your Cloud Run service:
Console
In the Google Cloud console, go to the Cloud Run page.
Click the service you want to delete an integration from.
Click the Integrations tab.
Locate the Redis cache integration you are interested in and click the ellipsis icon to the right of the integration, then click Delete.
gcloud
Use the following command to get a list of available integrations:
gcloud beta run integrations list
Using a name from the returned list, delete the integration:
gcloud beta run integrations delete INTEGRATION_NAME
Replace:
- INTEGRATION_NAME with the name of your Redis cache integration.
Optionally, you can use the following flag options:
Option Description --service-account
The user-managed service account email address to perform the delete operation.
Accessing Redis cache in your code
Note that as a result of completing the integration described on this page,
the environment variables REDISHOST
and REDISPORT
are automatically added
and available to be used by your Cloud Run service code.
Sample code for services using Redis
Refer to the Memorystore documentation for complete Cloud Run and Redis code samples in several languages that use Redis.
Limitations
- The Cloud Run integrations is only supported in certain regions. If you need to use a region that is not supported by the Cloud Run integrations, refer to Connecting to a Redis instance from a Cloud Run service.
- Only VPC connectors in the default VPC network are supported. Legacy networks are not supported.