Terraform を使用して Memorystore for Redis インスタンスを作成する
Memorystore for Redis インスタンスを作成して接続し、削除する方法を学習します。
このクイックスタートでは、Terraform 用の Google Cloud Platform プロバイダを使用します。
始める前に
- 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.
-
このガイドで既存のプロジェクトを使用する場合は、このガイドを完了するために必要な権限があることを確認します。新しいプロジェクトを作成した場合は、必要な権限がすでに付与されています。
-
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.
-
このガイドで既存のプロジェクトを使用する場合は、このガイドを完了するために必要な権限があることを確認します。新しいプロジェクトを作成した場合は、必要な権限がすでに付与されています。
-
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. - まだインストールしていない場合は、Google Cloud SDK をインストールします。
プロンプトが表示されたら、上記で作成または選択したプロジェクトを選択します。
Google Cloud SDK がすでにインストールされている場合は、更新します。
gcloud components update
-
Memorystore for Redis API を有効にします。
Memorystore for Redis - Memorystore リソースを追加できる Terraform ファイルを用意します。Google Cloud で Terraform を設定する手順については、Google Cloud プロバイダ スタートガイドをご覧ください。
-
Cloud Memorystore Redis 管理者(
roles/redis.admin) -
Compute インスタンス管理者(v1) (
roles/compute.instanceAdmin.v1) -
サービス アカウント ユーザー (
roles/iam.serviceAccountUser)
必要なロール
Memorystore for Redis インスタンスの作成に必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。
ロールの付与については、プロジェクト、フォルダ、組織へのアクセス権の管理をご覧ください。
Memorystore for Redis インスタンスを作成する
このセクションでは、us-central1 リージョンに配置され、ベーシック ティアの 2 GB の Memorystore for Redis インスタンスを作成します。階層の詳細については、Redis の階層の機能をご覧ください。
Terraform 構成ファイルに以下のリソースを追加します。
resource "google_redis_instance" "my_memorystore_redis_instance" { name = "myinstance" tier = "BASIC" memory_size_gb = 2 region = "us-central1" redis_version = "REDIS_6_X" }次の出力値を Terraform 構成ファイルに追加して、インスタンスの IP アドレスを出力します。このアドレスは、インスタンスに接続するために必要です。
output "host" { description = "The IP address of the instance." value = "${google_redis_instance.my_memorystore_redis_instance.host}" }terraform initコマンドを実行します。terraform planコマンドを実行して、作成されるインスタンスを確認します。インスタンスを作成するには、
terraform applyコマンドを実行します。
Compute Engine VM から Memorystore for Redis インスタンスに接続する
Memorystore for Redis インスタンスには、インスタンスの承認済みネットワークを使用する任意の Compute Engine VM からサポートされている RFC 1918 IP アドレス]をクリックします。
インスタンスと同じ承認済みネットワークを使用する Compute Engine VM がない場合は、作成して SSH を使用して VM に接続します。これを行うには、Compute Engine で Linux VM インスタンスを作成するの手順に沿って操作します。
apt-getを使用してtelnetをインストールします。sudo apt-get install telnetターミナルから、インスタンスの IP アドレスに telnet で接続します。VARIABLES は適切な値に置き換えます。
telnet INSTANCE_IP_ADDRESS 6379
インスタンスに接続できる場合、コマンドは次の結果を返します。
Trying INSTANCE_IP_ADDRESS… Connected to INSTANCE_IP_ADDRESS
telnet セッションで、次のようにいくつかの Redis コマンドを入力します。
次のように入力します。
PING
結果:
PONG
入力
SET HELLO WORLD
結果:
+OK
次のように入力します。
GET HELLO
結果:
$5 WORLD
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の操作を行います。
- Terraform 構成ファイルから
google_redis_instanceリソースを削除します。 - リソースを削除するには、
terraform init、terraform plan、terraform applyコマンドを実行します。 - (省略可)このクイックスタート用に Compute Engine VM を作成した場合は、それを削除します。
次のステップ
- インスタンスの作成と管理について学習する。
- Redis インスタンスに接続するその他の方法について学習する。
- Redis インスタンスの特性について学習する。