Stay organized with collectionsSave and categorize content based on your preferences.
Create a Memorystore for Redis Cluster instance using Terraform
This quickstart shows you how to use Terraform to create a new
Memorystore for Redis Cluster instance. It also shows you how to connect to the instance
using telnet.
This quickstart assumes you are starting a new Terraform file, and
includes the Terraform resource for setting up the Google Cloud provider. However, it omits
the steps for authenticating your Terraform file with the Google Cloud provider.
Sign in to your Google Cloud account. If you're new to
Google Cloud,create an accountto evaluate how our products perform in
real-world scenarios. New customers also get $300 in free credits to
run, test, and deploy workloads.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
CLUSTER_IDis your chosen ID of the Memorystore for Redis Cluster instance you're creating. The ID must be 1 to 63 characters and use only lowercase letters, numbers, or hyphens. It must start with a lowercase letter and end with a lowercase letter or number.
POLICY_NAMEis your chosen name for the Private Service Connect service connection policy for your project. For more information about the service connection policy, seeNetworking.
SUBNET_IDis your chosen ID of the subnet created in this example that is used by the Memorystore cluster.
NETWORK_IDis your chosen ID of the network created in this example that is used by the Memorystore cluster.
Deploy the Terraform configuration file
Runterraform init.
Runterraform plan.
Runterraform apply.
Connecting to the instance from a Compute Engine VM
Next, connect to the newly created Memorystore for Redis Cluster instance.
You can connect to the instance from any Compute Engine VM that uses the
instance's authorized network.
If you don't already have a Compute Engine VM that uses that same authorized network as your instance, create one and connect to it by followingQuickstart using a Linux VM.
Installtelnetusingapt-get:
sudo apt-get install telnet
From the terminal, telnet to the IP address of the instance, replacingVARIABLESwith appropriate values.
To avoid incurring charges to your Google Cloud account for the resources used
in this quickstart, delete the Redis instance using the following steps:
Remove thegoogle_redis_instanceresource from your Terraform configuration
file.
Run Terraforminit,plan, andapplyto destroy the Redis resource.
Deleteany Compute Engine VMs you created for this quickstart.
Configure Terraform to set up multiple VPC networks
The sections in this page show examples of creating a Memorystore for Redis Cluster instance and setting up connectivity in a Virtual Private Cloud (VPC) network.
To find instructions and examples of using Terraform to set up connectivity in
multiple VPC networks, see thegoogle_redis_cluster_user_created_connectionspage.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Quickstart: Create a Memorystore for Redis instance by using Terraform\n\nCreate a Memorystore for Redis Cluster instance using Terraform\n===============================================================\n\nThis quickstart shows you how to use Terraform to create a new Memorystore for Redis Cluster instance. It also shows you how to connect to the instance using telnet.\n\n\u003cbr /\u003e\n\nThis quickstart assumes you are starting a new Terraform file, and\nincludes the Terraform resource for setting up the Google Cloud provider. However, it omits\nthe steps for authenticating your Terraform file with the Google Cloud provider.\n\nFor instructions on authenticating Terraform with the Google Cloud provider, see\n[Getting started with the Google Cloud provider](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started)\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n1. If you haven't already, [install the Google Cloud SDK](/sdk/docs).\n\n When prompted, choose the project that you selected or created above.\n2. If you already have the Google Cloud SDK installed, update it.\n\n ```\n gcloud components update\n ```\n3. Enable the Memorystore for Redis Cluster API \n [Memorystore for Redis Cluster](https://console.cloud.google.com/apis/library/redis.googleapis.com)\n\n\u003cbr /\u003e\n\nConfigure Terraform for Memorystore\n-----------------------------------\n\nThis section shows examples of five Terraform resources needed to create a\nMemorystore for Redis Cluster instance:\n\n- The Google Cloud provider configuration.\n\n- A Memorystore instance configuration. In this guide, it is a 39 GB cluster with three [`redis-highmem-medium`](/memorystore/docs/cluster/cluster-node-specification) nodes in the `us-central1` region.\n\n- A Private Service Connect [service connection policy](/vpc/docs/about-service-connection-policies).\n\n- A subnet for your instance.\n\n- A network for your instance.\n\nThe instructions for adding these Terraform resources are as follows:\n\n1. Add the following resources to your Terraform configuration file:\n\n ```\n\n provider \"google\" {\n project = \"PROJECT_ID\"\n }\n\n resource \"google_redis_cluster\" \"cluster-ha\" {\n name = \"CLUSTER_ID\"\n shard_count = 3\n psc_configs {\n network = google_compute_network.producer_net.id\n }\n region = \"us-central1\"\n replica_count = 1\n depends_on = [\n google_network_connectivity_service_connection_policy.default\n ]\n\n }\n\n resource \"google_network_connectivity_service_connection_policy\" \"default\" {\n name = \"POLICY_NAME\"\n location = \"us-central1\"\n service_class = \"gcp-memorystore-redis\"\n description = \"my basic service connection policy\"\n network = google_compute_network.producer_net.id\n psc_config {\n subnetworks = [google_compute_subnetwork.producer_subnet.id]\n }\n }\n\n resource \"google_compute_subnetwork\" \"producer_subnet\" {\n name = \"SUBNET_ID\"\n ip_cidr_range = \"10.0.0.248/29\"\n region = \"us-central1\"\n network = google_compute_network.producer_net.id\n }\n\n resource \"google_compute_network\" \"producer_net\" {\n name = \"NETWORK_ID\"\n auto_create_subnetworks = false\n }\n\n ```\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e is your instance's project ID.\n - \u003cvar translate=\"no\"\u003eCLUSTER_ID\u003c/var\u003e is your chosen ID of the Memorystore for Redis Cluster instance you're creating. The ID must be 1 to 63 characters and use only lowercase letters, numbers, or hyphens. It must start with a lowercase letter and end with a lowercase letter or number.\n - \u003cvar translate=\"no\"\u003ePOLICY_NAME\u003c/var\u003e is your chosen name for the Private Service Connect service connection policy for your project. For more information about the service connection policy, see [Networking](/memorystore/docs/cluster/networking).\n - \u003cvar translate=\"no\"\u003eSUBNET_ID\u003c/var\u003e is your chosen ID of the subnet created in this example that is used by the Memorystore cluster.\n - \u003cvar translate=\"no\"\u003eNETWORK_ID\u003c/var\u003e is your chosen ID of the network created in this example that is used by the Memorystore cluster.\n\nDeploy the Terraform configuration file\n---------------------------------------\n\n1. Run `terraform init`.\n\n2. Run `terraform plan`.\n\n3. Run `terraform apply`.\n\nConnecting to the instance from a Compute Engine VM\n---------------------------------------------------\n\nNext, connect to the newly created Memorystore for Redis Cluster instance.\n\nYou can connect to the instance from any Compute Engine VM that uses the\ninstance's authorized network.\n\n1. If you don't already have a Compute Engine VM that uses that same authorized network as your instance, create one and connect to it by following [Quickstart using a Linux VM](/compute/docs/create-linux-vm-instance).\n\n2. Install `telnet` using `apt-get`:\n\n ```\n sudo apt-get install telnet\n ```\n3. From the terminal, telnet to the IP address of the instance, replacing \u003cvar translate=\"no\"\u003eVARIABLES\u003c/var\u003e with appropriate values.\n\n ```\n telnet CLUSTER_DISCOVERY_ENDPOINT_IP_ADDRESS CLUSTER_DISCOVERY_ENDPOINT_PORT_NUMBER\n ```\n4. In the telnet session, enter some Redis commands:\n\n Enter: \n\n ```\n PING\n ```\n\n Result: \n\n ```\n PONG\n ```\n\nClean up\n--------\n\nTo avoid incurring charges to your Google Cloud account for the resources used\nin this quickstart, delete the Redis instance using the following steps:\n\n1. Remove the `google_redis_instance` resource from your Terraform configuration file.\n2. Run Terraform `init`, `plan`, and `apply` to destroy the Redis resource.\n3. [Delete](/compute/docs/create-linux-vm-instance#clean-up) any Compute Engine VMs you created for this quickstart.\n\nConfigure Terraform to set up multiple VPC networks\n---------------------------------------------------\n\nThe sections in this page show examples of creating a Memorystore for Redis Cluster instance and setting up connectivity in a Virtual Private Cloud (VPC) network.\nTo find instructions and examples of using Terraform to set up connectivity in\nmultiple VPC networks, see the [google_redis_cluster_user_created_connections](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/redis_cluster_user_created_connections) page.\n\nWhat's next\n-----------\n\n- Learn more about [Creating instances](/memorystore/docs/cluster/create-instances).\n- Learn more about [High availability and replicas](/memorystore/docs/cluster/ha-and-replicas).\n- Learn more about [Best practices for Memorystore for Redis Cluster](/memorystore/docs/cluster/general-best-practices)."]]