Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Membuat instance Memorystore for Redis menggunakan Terraform
Halaman ini menjelaskan cara menggunakan Terraform untuk membuat instance Memorystore for Redis
baru. Bagian ini juga menunjukkan cara menghubungkan ke instance menggunakan telnet.
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.
Memiliki file Terraform yang dapat Anda tambahi resource Memorystore. Untuk
petunjuk tentang cara menyiapkan Terraform dengan Google Cloud, lihat Mulai Menggunakan Penyedia Google Cloud.
Membuat instance Memorystore for Redis
Contoh resource Terraform berikut membuat instance Redis Tingkat Dasar 2 GiB di region us-central1 dalam tingkat BASIC. Untuk mengetahui informasi selengkapnya tentang tingkat, lihat Kemampuan tingkat Redis.
Tambahkan resource berikut ke file konfigurasi Terraform Anda:
resource "google_redis_instance" "my_memorystore_redis_instance" {
name = "myinstance"
tier = "BASIC"
memory_size_gb = 2
region = "us-central1"
redis_version = "REDIS_6_X"
}
(Opsional) Tambahkan nilai output berikut
ke file konfigurasi Terraform Anda untuk mencetak alamat IP instance Redis, yang diperlukan nanti dalam panduan memulai cepat ini:
output "host" {
description = "The IP address of the instance."
value = "${google_redis_instance.my_memorystore_redis_instance.host}"
}
Jalankan terraform init.
Jalankan terraform plan, lalu tinjau resource yang akan dibuat.
Jalankan terraform apply untuk membuat.
Menghubungkan ke instance Redis dari VM Compute Engine
Selanjutnya, hubungkan ke instance Redis yang baru dibuat.
Anda dapat terhubung ke instance Redis dari VM Compute Engine mana pun yang menggunakan jaringan resmi instance Redis dengan alamat IP RFC 1918 yang didukung.
Jika Anda belum memiliki VM Compute Engine yang menggunakan jaringan resmi yang sama dengan instance Redis Anda, buat VM dan hubungkan ke VM tersebut dengan mengikuti Panduan memulai menggunakan VM Linux.
Instal telnet menggunakan apt-get:
sudoapt-getinstalltelnet
Dari terminal, telnet ke alamat IP instance Redis, dengan mengganti variables dengan nilai yang sesuai.
telnet instance-ip-address 6379
Jika berhasil, perintah akan menampilkan hasil ini:
Trying instance-ip-address…
Connected to instance-ip-address
Dalam sesi telnet, masukkan beberapa perintah Redis:
Enter:
PING
Hasil:
PONG
Enter
SET HELLO WORLD
Hasil:
+OK
Enter:
GET HELLO
Hasil:
$5
WORLD
Pembersihan
Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam panduan memulai ini, hapus instance Redis menggunakan langkah-langkah di bawah:
Hapus resource google_redis_instance dari file konfigurasi Terraform Anda.
Jalankan Terraform init, plan, dan apply untuk menghancurkan resource Redis.
Hapus semua VM Compute Engine yang Anda buat untuk panduan memulai ini.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-05 UTC."],[],[],null,["# Quickstart: Create a Memorystore for Redis instance by using Terraform\n\nCreate a Memorystore for Redis instance by using Terraform\n==========================================================\n\n| **Note:** If you are looking for the Memorystore for Redis Cluster documentation, see [Instance provisioning on a VPC network](/memorystore/docs/cluster/instance-provisioning-vpc).\n\nThis page describes how to use Terraform to create a new Memorystore for Redis\ninstance. It also shows you how to connect to the instance using telnet.\n\nThis quickstart uses the [Google Cloud Platform Provider](https://registry.terraform.io/providers/hashicorp/google/latest/docs)\nfor Terraform.\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n1. If you haven't already, [install the Google Cloud SDK](/sdk/docs).\n\n When prompted, choose the project that you selected or created above.\n2. If you already have the Google Cloud SDK installed, update it.\n\n ```\n gcloud components update\n ```\n3. Enable the Memorystore for Redis API \n [Memorystore for Redis](https://console.cloud.google.com/apis/library/redis.googleapis.com)\n4. 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).\n\n\u003cbr /\u003e\n\nCreating a Memorystore for Redis instance\n-----------------------------------------\n\nThe following Terraform resource example creates a 2 GiB Basic Tier Redis\ninstance in the `us-central1` region in the BASIC tier. For more information\nabout tiers, see [Redis tier capabilities](/memorystore/docs/redis/redis-tiers).\n\n1. Add the following resource to your Terraform configuration file:\n\n ```\n resource \"google_redis_instance\" \"my_memorystore_redis_instance\" {\n name = \"myinstance\"\n tier = \"BASIC\"\n memory_size_gb = 2\n region = \"us-central1\"\n redis_version = \"REDIS_6_X\"\n }\n ```\n2. **(Optional)** Add the following [output value](https://www.terraform.io/docs/configuration/outputs.html)\n to your Terraform configuration file to print the IP address of the Redis\n instance, which is needed later in this quickstart:\n\n ```\n output \"host\" {\n description = \"The IP address of the instance.\"\n value = \"${google_redis_instance.my_memorystore_redis_instance.host}\"\n }\n ```\n3. Run `terraform init`.\n\n4. Run `terraform plan`, and review the resource to be created.\n\n5. Run `terraform apply` to create.\n\nConnecting to the Redis instance from a Compute Engine VM\n---------------------------------------------------------\n\nNext, connect to the newly created Redis instance.\n\nYou can connect to the Redis instance from any Compute Engine VM\nthat uses the Redis instance's authorized network with a [supported RFC 1918 IP address](/memorystore/docs/redis/networking#supported_and_unsupported_networks).\n\n1. If you don't already have a Compute Engine VM that uses that same\n authorized network as your Redis instance, create one and connect to it by\n following [Quickstart using a Linux VM](/compute/docs/create-linux-vm-instance).\n\n2. Install `telnet` using `apt-get`:\n\n sudo apt-get install telnet\n\n3. From the terminal, telnet to the IP address of the Redis instance, replacing\n \u003cvar translate=\"no\"\u003evariables\u003c/var\u003e with appropriate values.\n\n ```\n telnet instance-ip-address 6379\n ```\n\n If successful, the command will return this result: \n\n ```\n Trying instance-ip-address…\n Connected to instance-ip-address\n ```\n4. In the telnet session, enter some Redis commands:\n\n Enter: \n\n ```\n PING\n ```\n\n Result: \n\n ```\n PONG\n ```\n\n Enter \n\n ```\n SET HELLO WORLD\n ```\n\n Result: \n\n ```\n +OK\n ```\n\n Enter: \n\n ```\n GET HELLO\n ```\n\n Result: \n\n ```\n $5\n WORLD\n ```\n\nClean up\n--------\n\nTo avoid incurring charges to your Google Cloud account for the resources used\nin this quickstart, delete the Redis instance using the steps below:\n\n1. Remove the `google_redis_instance` resource from your Terraform configuration file.\n2. Run Terraform `init`, `plan`, and `apply` to destroy the Redis resource.\n3. [Delete](/compute/docs/create-linux-vm-instance#clean-up) any Compute Engine VMs you created for this quickstart.\n\nWhat's next\n-----------\n\n- Learn more about [creating and managing instances](/memorystore/docs/redis/create-manage-instances).\n- Learn about other ways to [connect to your Redis instance](/memorystore/docs/redis/connecting-redis-instance).\n- Learn about the characteristics of a [Redis instance](/memorystore/docs/redis/instances)."]]