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

Commit

Permalink
Merge pull request #188 from hazelops/tf-backend-upd
Browse files Browse the repository at this point in the history
CORE-252 Tf backend upd
  • Loading branch information
igorkotof authored Dec 31, 2021
2 parents b1523a4 + ecf72c2 commit 5888ed5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 30 deletions.
8 changes: 4 additions & 4 deletions main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ ifneq (,$(TIER))
endif

# Get Service sub-directory name in "projects" folder. We're parsing Make task name and extracting PROJECT_SUB_DIR. So baz/foo.bar will result to PROJECT_SUB_DIR=baz
PROJECT_SUB_DIR = $(shell echo $(@) | grep $(SLASHSIGN) > /dev/null && echo $$(echo $(@) | $(CUT) -d/ -f1)$(SLASHSIGN) || echo "")
PROJECT_ROOT = projects/$(PROJECT_SUB_DIR)
PROJECT_PATH_ABS=$(shell cd $(PROJECT_ROOT)$(SVC) && pwd -P)
PROJECT_PATH = $(PROJECT_ROOT)$(shell basename $(PROJECT_PATH_ABS))
PROJECT_SUB_DIR ?= $(shell echo $(@) | grep $(SLASHSIGN) > /dev/null && echo $$(echo $(@) | $(CUT) -d/ -f1)$(SLASHSIGN) || echo "")
PROJECT_ROOT ?= projects/$(PROJECT_SUB_DIR)
PROJECT_PATH_ABS ?= $(shell cd $(PROJECT_ROOT)$(SVC) && pwd -P)
PROJECT_PATH ?= $(PROJECT_ROOT)$(shell basename $(PROJECT_PATH_ABS))
SERVICE_NAME ?= $(ENV)-$(SVC)
# Tasks
########################################################################################################################
Expand Down
63 changes: 37 additions & 26 deletions terraform/main.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Current value:$(shell grep -c "+ resource \"$(item)\"" $(ENV_DIR)/.terraform/tfp
\033[33m To request a service quota increase:\033[0m \033[36m aws service-quotas request-service-quota-increase --service-code $(shell echo $(AWS_LIMITS_LIST) | $(JQ) -r '.$(item)[].service') --quota-code $(shell echo $(AWS_LIMITS_LIST) | $(JQ) -r '.$(item)[].quotacode') --desired-value <your_desired_value> \033[0m"; fi )") )

# Terraform Backend Config
TERRAFORM_STATE_KEY = $(ENV)/terraform.tfstate
TERRAFORM_STATE_PROFILE = $(AWS_PROFILE)
TERRAFORM_STATE_REGION = $(AWS_REGION)
TERRAFORM_STATE_KEY ?= $(ENV)/terraform.tfstate
TERRAFORM_STATE_PROFILE ?= $(AWS_PROFILE)
TERRAFORM_STATE_REGION ?= $(AWS_REGION)
TERRAFORM_STATE_BUCKET_NAME ?= $(NAMESPACE)-tf-state
CHECKOV ?= $(DOCKER) run --user "$(CURRENT_USER_ID):$(CURRENT_USERGROUP_ID)" -v $(ENV_DIR):/tf -i bridgecrew/checkov -d /tf -s
TFLINT ?= $(DOCKER) run --user "$(CURRENT_USER_ID):$(CURRENT_USERGROUP_ID)" --rm -v $(ENV_DIR):/data -t wata727/tflint
Expand Down Expand Up @@ -47,6 +47,22 @@ CMD_SAVE_OUTPUT_TO_SSM = $(AWS) ssm put-parameter --name "/$(ENV)/terraform-outp

# Optional cmd to be used, because the branch related to TF v0.13 upgrade already have updated versions.tf files
CMD_TERRAFORM_MODULES_UPGRADE = $(shell find $(INFRA_DIR)/terraform -name '*.tf' | xargs -n1 dirname | uniq | xargs -n1 $(TERRAFORM) 0.13upgrade -yes)

CMD_TERRAFORM_INIT ?= @ cd $(ENV_DIR) && \
cat $(ICMK_TEMPLATE_TERRAFORM_BACKEND_CONFIG) | $(GOMPLATE) > backend.tf && \
cat $(ICMK_TEMPLATE_TERRAFORM_VARS) | $(GOMPLATE) > terraform.tfvars && \
$(TERRAFORM) init -input=true

CMD_TERRAFORM_PLAN ?= @ cd $(ENV_DIR) && \
$(TERRAFORM) plan -out=$(ENV_DIR)/.terraform/tfplan -input=false && \
$(TERRAFORM) show $(ENV_DIR)/.terraform/tfplan -input=false -no-color > $(ENV_DIR)/.terraform/tfplan.txt && \
cat $(ICMK_TEMPLATE_TERRAFORM_TFPLAN) | $(GOMPLATE) > $(ENV_DIR)/.terraform/tfplan.md

CMD_TERRAFORM_APPLY ?= @ cd $(ENV_DIR) && \
$(TERRAFORM) apply -input=false $(ENV_DIR)/.terraform/tfplan && \
$(TERRAFORM) output -json > $(ENV_DIR)/.terraform/output.json && \
$(CMD_SAVE_OUTPUT_TO_SSM)

# Tasks
########################################################################################################################
infra.init: terraform.init
Expand All @@ -64,11 +80,8 @@ terraform.debug:

# TODO: Potentionally replace gomplate by terragrunt
terraform.init: terraform.compat gomplate terraform
@ \
cd $(ENV_DIR) && \
cat $(ICMK_TEMPLATE_TERRAFORM_BACKEND_CONFIG) | $(GOMPLATE) > backend.tf && \
cat $(ICMK_TEMPLATE_TERRAFORM_VARS) | $(GOMPLATE) > terraform.tfvars && \
$(TERRAFORM) init -input=true
$(CMD_TERRAFORM_INIT)

# TODO: Potentionally replace gomplate by terragrunt
# TODO:? Implement -target approach so we can deploy specific apps only
# TODO: generate env vars into tfvars in only one task
Expand All @@ -84,25 +97,19 @@ terraform.reconfig:
cd $(ENV_DIR) && \
$(TERRAFORM) init -input=true -reconfigure

# Upgrading TF from v0.12 to v0.13
terraform.13upgrade:
@ echo "Terraform upgrade to v0.13 :"
@ echo "-----------------------------"
@ $(CMD_TERRAFORM_MODULES_UPGRADE)

terraform.apply: terraform.plan ## Deploy infrastructure
@ cd $(ENV_DIR) && \
$(TERRAFORM) apply -input=false $(ENV_DIR)/.terraform/tfplan && \
$(TERRAFORM) output -json > $(ENV_DIR)/.terraform/output.json && \
$(CMD_SAVE_OUTPUT_TO_SSM)
# TF Apply / Deploy infrastructure
terraform.apply: terraform.plan
$(CMD_TERRAFORM_APPLY)

terraform.checkov: ## Test infrastructure with checkov
## Test infrastructure with checkov
terraform.checkov:
@ echo "Testing with Checkov:"
@ echo "--------------------"
@ cd $(ENV_DIR)
@ $(CHECKOV)

terraform.tflint: ## Test infrastructure with tflint
## Test infrastructure with tflint
terraform.tflint:
@ echo "Testing with TFLint:"
@ echo "--------------------"
@ cd $(ENV_DIR)
Expand Down Expand Up @@ -131,15 +138,19 @@ terraform.output-to-ssm: ## Manual upload output.json to AWS SSM. Output.json en
@ cd $(ENV_DIR) && \
$(CMD_SAVE_OUTPUT_TO_SSM)

terraform.plan: terraform.init ## Terraform plan output for Github Action
@ cd $(ENV_DIR) && \
$(TERRAFORM) plan -out=$(ENV_DIR)/.terraform/tfplan -input=false && \
$(TERRAFORM) show $(ENV_DIR)/.terraform/tfplan -input=false -no-color > $(ENV_DIR)/.terraform/tfplan.txt && \
cat $(ICMK_TEMPLATE_TERRAFORM_TFPLAN) | $(GOMPLATE) > $(ENV_DIR)/.terraform/tfplan.md
## Terraform plan output for Github Action
terraform.plan: terraform.init
$(CMD_TERRAFORM_PLAN)

terraform.limits: terraform.plan
@ $(AWS_LIMITS)

# Upgrading TF from v0.12 to v0.13
terraform.13upgrade:
@ echo "Terraform upgrade to v0.13 :"
@ echo "-----------------------------"
@ $(CMD_TERRAFORM_MODULES_UPGRADE)

env.use: terraform jq
@ [ -e $(ENV_DIR) ] && \
( \
Expand Down

0 comments on commit 5888ed5

Please sign in to comment.