Create a Memorystore for Redis Cluster instance using Terraform
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.
For instructions on authenticating Terraform with the Google Cloud provider, see Getting started with the Google Cloud provider
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- If you haven't already, install the Google Cloud SDK.
When prompted, choose the project that you selected or created above.
If you already have the Google Cloud SDK installed, update it.
gcloud components update
-
Enable the Memorystore for Redis Cluster API
Memorystore for Redis Cluster
Configure Terraform for Memorystore
This section shows examples of five Terraform resources needed to create a Memorystore for Redis Cluster instance:
The Google Cloud provider configuration.
A Memorystore instance configuration. In this guide, it is a 39 GB cluster with three
redis-highmem-medium
nodes in theus-central1
region.A Private Service Connect (PSC) service connection policy.
A subnet for your instance.
A network for your instance.
The instructions for adding these Terraform resources are as follows:
Add the following resources to your Terraform configuration file:
provider "google" { project = "PROJECT_ID" } resource "google_redis_cluster" "cluster-ha" { name = "CLUSTER_ID" shard_count = 3 psc_configs { network = google_compute_network.producer_net.id } region = "us-central1" replica_count = 1 depends_on = [ google_network_connectivity_service_connection_policy.default ] } resource "google_network_connectivity_service_connection_policy" "default" { name = "POLICY_NAME" location = "us-central1" service_class = "gcp-memorystore-redis" description = "my basic service connection policy" network = google_compute_network.producer_net.id psc_config { subnetworks = [google_compute_subnetwork.producer_subnet.id] } } resource "google_compute_subnetwork" "producer_subnet" { name = "SUBNET_ID" ip_cidr_range = "10.0.0.248/29" region = "us-central1" network = google_compute_network.producer_net.id } resource "google_compute_network" "producer_net" { name = "NETWORK_ID" auto_create_subnetworks = false }
Replace the following:
- PROJECT_ID is your instance's project ID.
- CLUSTER_ID 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.
- POLICY_NAME 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.
- SUBNET_ID is your chosen ID of the subnet created in this example that is used by the Memorystore cluster.
- NETWORK_ID is your chosen ID of the network created in this example that is used by the Memorystore cluster.
Deploy the Terraform configuration file
Run
terraform init
.Run
terraform plan
.Run
terraform 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 following Quickstart using a Linux VM.
Install
telnet
usingapt-get
:sudo apt-get install telnet
From the terminal, telnet to the IP address of the instance, replacing VARIABLES with appropriate values.
telnet CLUSTER_DISCOVERY_ENDPOINT_IP_ADDRESS CLUSTER_DISCOVERY_ENDPOINT_PORT_NUMBER
In the telnet session, enter some Redis commands:
Enter:
PING
Result:
PONG
Clean up
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 the
google_redis_instance
resource from your Terraform configuration file. - Run Terraform
init
,plan
, andapply
to destroy the Redis resource. - Delete any Compute Engine VMs you created for this quickstart.
What's next
- Learn more about Creating instances.
- Learn more about High availability and replicas.
- Learn more about General practices.