From acd514afe2705143569bc1307d58503533c653cb Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Thu, 27 Mar 2025 21:49:33 +0000 Subject: [PATCH] Memorystore Instance Cross Instance Replication addition (#13432) [upstream:4a72b591b882645a803c578c097096c694eb9687] Signed-off-by: Modular Magician --- .../backing_file.tf | 15 ++ .../main.tf | 131 ++++++++++++++++++ memorystore_instance_secondary_instance/motd | 7 + .../tutorial.md | 79 +++++++++++ 4 files changed, 232 insertions(+) create mode 100644 memorystore_instance_secondary_instance/backing_file.tf create mode 100644 memorystore_instance_secondary_instance/main.tf create mode 100644 memorystore_instance_secondary_instance/motd create mode 100644 memorystore_instance_secondary_instance/tutorial.md diff --git a/memorystore_instance_secondary_instance/backing_file.tf b/memorystore_instance_secondary_instance/backing_file.tf new file mode 100644 index 00000000..c60b1199 --- /dev/null +++ b/memorystore_instance_secondary_instance/backing_file.tf @@ -0,0 +1,15 @@ +# This file has some scaffolding to make sure that names are unique and that +# a region and zone are selected when you try to create your Terraform resources. + +locals { + name_suffix = "${random_pet.suffix.id}" +} + +resource "random_pet" "suffix" { + length = 2 +} + +provider "google" { + region = "us-central1" + zone = "us-central1-c" +} diff --git a/memorystore_instance_secondary_instance/main.tf b/memorystore_instance_secondary_instance/main.tf new file mode 100644 index 00000000..68789383 --- /dev/null +++ b/memorystore_instance_secondary_instance/main.tf @@ -0,0 +1,131 @@ +// Primary instance +resource "google_memorystore_instance" "primary_instance" { + instance_id = "primary-instance-${local.name_suffix}" + shard_count = 1 + desired_psc_auto_connections { + network = google_compute_network.primary_producer_net.id + project_id = data.google_project.project.project_id + } + location = "asia-east1" + replica_count = 1 + node_type = "SHARED_CORE_NANO" + transit_encryption_mode = "TRANSIT_ENCRYPTION_DISABLED" + authorization_mode = "AUTH_DISABLED" + engine_configs = { + maxmemory-policy = "volatile-ttl" + } + zone_distribution_config { + mode = "SINGLE_ZONE" + zone = "asia-east1-c" + } + deletion_protection_enabled = false + persistence_config { + mode = "RDB" + rdb_config { + rdb_snapshot_period = "ONE_HOUR" + rdb_snapshot_start_time = "2024-10-02T15:01:23Z" + } + } + labels = { + "abc" : "xyz" + } + depends_on = [google_network_connectivity_service_connection_policy.primary_policy] + + lifecycle { + prevent_destroy = false + } +} + +resource "google_network_connectivity_service_connection_policy" "primary_policy" { + name = "my-policy-primary-instance-${local.name_suffix}" + location = "asia-east1" + service_class = "gcp-memorystore" + description = "my basic service connection policy" + network = google_compute_network.primary_producer_net.id + psc_config { + subnetworks = [google_compute_subnetwork.primary_producer_subnet.id] + } +} + +resource "google_compute_subnetwork" "primary_producer_subnet" { + name = "my-subnet-primary-instance-${local.name_suffix}" + ip_cidr_range = "10.0.1.0/29" + region = "asia-east1" + network = google_compute_network.primary_producer_net.id +} + +resource "google_compute_network" "primary_producer_net" { + name = "my-network-primary-instance-${local.name_suffix}" + auto_create_subnetworks = false +} + +// Secondary instance +resource "google_memorystore_instance" "secondary_instance" { + instance_id = "secondary-instance-${local.name_suffix}" + shard_count = 1 + desired_psc_auto_connections { + network = google_compute_network.secondary_producer_net.id + project_id = data.google_project.project.project_id + } + location = "europe-north1" + replica_count = 1 + node_type = "SHARED_CORE_NANO" + transit_encryption_mode = "TRANSIT_ENCRYPTION_DISABLED" + authorization_mode = "AUTH_DISABLED" + engine_configs = { + maxmemory-policy = "volatile-ttl" + } + zone_distribution_config { + mode = "SINGLE_ZONE" + zone = "europe-north1-c" + } + deletion_protection_enabled = false + // Cross instance replication config + cross_instance_replication_config { + instance_role = "SECONDARY" + primary_instance { + instance = google_memorystore_instance.primary_instance.id + } + } + persistence_config { + mode = "RDB" + rdb_config { + rdb_snapshot_period = "ONE_HOUR" + rdb_snapshot_start_time = "2024-10-02T15:01:23Z" + } + } + labels = { + "abc" : "xyz" + } + depends_on = [google_network_connectivity_service_connection_policy.secondary_policy] + + lifecycle { + prevent_destroy = false + } +} + +resource "google_network_connectivity_service_connection_policy" "secondary_policy" { + name = "my-policy-secondary-instance-${local.name_suffix}" + location = "europe-north1" + service_class = "gcp-memorystore" + description = "my basic service connection policy" + network = google_compute_network.secondary_producer_net.id + psc_config { + subnetworks = [google_compute_subnetwork.secondary_producer_subnet.id] + } +} + +resource "google_compute_subnetwork" "secondary_producer_subnet" { + name = "my-subnet-secondary-instance-${local.name_suffix}" + ip_cidr_range = "10.0.2.0/29" + region = "europe-north1" + network = google_compute_network.secondary_producer_net.id +} + +resource "google_compute_network" "secondary_producer_net" { + name = "my-network-secondary-instance-${local.name_suffix}" + auto_create_subnetworks = false +} + +data "google_project" "project" { +} diff --git a/memorystore_instance_secondary_instance/motd b/memorystore_instance_secondary_instance/motd new file mode 100644 index 00000000..45a906e8 --- /dev/null +++ b/memorystore_instance_secondary_instance/motd @@ -0,0 +1,7 @@ +=== + +These examples use real resources that will be billed to the +Google Cloud Platform project you use - so make sure that you +run "terraform destroy" before quitting! + +=== diff --git a/memorystore_instance_secondary_instance/tutorial.md b/memorystore_instance_secondary_instance/tutorial.md new file mode 100644 index 00000000..f80c87a1 --- /dev/null +++ b/memorystore_instance_secondary_instance/tutorial.md @@ -0,0 +1,79 @@ +# Memorystore Instance Secondary Instance - Terraform + +## Setup + + + +Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform. + + + +Terraform provisions real GCP resources, so anything you create in this session will be billed against this project. + +## Terraforming! + +Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command +to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up +the project name from the environment variable. + +```bash +export GOOGLE_CLOUD_PROJECT={{project-id}} +``` + +After that, let's get Terraform started. Run the following to pull in the providers. + +```bash +terraform init +``` + +With the providers downloaded and a project set, you're ready to use Terraform. Go ahead! + +```bash +terraform apply +``` + +Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan. + +```bash +yes +``` + + +## Post-Apply + +### Editing your config + +Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed. + +```bash +terraform plan +``` + +So let's make a change! Try editing a number, or appending a value to the name in the editor. Then, +run a 'plan' again. + +```bash +terraform plan +``` + +Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes +at the 'yes' prompt. + +```bash +terraform apply +``` + +```bash +yes +``` + +## Cleanup + +Run the following to remove the resources Terraform provisioned: + +```bash +terraform destroy +``` +```bash +yes +```