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 for Terraform.
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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Memorystore for Redis API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Memorystore for Redis API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. - If you haven't already, install the Google Cloud SDK.
When prompted, choose the project that you selected or created.
If you already have the Google Cloud SDK installed, then update it.
gcloud components update
-
Enable the Memorystore for Redis API.
Memorystore for Redis - 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.
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 (
roles/redis.admin) -
Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) -
Service Account User (
roles/iam.serviceAccountUser)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
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.
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" }Add the following output value 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}" }Run the
terraform initcommand.Run the
terraform plancommand, and then review the instance to be created.To create the instance, run the
terraform applycommand.
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.
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.
Use
apt-getto installtelnet:sudo apt-get install telnetFrom the terminal, telnet to the IP address of the instance. Replace VARIABLES 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
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.
- Remove the
google_redis_instanceresource from your Terraform configuration file. - To delete the resource, run the
terraform init,terraform plan, andterraform applycommands. - Optional. If you created a Compute Engine VM for this quickstart, then delete it.
What's next
- Learn more about creating and managing instances.
- Learn about other ways to connect to your Redis instance.
- Learn about the characteristics of a Redis instance.