Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #12 from padok-team/fix/lifecycle_issue
Browse files Browse the repository at this point in the history
fix(main.tf)!: error when replacing backend
  • Loading branch information
nlevee authored Feb 11, 2022
2 parents 3f24268 + d30199c commit a860958
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
47 changes: 44 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,25 @@ resource "google_compute_global_forwarding_rule" "http" {
# BACKEND SERVICES
# -----------------------------
# SERVICE

# create a random_id to suffix `google_compute_backend_service.this`
# > required to enable `lifecycle.create_before_destroy`
resource "random_id" "backend_service" {
for_each = var.service_backends

byte_length = 4
prefix = "${var.name}-${each.key}-"

keepers = {
security_policy = each.value.security_policy
groups = join(",", each.value["groups"])
}
}

resource "google_compute_backend_service" "this" {
for_each = var.service_backends
name = "${var.name}-${each.key}"
for_each = var.service_backends

name = random_id.backend_service[each.key].hex
security_policy = each.value.security_policy
enable_cdn = false

Expand All @@ -119,13 +135,33 @@ resource "google_compute_backend_service" "this" {
group = backend.value
}
}

lifecycle {
# Need because url_map updating after terraform try to delete the resource
create_before_destroy = true
}
}

# BUCKET

# create a random_id to suffix `google_compute_backend_bucket.this`
# > required to enable `lifecycle.create_before_destroy`
resource "random_id" "backend_bucket" {
for_each = var.buckets_backends

byte_length = 4
prefix = "${var.name}-${each.key}-"

keepers = {
bucket_name = each.value.bucket_name
cdn_policies = each.value.cdn_policy == null ? jsonencode(local.cdn_policies) : ""
}
}

resource "google_compute_backend_bucket" "this" {
for_each = var.buckets_backends

name = "${var.name}-${each.key}"
name = random_id.backend_bucket[each.key].hex
bucket_name = each.value.bucket_name
enable_cdn = each.value.cdn_policy == null ? false : true

Expand All @@ -149,4 +185,9 @@ resource "google_compute_backend_bucket" "this" {
}
}
}

lifecycle {
# Need because url_map updating after terraform try to delete the resource
create_before_destroy = true
}
}
51 changes: 51 additions & 0 deletions prettier.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
BLACK := $(shell tput setaf 0)
RED := $(shell tput setaf 1)
GREEN := $(shell tput setaf 2)
YELLOW := $(shell tput setaf 3)
LIGHTPURPLE := $(shell tput setaf 4)
PURPLE := $(shell tput setaf 5)
BLUE := $(shell tput setaf 6)
WHITE := $(shell tput setaf 7)
BOLD := $(shell tput bold)
RESET := $(shell tput sgr0)

##@ Prerequesites
.PHONY: install_tools
install_tools: ## Install all the necessary tools
$(call info_colors,green,👏 Installing Precommit)
@brew install pre-commit
$(call info_colors,blue,👏 Installing Terraform Docs)
@brew install terraform-docs
$(call info_colors,purple,👏 Installing Standard-Version)
npm install -g standard-version




.PHONY: colors
##@ Support
colors: ## show all the colors
$(call info_colors,yellow,Display all colors)

@echo "${BLACK}BLACK${RESET}"
@echo "${RED}RED${RESET}"
@echo "${GREEN}GREEN${RESET}"
@echo "${YELLOW}YELLOW${RESET}"
@echo "${LIGHTPURPLE}LIGHTPURPLE${RESET}"
@echo "${PURPLE}PURPLE${RESET}"
@echo "${BLUE}BLUE${RESET}"
@echo "${WHITE}WHITE${RESET}"
@echo "${BOLD}BOLD${RESET}"

## Upper case
UC = $(shell echo '$1' | tr '[:lower:]' '[:upper:]')

define info_colors
$(info $(BOLD)$($(call UC,$1))$(2)$(RESET))
endef

.PHONY: help
##@ Help
help: ## Displays the list of functions
$(call info_colors,purple,❓ Help)
@awk 'BEGIN {FS = ":.*##"; printf "Usage: make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-10s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
7 changes: 6 additions & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ terraform {
required_providers {
google = {
source = "hashicorp/google"
version = ">= 3.9.0"
version = "~> 3.9.0"
}

random = {
source = "hashicorp/random"
version = "~> 3.1.0"
}
}
}

0 comments on commit a860958

Please sign in to comment.