Skip to content

Commit 7eedbe5

Browse files
authored
feat: Added dev_folder construct (#46)
* Added dev_folder construct * Moved to local, renamed parent_folder * Removing extra check-in
1 parent 8c93cf1 commit 7eedbe5

File tree

14 files changed

+96
-28
lines changed

14 files changed

+96
-28
lines changed

0-bootstrap/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Further details of permissions required and resources created, can be found in t
3131
| group\_billing\_admins | Google Group for GCP Billing Administrators | string | n/a | yes |
3232
| group\_org\_admins | Google Group for GCP Organization Administrators | string | n/a | yes |
3333
| org\_id | GCP Organization ID | string | n/a | yes |
34+
| parent\_folder | Optional - if using a folder for testing. | string | `""` | no |
3435

3536
## Outputs
3637

0-bootstrap/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,20 @@ provider "random" {
3333
/*************************************************
3434
Bootstrap GCP Organization.
3535
*************************************************/
36+
locals {
37+
parent = var.parent_folder != "" ? "folders/${var.parent_folder}" : "organizations/${var.org_id}"
38+
}
39+
40+
resource "google_folder" "seed" {
41+
display_name = "seed"
42+
parent = local.parent
43+
}
3644

3745
module "seed_bootstrap" {
3846
source = "terraform-google-modules/bootstrap/google"
3947
version = "~> 1.0"
4048
org_id = var.org_id
49+
folder_id = google_folder.seed.id
4150
billing_account = var.billing_account
4251
group_org_admins = var.group_org_admins
4352
group_billing_admins = var.group_billing_admins
@@ -49,6 +58,7 @@ module "cloudbuild_bootstrap" {
4958
source = "terraform-google-modules/bootstrap/google//modules/cloudbuild"
5059
version = "~> 1.0"
5160
org_id = var.org_id
61+
folder_id = google_folder.seed.id
5262
billing_account = var.billing_account
5363
group_org_admins = var.group_org_admins
5464
default_region = var.default_region

0-bootstrap/terraform.example.tfvars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ group_org_admins = "[email protected]"
2323
group_billing_admins = "[email protected]"
2424

2525
default_region = "australia-southeast1"
26+
27+
//Optional - for development. Will place all resources under a specific folder instead of org root
28+
//parent_folder = "01234567890"

0-bootstrap/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,8 @@ variable "default_region" {
4040
default = "us-central1"
4141
}
4242

43+
variable "parent_folder" {
44+
description = "Optional - if using a folder for testing."
45+
type = string
46+
default = ""
47+
}

1-org/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ The purpose of this step is to setup top level shared folders, monitoring & netw
4444
| domains\_to\_allow | The list of domains to allow users from in IAM. | list(string) | n/a | yes |
4545
| monitoring\_workspace\_users | Gsuite or Cloud Identity group that have access to Monitoring Workspaces. | string | n/a | yes |
4646
| org\_id | The organization id for the associated services | string | n/a | yes |
47+
| parent\_folder | Optional - if using a folder for testing. | string | `""` | no |
4748
| system\_event\_table\_expiration\_ms | Period before tables expire for system event logs in milliseconds. Default is 400 days. | number | `"34560000000"` | no |
4849
| terraform\_service\_account | Service account email of the account to impersonate to run Terraform. | string | n/a | yes |
4950

1-org/folders.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17+
locals {
18+
parent = var.parent_folder != "" ? "folders/${var.parent_folder}" : "organizations/${var.org_id}"
19+
}
20+
1721
/******************************************
1822
Top level folders
1923
*****************************************/
2024

2125
resource "google_folder" "common" {
2226
display_name = "common"
23-
parent = "organizations/${var.org_id}"
27+
parent = local.parent
2428
}
2529

2630
/******************************************

1-org/log_sinks.tf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
locals {
18+
parent_resource_id = var.parent_folder != "" ? var.parent_folder : var.org_id
19+
parent_resource_type = var.parent_folder != "" ? "folder" : "organization"
20+
}
21+
1722
/******************************************
1823
Audit Logs - Activity
1924
*****************************************/
@@ -24,8 +29,8 @@ module "log_export_activity_logs" {
2429
destination_uri = module.bq_activity_logs.destination_uri
2530
filter = "logName: \"/logs/cloudaudit.googleapis.com%2Factivity\""
2631
log_sink_name = "bigquery_activity_logs"
27-
parent_resource_id = var.org_id
28-
parent_resource_type = "organization"
32+
parent_resource_id = local.parent_resource_id
33+
parent_resource_type = local.parent_resource_type
2934
unique_writer_identity = true
3035
}
3136

@@ -48,8 +53,8 @@ module "log_export_system_event_logs" {
4853
destination_uri = module.bq_system_event_logs.destination_uri
4954
filter = "logName: \"/logs/cloudaudit.googleapis.com%2Fsystem_event\""
5055
log_sink_name = "bigquery_system_event_logs"
51-
parent_resource_id = var.org_id
52-
parent_resource_type = "organization"
56+
parent_resource_id = local.parent_resource_id
57+
parent_resource_type = local.parent_resource_type
5358
unique_writer_identity = true
5459
}
5560

@@ -73,8 +78,8 @@ module "log_export_data_access_logs" {
7378
destination_uri = module.bq_data_access_logs.destination_uri
7479
filter = "logName: \"/logs/cloudaudit.googleapis.com%2Fdata_access\""
7580
log_sink_name = "bigquery_data_access_logs"
76-
parent_resource_id = var.org_id
77-
parent_resource_type = "organization"
81+
parent_resource_id = local.parent_resource_id
82+
parent_resource_type = local.parent_resource_type
7883
unique_writer_identity = true
7984
}
8085

1-org/org_policy.tf

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@
1414
* limitations under the License.
1515
*/
1616

17+
locals {
18+
organization_id = var.parent_folder != "" ? null : var.org_id
19+
folder_id = var.parent_folder != "" ? var.parent_folder : null
20+
policy_for = var.parent_folder != "" ? "folder" : "organization"
21+
}
22+
23+
1724
/******************************************
1825
Compute org policies
1926
*******************************************/
2027

2128
module "org_disable_nested_virtualization" {
2229
source = "terraform-google-modules/org-policy/google"
2330
version = "~> 3.0"
24-
organization_id = var.org_id
25-
policy_for = "organization"
31+
organization_id = local.organization_id
32+
folder_id = local.folder_id
33+
policy_for = local.policy_for
2634
policy_type = "boolean"
2735
enforce = "true"
2836
constraint = "constraints/compute.disableNestedVirtualization"
@@ -31,8 +39,9 @@ module "org_disable_nested_virtualization" {
3139
module "org_disable_serial_port_access" {
3240
source = "terraform-google-modules/org-policy/google"
3341
version = "~> 3.0"
34-
organization_id = var.org_id
35-
policy_for = "organization"
42+
organization_id = local.organization_id
43+
folder_id = local.folder_id
44+
policy_for = local.policy_for
3645
policy_type = "boolean"
3746
enforce = "true"
3847
constraint = "compute.disableSerialPortAccess"
@@ -41,8 +50,9 @@ module "org_disable_serial_port_access" {
4150
module "org_compute_disable_guest_attributes_access" {
4251
source = "terraform-google-modules/org-policy/google"
4352
version = "~> 3.0"
44-
organization_id = var.org_id
45-
policy_for = "organization"
53+
organization_id = local.organization_id
54+
folder_id = local.folder_id
55+
policy_for = local.policy_for
4656
policy_type = "boolean"
4757
enforce = "true"
4858
constraint = "constraints/compute.disableGuestAttributesAccess"
@@ -51,8 +61,9 @@ module "org_compute_disable_guest_attributes_access" {
5161
module "org_vm_external_ip_access" {
5262
source = "terraform-google-modules/org-policy/google"
5363
version = "~> 3.0"
54-
organization_id = var.org_id
55-
policy_for = "organization"
64+
organization_id = local.organization_id
65+
folder_id = local.folder_id
66+
policy_for = local.policy_for
5667
policy_type = "list"
5768
enforce = "true"
5869
constraint = "constraints/compute.vmExternalIpAccess"
@@ -61,8 +72,9 @@ module "org_vm_external_ip_access" {
6172
module "org_skip_default_network" {
6273
source = "terraform-google-modules/org-policy/google"
6374
version = "~> 3.0"
64-
organization_id = var.org_id
65-
policy_for = "organization"
75+
organization_id = local.organization_id
76+
folder_id = local.folder_id
77+
policy_for = local.policy_for
6678
policy_type = "boolean"
6779
enforce = "true"
6880
constraint = "constraints/compute.skipDefaultNetworkCreation"
@@ -71,8 +83,9 @@ module "org_skip_default_network" {
7183
module "org_shared_vpc_lien_removal" {
7284
source = "terraform-google-modules/org-policy/google"
7385
version = "~> 3.0"
74-
organization_id = var.org_id
75-
policy_for = "organization"
86+
organization_id = local.organization_id
87+
folder_id = local.folder_id
88+
policy_for = local.policy_for
7689
policy_type = "boolean"
7790
enforce = "true"
7891
constraint = "constraints/compute.restrictXpnProjectLienRemoval"
@@ -85,8 +98,9 @@ module "org_shared_vpc_lien_removal" {
8598
module "org_cloudsql_external_ip_access" {
8699
source = "terraform-google-modules/org-policy/google"
87100
version = "~> 3.0"
88-
organization_id = var.org_id
89-
policy_for = "organization"
101+
organization_id = local.organization_id
102+
folder_id = local.folder_id
103+
policy_for = local.policy_for
90104
policy_type = "boolean"
91105
enforce = "true"
92106
constraint = "constraints/sql.restrictPublicIp"
@@ -99,16 +113,18 @@ module "org_cloudsql_external_ip_access" {
99113
module "org_domain_restricted_sharing" {
100114
source = "terraform-google-modules/org-policy/google//modules/domain_restricted_sharing"
101115
version = "~> 3.0"
102-
organization_id = var.org_id
103-
policy_for = "organization"
116+
organization_id = local.organization_id
117+
folder_id = local.folder_id
118+
policy_for = local.policy_for
104119
domains_to_allow = var.domains_to_allow
105120
}
106121

107122
module "org_disable_sa_key_creation" {
108123
source = "terraform-google-modules/org-policy/google"
109124
version = "~> 3.0"
110-
organization_id = var.org_id
111-
policy_for = "organization"
125+
organization_id = local.organization_id
126+
folder_id = local.folder_id
127+
policy_for = local.policy_for
112128
policy_type = "boolean"
113129
enforce = "true"
114130
constraint = "constraints/iam.disableServiceAccountKeyCreation"
@@ -121,8 +137,9 @@ module "org_disable_sa_key_creation" {
121137
module "org_enforce_bucket_level_access" {
122138
source = "terraform-google-modules/org-policy/google"
123139
version = "~> 3.0"
124-
organization_id = var.org_id
125-
policy_for = "organization"
140+
organization_id = local.organization_id
141+
folder_id = local.folder_id
142+
policy_for = local.policy_for
126143
policy_type = "boolean"
127144
enforce = "true"
128145
constraint = "constraints/storage.uniformBucketLevelAccess"

1-org/terraform.example.tfvars

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ billing_account = "000000-000000-000000"
2929
terraform_service_account = "[email protected]"
3030

3131
default_region = "australia-southeast1"
32+
33+
//Optional - for development. Will place all resources under a specific folder instead of org root
34+
//parent_folder = "01234567890"

1-org/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ variable "data_access_table_expiration_ms" {
7272
default = 2592000000
7373
}
7474

75+
variable "parent_folder" {
76+
description = "Optional - if using a folder for testing."
77+
type = string
78+
default = ""
79+
}

0 commit comments

Comments
 (0)