Skip to content

Commit 840ecc4

Browse files
authored
Merge pull request #45 from terraform-google-modules/fabric-svpc-submodule
Second take at Shared VPC configuration.
2 parents 71c3b30 + 94c6e7e commit 840ecc4

File tree

10 files changed

+294
-0
lines changed

10 files changed

+294
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Then perform the following commands on the root folder:
102102
| subnets\_regions | The region where the subnets will be created |
103103
| subnets\_secondary\_ranges | The secondary ranges associated with these subnets |
104104
| subnets\_self\_links | The self-links of subnets being created |
105+
| svpc\_host\_project\_id | Shared VPC host project id. |
105106

106107
[^]: (autogen_docs_end)
107108

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Shared VPC with service projects
2+
3+
This simple example configures a shared VPC, and grants access to it to service projects.
4+
5+
The VPC has two subnets with no secondary ranges, service projects are configured as follows:
6+
7+
- the first service project is granted VPC-level access
8+
- the second service project is granted subnet-level access to the second subnet
9+
- the third service project is granted subnet-level access to the first and second subnet
10+
11+
Subnet-level access in this example is only granted to the default GCE service accounts for illustrative purposes. More realistic examples should grant access to other service accounts (possibly including the GKE robot service accounts as per [documentation](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc)), and project users/groups that need to use the Shared VPC from other projects (eg to create VMs).
12+
13+
[^]: (autogen_docs_start)
14+
15+
## Inputs
16+
17+
| Name | Description | Type | Default | Required |
18+
|------|-------------|:----:|:-----:|:-----:|
19+
| host\_project\_id | Id of the host project where the shared VPC will be created. | string | n/a | yes |
20+
| network\_name | Name of the shared VPC. | string | `"test-svpc"` | no |
21+
| service\_project\_id\_full\_access | Id of the service project that will get VPC-level access. | string | n/a | yes |
22+
| service\_project\_number\_first\_subnet | Project number to derive service accounts with access to first subnet. | string | n/a | yes |
23+
| service\_project\_number\_multi\_subnet | Project number to derive service accounts with access to first and second subnet. | string | n/a | yes |
24+
25+
[^]: (autogen_docs_end)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
first_gce_sa = "serviceAccount:${var.service_project_number_first_subnet}[email protected]"
19+
second_gce_sa = "serviceAccount:${var.service_project_number_multi_subnet}[email protected]"
20+
}
21+
22+
module "net-vpc-shared" {
23+
source = "../../"
24+
project_id = "${var.host_project_id}"
25+
network_name = "${var.network_name}"
26+
shared_vpc_host = "true"
27+
28+
subnets = [
29+
{
30+
subnet_name = "first"
31+
subnet_ip = "10.10.10.0/24"
32+
subnet_region = "europe-west1"
33+
},
34+
{
35+
subnet_name = "second"
36+
subnet_ip = "10.10.20.0/24"
37+
subnet_region = "europe-west1"
38+
},
39+
]
40+
41+
secondary_ranges = {
42+
first = []
43+
second = []
44+
}
45+
}
46+
47+
module "net-svpc-access" {
48+
source = "../../modules/fabric-net-svpc-access"
49+
host_project_id = "${module.net-vpc-shared.svpc_host_project_id}"
50+
service_project_num = 1
51+
service_project_ids = ["${var.service_project_id_full_access}"]
52+
host_subnets = ["${module.net-vpc-shared.subnets_names}"]
53+
host_subnet_regions = ["${module.net-vpc-shared.subnets_regions}"]
54+
55+
host_subnet_users = {
56+
first = "${local.first_gce_sa},${local.second_gce_sa}"
57+
second = "${local.second_gce_sa}"
58+
}
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "host_project_id" {
18+
description = "Id of the host project where the shared VPC will be created."
19+
}
20+
21+
variable "service_project_id_full_access" {
22+
description = "Id of the service project that will get VPC-level access."
23+
}
24+
25+
variable "service_project_number_first_subnet" {
26+
description = "Project number to derive service accounts with access to first subnet."
27+
}
28+
29+
variable "service_project_number_multi_subnet" {
30+
description = "Project number to derive service accounts with access to first and second subnet."
31+
}
32+
33+
variable "network_name" {
34+
description = "Name of the shared VPC."
35+
default = "test-svpc"
36+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Google Cloud Shared VPC Access Configuration
2+
3+
This module allows configuring service project access to a Shared VPC, created with the top-level network module. Two configuration modes for each service project are supported:
4+
5+
- VPC access, where service projects are granted IAM roles at the host project level, and can use any of the VPC subnets
6+
- subnetwork access, where service projects are granted IAM roles at the subnet level, and can then only use specific subnets
7+
8+
Full details on service project configuration can be found in the Google Cloud documentation on [provisioning Shared VPC](https://cloud.google.com/vpc/docs/provisioning-shared-vpc), and on [setting up clusters with Shared VPC](https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc).
9+
10+
The resources created/managed by this module are:
11+
12+
- one `google_compute_shared_vpc_service_project` resource for each project where full VPC access is needed
13+
- one `google_compute_subnetwork_iam_binding` for each subnetwork where individual subnetwork access is needed
14+
15+
## Usage
16+
17+
Basic usage of this module is as follows:
18+
19+
```hcl
20+
module "net-shared-vpc-access" {
21+
source = "terraform-google-modules/terraform-google-network/google//modules/fabric-net-svpc-access"
22+
host_project_id = "my-host-project-id"
23+
service_project_num = 1
24+
service_project_ids = ["my-service-project-id"]
25+
host_subnets = ["my-subnet-1", "my-subnet-2"]
26+
host_subnet_regions = ["europe-west1", "europe-west1]
27+
host_subnet_users = [
28+
"serviceAccount:${module.project-foo.gce_service_account}",
29+
"serviceAccount:${module.project-spam.gce_service_account},group:[email protected]"
30+
]
31+
}
32+
```
33+
34+
[^]: (autogen_docs_start)
35+
36+
## Inputs
37+
38+
| Name | Description | Type | Default | Required |
39+
|------|-------------|:----:|:-----:|:-----:|
40+
| host\_project\_id | Project id of the shared VPC host project. | string | n/a | yes |
41+
| host\_subnet\_regions | List of subnet regions, one per subnet. | list | `<list>` | no |
42+
| host\_subnet\_users | Map of comma-delimited IAM-style members, one per subnet. | map | `<map>` | no |
43+
| host\_subnets | List of subnet names on which to grant access. | list | `<list>` | no |
44+
| service\_project\_ids | Ids of the service projects that will be granted access to all subnetworks. | list | n/a | yes |
45+
| service\_project\_num | Number of service projects that will be granted access to all subnetworks. | string | `"0"` | no |
46+
47+
## Outputs
48+
49+
| Name | Description |
50+
|------|-------------|
51+
| service\_projects | Project ids of the services with access to all subnets. |
52+
53+
[^]: (autogen_docs_end)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
resource "google_compute_shared_vpc_service_project" "projects" {
18+
count = "${var.service_project_num}"
19+
host_project = "${var.host_project_id}"
20+
service_project = "${element(var.service_project_ids, count.index)}"
21+
}
22+
23+
resource "google_compute_subnetwork_iam_binding" "subnets" {
24+
count = "${length(var.host_subnets)}"
25+
project = "${var.host_project_id}"
26+
region = "${element(var.host_subnet_regions, count.index)}"
27+
subnetwork = "${element(var.host_subnets, count.index)}"
28+
role = "roles/compute.networkUser"
29+
30+
members = ["${compact(split(",",
31+
lookup(var.host_subnet_users, element(var.host_subnets, count.index))
32+
))}"]
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "service_projects" {
18+
description = "Project ids of the services with access to all subnets."
19+
value = ["${google_compute_shared_vpc_service_project.projects.*.service_project}"]
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "host_project_id" {
18+
description = "Project id of the shared VPC host project."
19+
}
20+
21+
# passed-in values can be dynamic, so variables used in count need to be separate
22+
23+
variable "service_project_num" {
24+
description = "Number of service projects that will be granted access to all subnetworks."
25+
default = 0
26+
}
27+
28+
variable "service_project_ids" {
29+
description = "Ids of the service projects that will be granted access to all subnetworks."
30+
type = "list"
31+
}
32+
33+
variable "host_subnets" {
34+
description = "List of subnet names on which to grant access."
35+
default = []
36+
}
37+
38+
variable "host_subnet_regions" {
39+
description = "List of subnet regions, one per subnet."
40+
default = []
41+
}
42+
43+
variable "host_subnet_users" {
44+
description = "Map of comma-delimited IAM-style members, one per subnet."
45+
default = {}
46+
}

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ output "network_self_link" {
2424
description = "The URI of the VPC being created"
2525
}
2626

27+
output "svpc_host_project_id" {
28+
value = "${element(concat(google_compute_shared_vpc_host_project.shared_vpc_host.*.project, list("")), 0)}"
29+
description = "Shared VPC host project id."
30+
}
31+
2732
output "subnets_names" {
2833
value = "${google_compute_subnetwork.subnetwork.*.name}"
2934
description = "The names of the subnets being created"

0 commit comments

Comments
 (0)