Using gcp cloudrun with redis
TypeScriptTo deploy a service on Google Cloud Run with a Redis instance, we will create two main resources using Pulumi and the Google Cloud provider. The two resources are:
-
Google Cloud Run Service: This provides a managed platform for deploying containerized applications. It is serverless, meaning it abstracts away all infrastructure management tasks, allowing you to focus on building great applications without worrying about the underlying infrastructure.
-
Google Cloud Memorystore (Redis Instance): This is a fully managed in-memory data store service built on scalable, secure, and highly available infrastructure managed by Google. It is compatible with the Redis protocol, which means it behaves like a Redis instance, allowing applications to use it as a cache or in-memory data structure store.
Here's a detailed program written in TypeScript to create a Google Cloud Run service that interacts with a Redis instance:
import * as pulumi from '@pulumi/pulumi'; import * as gcp from '@pulumi/gcp'; // Define the configuration for our Redis instance. // We’re choosing a region, tier, and capacity here. const redisInstance = new gcp.redis.Instance("my-redis-instance", { memorySizeGb: 1, // The capacity of the Redis instance (in GB) tier: "BASIC", // The tier - here we're using the Basic tier region: "us-central1", // The GCP region in which the Redis instance should be created. displayName: "My Redis Instance", }); // Define the Cloud Run service. // We need to give it a name, an image to run, and a location. // We also provide the Redis instance details to the service as environment variables. const cloudRunService = new gcp.cloudrun.Service("my-cloudrun-service", { location: redisInstance.region, // Keep the Cloud Run service in the same region as our Redis instance template: { spec: { containers: [{ image: "gcr.io/my-project/my-image", // Specify the container image to deploy envs: [ { name: "REDIS_HOST", value: redisInstance.host, // Provide the Redis instance host as an environment variable }, { name: "REDIS_PORT", value: pulumi.interpolate`${redisInstance.port}`, // Provide the Redis instance port as an environment variable }, ], }], }, }, traffic: [{ percent: 100, latestRevision: true, }], }); // Export the URL of the Cloud Run service export const url = cloudRunService.status.url; // Export the Redis instance host and port to be used by applications export const redisHost = redisInstance.host; export const redisPort = redisInstance.port;
Let's walk through what this program does:
-
Import the necessary modules from
@pulumi/pulumi
and@pulumi/gcp
. -
Create a new Redis instance using
gcp.redis.Instance
, specifying its size, tier, and region. Note that Redis instances are regional resources, so the region should be chosen based on where the Cloud Run service is or where it is closest to other services that will interact with it. -
Define a Cloud Run service with
gcp.cloudrun.Service
. For the container image, you would use a Docker image stored in Google Container Registry that is configured to connect to a Redis instance. The environment variablesREDIS_HOST
andREDIS_PORT
are pulled from the Redis instance details and passed to the Cloud Run service, allowing the application in the container to connect to our Redis cache. -
Set the traffic configuration for the Cloud Run service to send 100% of traffic to the latest revision.
-
Lastly, export the URL of the deployed Cloud Run service and the host and port of the Redis instance. These exports are useful for connecting to the service and cache from outside the Google Cloud environment.
Before you run this program, ensure that you have the Pulumi CLI installed, a GCP project set up with billing enabled, and the necessary permissions to create resources within the project. Also, replace
"gcr.io/my-project/my-image"
with the path to your container image in the Google Container Registry.To deploy these resources, you would save this file as
index.ts
, runpulumi up
, and Pulumi would handle the resource provisioning based on the code.Remember to replace the placeholder values (like the image path) with actual values that match your application's needs.
-