Skip to content

Commit

Permalink
Add checks to guarantee resource creation order
Browse files Browse the repository at this point in the history
  • Loading branch information
bbhuston committed Apr 11, 2023
1 parent 24df96f commit 38bd3c3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
1 change: 0 additions & 1 deletion infrastructure/org-policies/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ terraform {
google = {
source = "hashicorp/google"
version = "4.56.0"
user_project_override = true
}
}

Expand Down
8 changes: 8 additions & 0 deletions infrastructure/org-policies/org-policies.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module "gcp_org_policy_v2_requireShieldedVm" {
}]
constraint = "compute.requireShieldedVm"
policy_type = "boolean"

depends_on = [google_project_service.project]
}

module "gcp_org_policy_v2_disableServiceAccountKeyCreation" {
Expand All @@ -43,6 +45,8 @@ module "gcp_org_policy_v2_disableServiceAccountKeyCreation" {
}]
constraint = "iam.disableServiceAccountKeyCreation"
policy_type = "boolean"

depends_on = [google_project_service.project]
}

module "gcp_org_policy_v2_vmCanIpForward" {
Expand All @@ -59,6 +63,8 @@ module "gcp_org_policy_v2_vmCanIpForward" {
}]
constraint = "compute.vmCanIpForward"
policy_type = "list"

depends_on = [google_project_service.project]
}

module "gcp_org_policy_v2_vmExternalIpAccess" {
Expand All @@ -75,4 +81,6 @@ module "gcp_org_policy_v2_vmExternalIpAccess" {
}]
constraint = "compute.vmExternalIpAccess"
policy_type = "list"

depends_on = [google_project_service.project]
}
26 changes: 21 additions & 5 deletions infrastructure/org-policies/project-services.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

resource "google_project_services" "project" {
resource "google_project_service" "project" {
project = var.project
services = ["iam.googleapis.com",
"cloudresourcemanager.googleapis.com",
"orgpolicy.googleapis.com"
]
for_each = toset(var.gcp_project_services)
service = each.value

timeouts {
create = "30m"
update = "40m"
}

# Ensure service is truly active before continuing onward
provisioner "local-exec" {
command = <<EOF
while [ ! $(gcloud services list --project=${var.project} | grep ${each.value} | wc -l ) ];
do
sleep 1s
done
EOF
}

disable_dependent_services = false
disable_on_destroy = false
}
8 changes: 7 additions & 1 deletion infrastructure/org-policies/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

### Project Variables ###
# Project Variables

variable "project" {
type = string
description = "GCP Project ID"
}

variable "gcp_project_services" {
type = list(any)
description = "GCP Service APIs (<api>.googleapis.com) to enable for this project"
default = []
}

0 comments on commit 38bd3c3

Please sign in to comment.