Quickstart using Terraform

This page describes how to use Terraform to create a new Memorystore for Redis instance. It also shows you how to connect to the instance using SSH in the Google Cloud.

This quickstart uses the Google Cloud Platform Provider for Terraform.

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 Cloud project. Learn how to confirm that billing is enabled for your 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 Cloud project. Learn how to confirm that billing is enabled for your project.

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

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

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

    gcloud components update
  8. Enable the Memorystore for Redis API
    Memorystore for Redis
  9. Have a Terraform file to which you can add the Memorystore resource. For instructions on setting up Terraform with Google Cloud, see Getting Started with the Google Cloud Provider.

Creating a Memorystore for Redis instance

The following Terraform resource example creates a 2 GiB Basic Tier Redis instance in the us-central1 region in the BASIC tier. For more information about tiers, see Redis tier capabilities.

  1. Add the following resource to your Terraform configuration file:

    resource "google_redis_instance" "my_memorystore_redis_instance" {
      name           = "myinstance"
      tier           = "BASIC"
      memory_size_gb = 2
      region         = "us-central1"
      redis_version  = "REDIS_5_0"
    }
    
  2. (Optional) Add the following output value to your Terraform configuration file to print the IP address of the Redis instance, which is needed later in this quickstart:

    output "host" {
     description = "The IP address of the instance."
     value = "${google_redis_instance.my_memorystore_redis_instance.host}"
    }
    
  3. Run terraform init.

  4. Run terraform plan, and review the resource to be created.

  5. Run terraform apply to create.

Connecting to the Redis instance from a Compute Engine VM

Next, connect to the newly created Redis instance.

You can connect to the Redis instance from any Compute Engine VM that uses the Redis instance's authorized network with a supported RFC 1918 IP address.

  1. If you don't already have a Compute Engine VM that uses that same authorized network as your Redis 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 Redis instance, replacing variables with appropriate values.

    telnet instance-ip-address 6379
    

    If successful, the command will return this result:

    Trying instance-ip-address…
    Connected to instance-ip-address
    
  4. In the telnet session, enter some Redis commands:

    Enter:

    PING
    

    Result:

    PONG
    

    Enter

    SET HELLO WORLD
    

    Result:

    +OK
    

    Enter:

    GET HELLO
    

    Result:

    $5
    WORLD
    

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, delete the Redis instance using the steps below:

  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