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.

For instructions on authenticating Terraform with the Google Cloud provider, see Getting started with the Google Cloud provider

Before you begin

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. If you haven't already, install the Google Cloud SDK.

    When prompted, choose the project that you selected or created above.

  7. If you already have the Google Cloud SDK installed, update it.

    gcloud components update
  8. 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 the us-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:

  1. 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

  1. Run terraform init.

  2. Run terraform plan.

  3. 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.

  1. 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.

  2. Install telnet using apt-get:

    sudo apt-get install telnet
    
  3. 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
    
  4. 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:

  1. Remove the google_redis_instance resource from your Terraform configuration file.
  2. Run Terraform init, plan, and apply to destroy the Redis resource.
  3. Delete any Compute Engine VMs you created for this quickstart.

What's next