# Create a Memorystore for Redis instance by using Terraform

Learn how to create a Memorystore for Redis instance, connect to it, and then delete
it.

This quickstart uses the [Google Cloud Platform provider](https://registry.terraform.io/providers/hashicorp/google/latest/docs)
for Terraform.

## Before you begin

1. If you haven't already, [install the Google Cloud SDK](https://docs.cloud.google.com/sdk/docs.md.txt).

   When prompted, choose the project that you selected or created.
2. If you already have the Google Cloud SDK installed, then update it.

   ```
   gcloud components update
   ```
3. Enable the Memorystore for Redis API.   
   [Memorystore for Redis](https://console.cloud.google.com/apis/library/redis.googleapis.com)
4. 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](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/getting_started).

<br />

### Required roles


To get the permissions that
you need to create a Memorystore for Redis instance,

ask your administrator to grant you the
following IAM roles on the project:

- [Cloud Memorystore Redis Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/redis.md.txt#redis.admin) (`roles/redis.admin`)
- [Compute Instance Admin (v1)](https://docs.cloud.google.com/iam/docs/roles-permissions/compute.md.txt#compute.instanceAdmin.v1) (`roles/compute.instanceAdmin.v1`)
- [Service Account User](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`)


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


You might also be able to get
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

## Create a Memorystore for Redis instance

In this section, you create a 2-GB Memorystore for Redis instance that's
located in the `us-central1` region and is in the Basic tier. For more information
about tiers, see [Redis tier capabilities](https://docs.cloud.google.com/memorystore/docs/redis/redis-tiers.md.txt).

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_6_X"
   }
   ```
2. Add the following [output value](https://www.terraform.io/docs/configuration/outputs.html)
   to your Terraform configuration file to print the IP address of the
   instance. You need this address to connect to your instance.

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

4. Run the `terraform plan` command, and then review the instance to be created.

5. To create the instance, run the `terraform apply` command.

## Connect to the Memorystore for Redis instance from a Compute Engine VM

You can connect to the Memorystore for Redis instance from any Compute Engine
VM that uses the instance's authorized network with a [supported RFC 1918 IP address](https://docs.cloud.google.com/memorystore/docs/redis/networking.md.txt#supported_and_unsupported_networks).

1. If you don't have a Compute Engine VM that uses that same authorized
   network as your instance, then create one and connect to the VM by using SSH. To
   do this, follow the steps in [Create a Linux VM instance in Compute Engine](https://docs.cloud.google.com/compute/docs/create-linux-vm-instance.md.txt).

2. Use `apt-get` to install `telnet`:

   ```terraform
   sudo apt-get install telnet
   ```
3. From the terminal, telnet to the IP address of the instance. Replace
   <var translate="no">VARIABLES</var> with appropriate values.

   ```
   telnet INSTANCE_IP_ADDRESS 6379
   ```

   If you can connect to the instance, then the command returns 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
on this page, follow these steps.

1. Remove the `google_redis_instance` resource from your Terraform configuration file.
2. To delete the resource, run the `terraform init`, `terraform plan`, and `terraform apply` commands.
3. **Optional** . If you created a Compute Engine VM for this quickstart, then [delete](https://docs.cloud.google.com/compute/docs/create-linux-vm-instance.md.txt#clean-up) it.

## What's next

- Learn more about [creating and managing instances](https://docs.cloud.google.com/memorystore/docs/redis/create-manage-instances.md.txt).
- Learn about other ways to [connect to your Redis instance](https://docs.cloud.google.com/memorystore/docs/redis/connect-redis-instance.md.txt).
- Learn about the characteristics of a [Redis instance](https://docs.cloud.google.com/memorystore/docs/redis/instances.md.txt).