Skip to content

Commit 9005920

Browse files
Add an example for connecting to a private SSM instance using PSC (#11150) (#750)
[upstream:3d79f2e56865fcaf00f7d24b4632e59b1773ade6] Signed-off-by: Modular Magician <[email protected]>
1 parent c665f9c commit 9005920

File tree

9 files changed

+528
-3
lines changed

9 files changed

+528
-3
lines changed

secure_source_manager_instance_private/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ resource "google_secure_source_manager_instance" "default" {
6262
}
6363
depends_on = [
6464
google_privateca_certificate_authority.root_ca,
65-
time_sleep.wait_60_seconds
65+
time_sleep.wait_120_seconds
6666
]
6767
}
6868

6969
# ca pool IAM permissions can take time to propagate
70-
resource "time_sleep" "wait_60_seconds" {
70+
resource "time_sleep" "wait_120_seconds" {
7171
depends_on = [google_privateca_ca_pool_iam_binding.ca_pool_binding]
7272

73-
create_duration = "60s"
73+
create_duration = "120s"
7474
}
7575

7676
data "google_project" "project" {}
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: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
data "google_project" "project" {}
2+
3+
resource "google_privateca_ca_pool" "ca_pool" {
4+
name = "ca-pool-${local.name_suffix}"
5+
location = "us-central1"
6+
tier = "ENTERPRISE"
7+
publishing_options {
8+
publish_ca_cert = true
9+
publish_crl = true
10+
}
11+
}
12+
13+
resource "google_privateca_certificate_authority" "root_ca" {
14+
pool = google_privateca_ca_pool.ca_pool.name
15+
certificate_authority_id = "root-ca-${local.name_suffix}"
16+
location = "us-central1"
17+
config {
18+
subject_config {
19+
subject {
20+
organization = "google"
21+
common_name = "my-certificate-authority"
22+
}
23+
}
24+
x509_config {
25+
ca_options {
26+
is_ca = true
27+
}
28+
key_usage {
29+
base_key_usage {
30+
cert_sign = true
31+
crl_sign = true
32+
}
33+
extended_key_usage {
34+
server_auth = true
35+
}
36+
}
37+
}
38+
}
39+
key_spec {
40+
algorithm = "RSA_PKCS1_4096_SHA256"
41+
}
42+
43+
// Disable deletion protections for easier test cleanup purposes
44+
deletion_protection = false
45+
ignore_active_certificates_on_deletion = true
46+
skip_grace_period = true
47+
}
48+
49+
resource "google_privateca_ca_pool_iam_binding" "ca_pool_binding" {
50+
ca_pool = google_privateca_ca_pool.ca_pool.id
51+
role = "roles/privateca.certificateRequester"
52+
53+
members = [
54+
"serviceAccount:service-${data.google_project.project.number}@gcp-sa-sourcemanager.iam.gserviceaccount.com"
55+
]
56+
}
57+
58+
// See https://cloud.google.com/secure-source-manager/docs/create-private-service-connect-instance#root-ca-api
59+
resource "google_secure_source_manager_instance" "default" {
60+
instance_id = "my-instance-${local.name_suffix}"
61+
location = "us-central1"
62+
private_config {
63+
is_private = true
64+
ca_pool = google_privateca_ca_pool.ca_pool.id
65+
}
66+
depends_on = [
67+
google_privateca_certificate_authority.root_ca,
68+
time_sleep.wait_120_seconds
69+
]
70+
}
71+
72+
# ca pool IAM permissions can take time to propagate
73+
resource "time_sleep" "wait_120_seconds" {
74+
depends_on = [google_privateca_ca_pool_iam_binding.ca_pool_binding]
75+
76+
create_duration = "120s"
77+
}
78+
79+
// Connect SSM private instance with L4 proxy ILB.
80+
resource "google_compute_network" "network" {
81+
name = "my-network-${local.name_suffix}"
82+
auto_create_subnetworks = false
83+
}
84+
85+
resource "google_compute_subnetwork" "subnet" {
86+
name = "my-subnet-${local.name_suffix}"
87+
region = "us-central1"
88+
network = google_compute_network.network.id
89+
ip_cidr_range = "10.0.1.0/24"
90+
private_ip_google_access = true
91+
}
92+
93+
resource "google_compute_region_network_endpoint_group" "psc_neg" {
94+
name = "my-neg-${local.name_suffix}"
95+
region = "us-central1"
96+
97+
network_endpoint_type = "PRIVATE_SERVICE_CONNECT"
98+
psc_target_service = google_secure_source_manager_instance.default.private_config.0.http_service_attachment
99+
100+
network = google_compute_network.network.id
101+
subnetwork = google_compute_subnetwork.subnet.id
102+
}
103+
104+
resource "google_compute_region_backend_service" "backend_service" {
105+
name = "my-backend-service-${local.name_suffix}"
106+
region = "us-central1"
107+
protocol = "TCP"
108+
load_balancing_scheme = "INTERNAL_MANAGED"
109+
backend {
110+
group = google_compute_region_network_endpoint_group.psc_neg.id
111+
balancing_mode = "UTILIZATION"
112+
capacity_scaler = 1.0
113+
}
114+
}
115+
116+
resource "google_compute_subnetwork" "proxy_subnet" {
117+
name = "my-proxy-subnet-${local.name_suffix}"
118+
region = "us-central1"
119+
network = google_compute_network.network.id
120+
ip_cidr_range = "10.0.2.0/24"
121+
purpose = "REGIONAL_MANAGED_PROXY"
122+
role = "ACTIVE"
123+
}
124+
125+
resource "google_compute_region_target_tcp_proxy" "target_proxy" {
126+
name = "my-target-proxy-${local.name_suffix}"
127+
region = "us-central1"
128+
backend_service = google_compute_region_backend_service.backend_service.id
129+
}
130+
131+
resource "google_compute_forwarding_rule" "fw_rule_target_proxy" {
132+
name = "fw-rule-target-proxy-${local.name_suffix}"
133+
region = "us-central1"
134+
135+
load_balancing_scheme = "INTERNAL_MANAGED"
136+
ip_protocol = "TCP"
137+
port_range = "443"
138+
target = google_compute_region_target_tcp_proxy.target_proxy.id
139+
network = google_compute_network.network.id
140+
subnetwork = google_compute_subnetwork.subnet.id
141+
network_tier = "PREMIUM"
142+
depends_on = [google_compute_subnetwork.proxy_subnet]
143+
}
144+
145+
resource "google_dns_managed_zone" "private_zone" {
146+
name = "my-dns-zone-${local.name_suffix}"
147+
dns_name = "p.sourcemanager.dev."
148+
visibility = "private"
149+
private_visibility_config {
150+
networks {
151+
network_url = google_compute_network.network.id
152+
}
153+
}
154+
}
155+
156+
resource "google_dns_record_set" "ssm_instance_html_record" {
157+
name = "${google_secure_source_manager_instance.default.host_config.0.html}."
158+
type = "A"
159+
ttl = 300
160+
managed_zone = google_dns_managed_zone.private_zone.name
161+
rrdatas = [google_compute_forwarding_rule.fw_rule_target_proxy.ip_address]
162+
}
163+
164+
resource "google_dns_record_set" "ssm_instance_api_record" {
165+
name = "${google_secure_source_manager_instance.default.host_config.0.api}."
166+
type = "A"
167+
ttl = 300
168+
managed_zone = google_dns_managed_zone.private_zone.name
169+
rrdatas = [google_compute_forwarding_rule.fw_rule_target_proxy.ip_address]
170+
}
171+
172+
resource "google_dns_record_set" "ssm_instance_git_record" {
173+
name = "${google_secure_source_manager_instance.default.host_config.0.git_http}."
174+
type = "A"
175+
ttl = 300
176+
managed_zone = google_dns_managed_zone.private_zone.name
177+
rrdatas = [google_compute_forwarding_rule.fw_rule_target_proxy.ip_address]
178+
}
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+
# Secure Source Manager Instance Private Psc Backend - Terraform
2+
3+
## Setup
4+
5+
<walkthrough-author name="[email protected]" analyticsId="UA-125550242-1" tutorialName="secure_source_manager_instance_private_psc_backend" 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+
}

0 commit comments

Comments
 (0)