Skip to content

Commit 36b8f3a

Browse files
Redis cluster Multi vpc support (#12548) (#892)
[upstream:f7be33fce8fb1b020bba9f16fed20fe48b9e3d12] Signed-off-by: Modular Magician <[email protected]>
1 parent f4aa6e9 commit 36b8f3a

File tree

8 files changed

+459
-0
lines changed

8 files changed

+459
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
resource "google_redis_cluster_user_created_connections" "cluster-user-auto-conn" {
2+
name = "cluster-user-auto-conn-${local.name_suffix}"
3+
region = "us-central1"
4+
cluster_endpoints {
5+
connections {
6+
psc_connection {
7+
psc_connection_id = google_compute_forwarding_rule.forwarding_rule1_network2.psc_connection_id
8+
address = google_compute_address.ip1_network2.address
9+
forwarding_rule = google_compute_forwarding_rule.forwarding_rule1_network2.id
10+
network = google_compute_network.network2.id
11+
service_attachment = google_redis_cluster.cluster-user-auto-conn.psc_service_attachments[0].service_attachment
12+
}
13+
}
14+
connections {
15+
psc_connection {
16+
psc_connection_id = google_compute_forwarding_rule.forwarding_rule2_network2.psc_connection_id
17+
address = google_compute_address.ip2_network2.address
18+
forwarding_rule = google_compute_forwarding_rule.forwarding_rule2_network2.id
19+
network = google_compute_network.network2.id
20+
service_attachment = google_redis_cluster.cluster-user-auto-conn.psc_service_attachments[1].service_attachment
21+
}
22+
}
23+
}
24+
}
25+
26+
resource "google_compute_forwarding_rule" "forwarding_rule1_network2" {
27+
name = "fwd1-net2-${local.name_suffix}"
28+
region = "us-central1"
29+
ip_address = google_compute_address.ip1_network2.id
30+
load_balancing_scheme = ""
31+
network = google_compute_network.network2.id
32+
target = google_redis_cluster.cluster-user-auto-conn.psc_service_attachments[0].service_attachment
33+
}
34+
35+
resource "google_compute_forwarding_rule" "forwarding_rule2_network2" {
36+
name = "fwd2-net2-${local.name_suffix}"
37+
region = "us-central1"
38+
ip_address = google_compute_address.ip2_network2.id
39+
load_balancing_scheme = ""
40+
network = google_compute_network.network2.id
41+
target = google_redis_cluster.cluster-user-auto-conn.psc_service_attachments[1].service_attachment
42+
}
43+
44+
resource "google_compute_address" "ip1_network2" {
45+
name = "ip1-net2-${local.name_suffix}"
46+
region = "us-central1"
47+
subnetwork = google_compute_subnetwork.subnet_network2.id
48+
address_type = "INTERNAL"
49+
purpose = "GCE_ENDPOINT"
50+
}
51+
52+
resource "google_compute_address" "ip2_network2" {
53+
name = "ip2-net2-${local.name_suffix}"
54+
region = "us-central1"
55+
subnetwork = google_compute_subnetwork.subnet_network2.id
56+
address_type = "INTERNAL"
57+
purpose = "GCE_ENDPOINT"
58+
}
59+
60+
resource "google_compute_subnetwork" "subnet_network2" {
61+
name = "subnet-net2-${local.name_suffix}"
62+
ip_cidr_range = "10.0.0.248/29"
63+
region = "us-central1"
64+
network = google_compute_network.network2.id
65+
}
66+
67+
resource "google_compute_network" "network2" {
68+
name = "network2-${local.name_suffix}"
69+
auto_create_subnetworks = false
70+
}
71+
72+
// redis cluster without endpoint
73+
resource "google_redis_cluster" "cluster-user-auto-conn" {
74+
name = "cluster-user-auto-conn-${local.name_suffix}"
75+
shard_count = 3
76+
region = "us-central1"
77+
replica_count = 0
78+
deletion_protection_enabled = false
79+
psc_configs {
80+
network = google_compute_network.network1.id
81+
}
82+
depends_on = [
83+
google_network_connectivity_service_connection_policy.default
84+
]
85+
}
86+
87+
resource "google_network_connectivity_service_connection_policy" "default" {
88+
name = "scpolicy-${local.name_suffix}"
89+
location = "us-central1"
90+
service_class = "gcp-memorystore-redis"
91+
description = "my basic service connection policy"
92+
network = google_compute_network.network1.id
93+
psc_config {
94+
subnetworks = [google_compute_subnetwork.subnet_network1.id]
95+
}
96+
}
97+
98+
resource "google_compute_subnetwork" "subnet_network1" {
99+
name = "subnet-net1-${local.name_suffix}"
100+
ip_cidr_range = "10.0.0.248/29"
101+
region = "us-central1"
102+
network = google_compute_network.network1.id
103+
}
104+
105+
resource "google_compute_network" "network1" {
106+
name = "net1-${local.name_suffix}"
107+
auto_create_subnetworks = false
108+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
===
2+
3+
These examples use real resources that will be billed to the
4+
Google Cloud Platform project you use - so make sure that you
5+
run "terraform destroy" before quitting!
6+
7+
===
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Redis Cluster User And Auto Created Connections - Terraform
2+
3+
## Setup
4+
5+
<walkthrough-author name="[email protected]" analyticsId="UA-125550242-1" tutorialName="redis_cluster_user_and_auto_created_connections" repositoryUrl="https://github.com/terraform-google-modules/docs-examples"></walkthrough-author>
6+
7+
Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform.
8+
9+
<walkthrough-project-billing-setup></walkthrough-project-billing-setup>
10+
11+
Terraform provisions real GCP resources, so anything you create in this session will be billed against this project.
12+
13+
## Terraforming!
14+
15+
Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command
16+
to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up
17+
the project name from the environment variable.
18+
19+
```bash
20+
export GOOGLE_CLOUD_PROJECT={{project-id}}
21+
```
22+
23+
After that, let's get Terraform started. Run the following to pull in the providers.
24+
25+
```bash
26+
terraform init
27+
```
28+
29+
With the providers downloaded and a project set, you're ready to use Terraform. Go ahead!
30+
31+
```bash
32+
terraform apply
33+
```
34+
35+
Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan.
36+
37+
```bash
38+
yes
39+
```
40+
41+
42+
## Post-Apply
43+
44+
### Editing your config
45+
46+
Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed.
47+
48+
```bash
49+
terraform plan
50+
```
51+
52+
So let's make a change! Try editing a number, or appending a value to the name in the editor. Then,
53+
run a 'plan' again.
54+
55+
```bash
56+
terraform plan
57+
```
58+
59+
Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes
60+
at the 'yes' prompt.
61+
62+
```bash
63+
terraform apply
64+
```
65+
66+
```bash
67+
yes
68+
```
69+
70+
## Cleanup
71+
72+
Run the following to remove the resources Terraform provisioned:
73+
74+
```bash
75+
terraform destroy
76+
```
77+
```bash
78+
yes
79+
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
resource "google_redis_cluster_user_created_connections" "cluster-user-conn" {
2+
name = "cluster-user-conn-${local.name_suffix}"
3+
region = "us-central1"
4+
cluster_endpoints {
5+
connections {
6+
psc_connection {
7+
psc_connection_id = google_compute_forwarding_rule.forwarding_rule1_network1.psc_connection_id
8+
address = google_compute_address.ip1_network1.address
9+
forwarding_rule = google_compute_forwarding_rule.forwarding_rule1_network1.id
10+
network = google_compute_network.network1.id
11+
project_id = data.google_project.project.project_id
12+
service_attachment = google_redis_cluster.cluster-user-conn.psc_service_attachments[0].service_attachment
13+
}
14+
}
15+
connections {
16+
psc_connection {
17+
psc_connection_id = google_compute_forwarding_rule.forwarding_rule2_network1.psc_connection_id
18+
address = google_compute_address.ip2_network1.address
19+
forwarding_rule = google_compute_forwarding_rule.forwarding_rule2_network1.id
20+
network = google_compute_network.network1.id
21+
service_attachment = google_redis_cluster.cluster-user-conn.psc_service_attachments[1].service_attachment
22+
}
23+
}
24+
}
25+
cluster_endpoints {
26+
connections {
27+
psc_connection {
28+
psc_connection_id = google_compute_forwarding_rule.forwarding_rule1_network2.psc_connection_id
29+
address = google_compute_address.ip1_network2.address
30+
forwarding_rule = google_compute_forwarding_rule.forwarding_rule1_network2.id
31+
network = google_compute_network.network2.id
32+
service_attachment = google_redis_cluster.cluster-user-conn.psc_service_attachments[0].service_attachment
33+
}
34+
}
35+
connections {
36+
psc_connection {
37+
psc_connection_id = google_compute_forwarding_rule.forwarding_rule2_network2.psc_connection_id
38+
address = google_compute_address.ip2_network2.address
39+
forwarding_rule = google_compute_forwarding_rule.forwarding_rule2_network2.id
40+
network = google_compute_network.network2.id
41+
service_attachment = google_redis_cluster.cluster-user-conn.psc_service_attachments[1].service_attachment
42+
}
43+
}
44+
}
45+
}
46+
47+
resource "google_compute_forwarding_rule" "forwarding_rule1_network1" {
48+
name = "fwd1-net1-${local.name_suffix}"
49+
region = "us-central1"
50+
ip_address = google_compute_address.ip1_network1.id
51+
load_balancing_scheme = ""
52+
network = google_compute_network.network1.id
53+
target = google_redis_cluster.cluster-user-conn.psc_service_attachments[0].service_attachment
54+
}
55+
56+
resource "google_compute_forwarding_rule" "forwarding_rule2_network1" {
57+
name = "fwd2-net1-${local.name_suffix}"
58+
region = "us-central1"
59+
ip_address = google_compute_address.ip2_network1.id
60+
load_balancing_scheme = ""
61+
network = google_compute_network.network1.id
62+
target = google_redis_cluster.cluster-user-conn.psc_service_attachments[1].service_attachment
63+
}
64+
65+
resource "google_compute_address" "ip1_network1" {
66+
name = "ip1-net1-${local.name_suffix}"
67+
region = "us-central1"
68+
subnetwork = google_compute_subnetwork.subnet_network1.id
69+
address_type = "INTERNAL"
70+
purpose = "GCE_ENDPOINT"
71+
}
72+
73+
resource "google_compute_address" "ip2_network1" {
74+
name = "ip2-net1-${local.name_suffix}"
75+
region = "us-central1"
76+
subnetwork = google_compute_subnetwork.subnet_network1.id
77+
address_type = "INTERNAL"
78+
purpose = "GCE_ENDPOINT"
79+
}
80+
81+
resource "google_compute_subnetwork" "subnet_network1" {
82+
name = "subnet-net1-${local.name_suffix}"
83+
ip_cidr_range = "10.0.0.248/29"
84+
region = "us-central1"
85+
network = google_compute_network.network1.id
86+
}
87+
88+
resource "google_compute_network" "network1" {
89+
name = "net1-${local.name_suffix}"
90+
auto_create_subnetworks = false
91+
}
92+
93+
resource "google_compute_forwarding_rule" "forwarding_rule1_network2" {
94+
name = "fwd1-net2-${local.name_suffix}"
95+
region = "us-central1"
96+
ip_address = google_compute_address.ip1_network2.id
97+
load_balancing_scheme = ""
98+
network = google_compute_network.network2.id
99+
target = google_redis_cluster.cluster-user-conn.psc_service_attachments[0].service_attachment
100+
}
101+
102+
resource "google_compute_forwarding_rule" "forwarding_rule2_network2" {
103+
name = "fwd2-net2-${local.name_suffix}"
104+
region = "us-central1"
105+
ip_address = google_compute_address.ip2_network2.id
106+
load_balancing_scheme = ""
107+
network = google_compute_network.network2.id
108+
target = google_redis_cluster.cluster-user-conn.psc_service_attachments[1].service_attachment
109+
}
110+
111+
resource "google_compute_address" "ip1_network2" {
112+
name = "ip1-net2-${local.name_suffix}"
113+
region = "us-central1"
114+
subnetwork = google_compute_subnetwork.subnet_network2.id
115+
address_type = "INTERNAL"
116+
purpose = "GCE_ENDPOINT"
117+
}
118+
119+
resource "google_compute_address" "ip2_network2" {
120+
name = "ip2-net2-${local.name_suffix}"
121+
region = "us-central1"
122+
subnetwork = google_compute_subnetwork.subnet_network2.id
123+
address_type = "INTERNAL"
124+
purpose = "GCE_ENDPOINT"
125+
}
126+
127+
resource "google_compute_subnetwork" "subnet_network2" {
128+
name = "subnet-net2-${local.name_suffix}"
129+
ip_cidr_range = "10.0.0.248/29"
130+
region = "us-central1"
131+
network = google_compute_network.network2.id
132+
}
133+
134+
resource "google_compute_network" "network2" {
135+
name = "network2-${local.name_suffix}"
136+
auto_create_subnetworks = false
137+
}
138+
139+
// redis cluster without endpoint
140+
resource "google_redis_cluster" "cluster-user-conn" {
141+
name = "cluster-user-conn-${local.name_suffix}"
142+
shard_count = 3
143+
region = "us-central1"
144+
replica_count = 0
145+
deletion_protection_enabled = false
146+
}
147+
148+
data "google_project" "project" {
149+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
===
2+
3+
These examples use real resources that will be billed to the
4+
Google Cloud Platform project you use - so make sure that you
5+
run "terraform destroy" before quitting!
6+
7+
===

0 commit comments

Comments
 (0)